拉动刷新recyclerview android

嗨,我有一个标签式活动,在第一个标签中,我从服务器获取数据并在卡片视图中的recyclerview中显示它。 要从服务器获取数据,我使用排球库。 我想实现Pull to refresh来加载数据(所以每当我拉它就必须向网络发出请求)。 我还希望每当我在标签之间切换时禁用网络请求(因为当我在我的应用程序中更改标签焦点时它开始获取数据)我只想做一次网络请求(当用户第一次登录时)然后其他人只是通过拉动刷新请求。

这是我的片段,我在这里回收并查看数据:

public class Tab1History extends Fragment { private RecyclerView recyclerView; private CespiteAdapter adapter; UserSessionManager session; private static final String URL_DATA = "http://mydata.php"; private List 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 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() { @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 getParams() throws AuthFailureError { Map params = new HashMap(); params.put("Username", usernameUtente); return params; } }; RegisterRequest.getmInstance(getActivity()).addToRequestque(stringRequest); } } 

它是适配器:

 public class CespiteAdapter extends RecyclerView.Adapter { private List cespiteOggList; private Context context; public CespiteAdapter(List 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(); } } 

Solutions Collecting From Web of "拉动刷新recyclerview android"

您可以使用Android SwipeRefreshLayout小部件而不是ProgressDialog

按照以下步骤在您的Tab1history片段中集成SwipeRefreshLayout

1.在布局tab1history ,将SwipeRefreshLayout添加为根布局并将RecyclewrView放入其中。

 // tab1history.xml < ?xml version="1.0" encoding="utf-8"?>    

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 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 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() { @Override public void onResponse(String s) { try { JSONObject jsonObject = new JSONObject(s); JSONArray array = jsonObject.getJSONArray("dates"); for(int i=0; i getParams() throws AuthFailureError { Map params = new HashMap(); params.put("Username", usernameUtente); return params; } }; RegisterRequest.getmInstance(getActivity()).addToRequestque(stringRequest); } } 

希望这会正常运作。

Android有一个SwipeToRefresh小部件来执行此操作。

这个问题在此之前已经得到解答, 如何实现-draw-down-refresh-in-android以及如何实现android-pull-to-refresh

在Android Studio中拉动以刷新ListView和RecyclerView示例 – SwipeRefreshLayout.I希望此代码正常工作,您可以尝试此代码。

基本的Pull To Refresh / SwipeRefreshLayout XML代码:

   < Add View's Here..../>  

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

源代码

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(); }