大家好,我有一个选项卡式活动,在第一个选项卡中,我从服务器获取数据,并将其显示在卡片视图中的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();
}
}
7条答案
按热度按时间d8tt03nd1#
您可以使用android SwipeRefreshLayout小部件代替
ProgressDialog
。按照以下步骤将
SwipeRefreshLayout
集成到Tab1history
片段中:**1.**在您的布局
tab1history
中,添加SwipeRefreshLayout
作为根布局,并将RecyclewrView
放在其中。**2.**在您的
Tab1History
片段中,使用SwipeRefreshLayout
从服务器加载数据,如下所示:希望这能正常工作。
z5btuh9x2#
XML程式码
Java程式码
6mw9ycah3#
源代码
https://drive.google.com/open?id=1qjJ_to-1knVNaJB4T3U_L_p_YYNvgAeZ
APK
https://drive.google.com/open?id=1MxQZwjIXgR2jgDkUW1mbFrTLMSaQQisC
np8igboo4#
Android有一个SwipeToRefresh小部件可以做到这一点。
此问题之前已在how-to-implement-pull-down-refresh-in-android和how-to-implement-android-pull-to-refresh中得到解答
jm81lzqq5#
在Android Studio中的示例- SwipeRefreshLayout.我希望这段代码能正常工作,你可以试试这段代码。
基本提取以刷新/ SwipeRefreshLayout XML代码:
您可以在下面下载代码,查看最终输出和基本Pull To refresh示例的逐步说明。获取示例https://abhiandroid.com/materialdesign/pulltorefresh的完整代码
8ftvxx2r6#
kpbwa7wx7#
不使用
SwipeRefreshLayout
。例如,如果您想从互联网上获取数据,而您的RecyclerView
已经完全向上/向下滚动 (您的顶部项目可见),并且您正在尝试继续滚动。如果在这种情况下,您想从net调用getListItems
: