android-fragments 拉入以刷新recyclerview android

vngu2lb8  于 2022-11-14  发布在  Android
关注(0)|答案(7)|浏览(333)

大家好,我有一个选项卡式活动,在第一个选项卡中,我从服务器获取数据,并将其显示在卡片视图中的recyclerview中。(所以每当我拉它必须做的网络请求)。我还想禁用网络请求每当我切换标签(因为当我在我的应用程序中更改选项卡焦点时,它会开始获取数据)我只想执行一次网络请求(当用户第一次登录时),然后其他请求仅通过拉刷新来管理。
这是我的片段,我在其中循环查看并显示数据:

public class Tab1History extends Fragment
{

private RecyclerView recyclerView;
private CespiteAdapter adapter;
UserSessionManager session;

private static final String URL_DATA = "http://mydata.php";

private List<CespiteOgg> cespiteOggList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{

    View rootView = inflater.inflate(R.layout.tab1history, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
    recyclerView.setHasFixedSize(true);//every item of the RecyclerView has a fix size
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    cespiteOggList = new ArrayList<>();

    loadRecyclerViewData();

            return rootView;
}

private void loadRecyclerViewData()
{
    // Session class instance
    session = new UserSessionManager(getActivity());
    //get user data from session
    HashMap<String, String> user = session.getUserDetails();
    //get name
    String name = user.get(UserSessionManager.KEY_NAME);
    // get username
    final String usernameUtente = user.get(UserSessionManager.KEY_USERNAME);

    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Carimento dati...");
    progressDialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            URL_DATA,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    progressDialog.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        JSONArray array = jsonObject.getJSONArray("dates");

                        for(int i=0; i<array.length(); i++)
                        {
                            JSONObject o = array.getJSONObject(i);
                            CespiteOgg item = new CespiteOgg(
                                    o.getString("CodNumInventario"),
                                    o.getString("Nome"),
                                    o.getString("DtCatalogazione"),
                                    o.getString("CodIdA"),
                                    o.getString("username")
                            );
                            cespiteOggList.add(item);
                        }

                        adapter = new CespiteAdapter(cespiteOggList, getActivity());
                        recyclerView.setAdapter(adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                }
            })
            {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("Username", usernameUtente);
                    return params;
                }
            };

    RegisterRequest.getmInstance(getActivity()).addToRequestque(stringRequest);

}
}

这是适配器

public class CespiteAdapter extends RecyclerView.Adapter<CespiteAdapter.ViewHolder>
{

private List<CespiteOgg> cespiteOggList;
private Context context;

public CespiteAdapter(List<CespiteOgg> cespiteOggList, Context context) {

    this.cespiteOggList = cespiteOggList;
    this.context = context;
}

public class ViewHolder extends RecyclerView.ViewHolder
{

    public CardView cv;
    public TextView txtNumInventario;
    public TextView txtNomeCespite;
    public TextView txtDtCatalogazione;
    public TextView txtAula;
    public TextView txtNomeUser;

    ViewHolder(View itemView)
    {

        super (itemView);
        //cv = (CardView) itemView.findViewById(R.id.cardView);
        txtNumInventario = (TextView) itemView.findViewById(R.id.txtNumeroInventario);
        txtNomeCespite = (TextView) itemView.findViewById(R.id.txtNomeCespite);
        txtDtCatalogazione = (TextView) itemView.findViewById(R.id.txtDataCatalogazione);
        txtAula = (TextView) itemView.findViewById(R.id.txtAula);
        txtNomeUser= (TextView) itemView.findViewById(R.id.txtNomeUser);

    }
}


@Override
public CespiteAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{

    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    View cespiteView = inflater.inflate(R.layout.cespite_card_view, parent, false);

    return new ViewHolder(cespiteView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position)
{

    CespiteOgg cespiteOgg = cespiteOggList.get(position);

    holder.txtNumInventario.setText(cespiteOgg.getNumInventario());
    holder.txtNomeCespite.setText(cespiteOgg.getNomeCespite());
    holder.txtDtCatalogazione.setText(cespiteOgg.getDtCatalogazione());
    holder.txtAula.setText(cespiteOgg.getAula());
    holder.txtNomeUser.setText(cespiteOgg.getNomeUser());

}

@Override
public int getItemCount()
{

    return cespiteOggList.size();
}

}
d8tt03nd

d8tt03nd1#

您可以使用android SwipeRefreshLayout小部件代替ProgressDialog

按照以下步骤将SwipeRefreshLayout集成到Tab1history片段中:
**1.**在您的布局tab1history中,添加SwipeRefreshLayout作为根布局,并将RecyclewrView放在其中。

// tab1history.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>

**2.**在您的Tab1History片段中,使用SwipeRefreshLayout从服务器加载数据,如下所示:

// Tab1History.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class Tab1History extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView recyclerView;
    private CespiteAdapter adapter;
    UserSessionManager session;

    private static final String URL_DATA = "http://mydata.php";

    private List<CespiteOgg> cespiteOggList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View rootView = inflater.inflate(R.layout.tab1history, container, false);

        recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(true);//every item of the RecyclerView has a fix size
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        cespiteOggList = new ArrayList<>();

        // SwipeRefreshLayout
        mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,
                android.R.color.holo_green_dark,
                android.R.color.holo_orange_dark,
                android.R.color.holo_blue_dark);

        /**
         * Showing Swipe Refresh animation on activity create
         * As animation won't start on onCreate, post runnable is used
         */
        mSwipeRefreshLayout.post(new Runnable() {

            @Override
            public void run() {

                mSwipeRefreshLayout.setRefreshing(true);

                // Fetching data from server
                loadRecyclerViewData();
            }
        });

        return rootView;
    }

    /**
     * This method is called when swipe refresh is pulled down
     */
    @Override
    public void onRefresh() {

        // Fetching data from server
        loadRecyclerViewData();
    }

    private void loadRecyclerViewData()
    {
        // Showing refresh animation before making http call
        mSwipeRefreshLayout.setRefreshing(true);

        // Session class instance
        session = new UserSessionManager(getActivity());
        //get user data from session
        HashMap<String, String> user = session.getUserDetails();
        //get name
        String name = user.get(UserSessionManager.KEY_NAME);
        // get username
        final String usernameUtente = user.get(UserSessionManager.KEY_USERNAME);

        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                URL_DATA,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {

                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONArray array = jsonObject.getJSONArray("dates");

                            for(int i=0; i<array.length(); i++)
                            {
                                JSONObject o = array.getJSONObject(i);
                                CespiteOgg item = new CespiteOgg(
                                        o.getString("CodNumInventario"),
                                        o.getString("Nome"),
                                        o.getString("DtCatalogazione"),
                                        o.getString("CodIdA"),
                                        o.getString("username")
                                );
                                cespiteOggList.add(item);
                            }

                            adapter = new CespiteAdapter(cespiteOggList, getActivity());
                            recyclerView.setAdapter(adapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        // Stopping swipe refresh
                        mSwipeRefreshLayout.setRefreshing(false);

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        // Stopping swipe refresh
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Username", usernameUtente);
                return params;
            }
        };

        RegisterRequest.getmInstance(getActivity()).addToRequestque(stringRequest);

    }
}

希望这能正常工作。

z5btuh9x

z5btuh9x2#

XML程式码

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipe_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/layout_titlebar">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/layout_titlebar"
        android:layout_margin="5dp"
        />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Java程式码

public class BooksActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

            SwipeRefreshLayout swipLayout;
            RecyclerView recyclerview;

        @Override
            protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity);
                recyclerview = view.findViewById(R.id.recyclerview);
                swipLayout = view.findViewById(R.id.swipe_layout);
                swipLayout.setOnRefreshListener(this);
            }//end of onCreate

        @Override
            public void onRefresh() {
        //your refresh code here
           loadRecyclerViewData()
             }

        private void loadRecyclerViewData(){
        onSuccess(){
        //don't forget to stop refreshing
        swipLayout.setRefreshing(false);
        }

        onFaliure(){
        //don't forget to stop refreshing
        swipLayout.setRefreshing(false);
        }
    }
    }
6mw9ycah

6mw9ycah3#

源代码

https://drive.google.com/open?id=1qjJ_to-1knVNaJB4T3U_L_p_YYNvgAeZ

APK

https://drive.google.com/open?id=1MxQZwjIXgR2jgDkUW1mbFrTLMSaQQisC

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    SwipeRefreshLayout mSwipeRefreshLayout;

        // SwipeRefreshLayout
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);

        mSwipeRefreshLayout.setOnRefreshListener(this);
//        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,
                android.R.color.holo_green_dark,
                android.R.color.holo_orange_dark,
                android.R.color.holo_blue_dark);

        /**
         * Showing Swipe Refresh animation on activity create
         * As animation won't start on onCreate, post runnable is used
         */
        mSwipeRefreshLayout.post(new Runnable() {

            @Override
            public void run() {

                if(mSwipeRefreshLayout != null) {
                    mSwipeRefreshLayout.setRefreshing(true);
                }
                // TODO Fetching data from server
                fetchContacts();
            }
        });


  @Override
    public void onRefresh() {
        fetchContacts();
    }
np8igboo

np8igboo4#

Android有一个SwipeToRefresh小部件可以做到这一点。
此问题之前已在how-to-implement-pull-down-refresh-in-androidhow-to-implement-android-pull-to-refresh中得到解答

jm81lzqq

jm81lzqq5#

在Android Studio中的示例- SwipeRefreshLayout.我希望这段代码能正常工作,你可以试试这段代码。

基本提取以刷新/ SwipeRefreshLayout XML代码:

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/simpleSwipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        < Add View's Here..../>
</android.support.v4.widget.SwipeRefreshLayout>

您可以在下面下载代码,查看最终输出和基本Pull To refresh示例的逐步说明。获取示例https://abhiandroid.com/materialdesign/pulltorefresh的完整代码

8ftvxx2r

8ftvxx2r6#

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout> 


  SwipeRefreshLayout swipeLayout;  

  //Getting SwipeContainerLayout
  swipeLayout = findViewById(R.id.swipe_container);;

    //swip listener
    swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            loadData();
            swipeLayout.setRefreshing(false);
        }
    });
}
kpbwa7wx

kpbwa7wx7#

不使用SwipeRefreshLayout。例如,如果您想从互联网上获取数据,而您的RecyclerView已经完全向上/向下滚动 (您的顶部项目可见),并且您正在尝试继续滚动。如果在这种情况下,您想从net调用getListItems

itemsRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
    int scrolling = 0;
    int scrollingOnPrevIdleState = 0; // -1 or less prevents call getItemList() on first try

    @Override
    public void onScrollStateChanged(RecyclerView rv, int newState) {
        super.onScrollStateChanged(rv, newState);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            if (scrolling == scrollingOnPrevIdleState) {
                myViewModel.getItemList();
                // HERE IS THE PLACE where your refreshing data method is calling
            }
            scrollingOnPrevIdleState = scrolling;
        }
    }
    @Override
    public void onScrolled(RecyclerView rv, int dx, int dy) {
        super.onScrolled(rv, dx, dy);
        scrolling += Math.abs(dy);
    }
});

相关问题