gpt4 book ai didi

android - 避免在 Pager Sliding Tab 中加载所有 fragment

转载 作者:行者123 更新时间:2023-11-29 19:46:12 39 4
gpt4 key购买 nike

我在 PagerSlidingTab 中有几个 fragment 。因为一次性加载所有 fragment ,它没有良好的性能和质量(有时加载正确,有时转到 Volley onResponseError )。

在 fragment 中,我加载 json 数据并使用 Json 数据填充 RecyclerView

One of my fragment :

public class Fragment_four extends Fragment {
private boolean isFragmentLoaded = false;
private static final String FIRST_URL = "http://myapiservice.com/api/3";
private ProgressBar loadingProgressBar;
private ArrayList<ModelDars> darsha = new ArrayList<ModelDars>();
private RecyclerView reyclerCharDab;
private AdapterRecyclerDars adapterRecyclerDars;
private String after_url;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_four, container, false);

//======================================
loadingProgressBar = (ProgressBar) view.findViewById(R.id.prg_loading_frg_four);

//recyclerview init
reyclerCharDab = (RecyclerView) view.findViewById(R.id.recycler_frg_chaharom_dab);
reyclerCharDab.setHasFixedSize(true);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
reyclerCharDab.setLayoutManager(gridLayoutManager);
adapterRecyclerDars = new AdapterRecyclerDars(darsha, getActivity(), FIRST_URL, getActivity());
reyclerCharDab.setAdapter(adapterRecyclerDars);

if (helpers.isOnline(getActivity())) {
//creating volley object request
JsonObjectRequest request = new JsonObjectRequest(FIRST_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null || response.length() > 0) {
try {
JSONArray data = response.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject currentData = data.getJSONObject(i);
ModelDars newModelDars = new ModelDars();

newModelDars.setDarsId(currentData.getString("id"));
newModelDars.setDarsTitle(currentData.getString("title"));
newModelDars.setDarsPoster(currentData.getString("poster"));
newModelDars.setDarsVisitCnt(currentData.getString("visitcnt"));
newModelDars.setDarsDate(currentData.getString("date"));
newModelDars.setDarsCatId(currentData.getString("cat_id"));
newModelDars.setDarsCatName(currentData.getString("cat_name"));

darsha.add(newModelDars);
}
after_url = response.getString("pagination");

} catch (JSONException e) {
e.printStackTrace();
}
//notify adapter about data changed
adapterRecyclerDars.notifyDataSetChanged();
loadingProgressBar.setVisibility(View.GONE);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loadingProgressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), "failed to fetch data from internet ", Toast.LENGTH_SHORT).show();
}
});
AppController.getInstance().addToRequestQueue(request);

// LOAD MORE FUNCTION =====================================================================

reyclerCharDab.addOnScrollListener(new EndlessRecyclerViewScrollListener(gridLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount) {
if (after_url == "null") {

} else {

JsonObjectRequest request = new JsonObjectRequest(after_url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null || response.length() > 0) {

try {
JSONArray data = response.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject currentData = data.getJSONObject(i);
ModelDars newModelDars = new ModelDars();

newModelDars.setDarsId(currentData.getString("id"));
newModelDars.setDarsTitle(currentData.getString("title"));
newModelDars.setDarsPoster(currentData.getString("poster"));
newModelDars.setDarsVisitCnt(currentData.getString("visitcnt"));
newModelDars.setDarsDate(currentData.getString("date"));
newModelDars.setDarsCatId(currentData.getString("cat_id"));
newModelDars.setDarsCatName(currentData.getString("cat_name"));

darsha.add(newModelDars);
}
after_url = response.getString("pagination");


} catch (JSONException e) {
e.printStackTrace();
}
//notify adapter about data changed
adapterRecyclerDars.notifyDataSetChanged();

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

}
});
AppController.getInstance().addToRequestQueue(request);
}
}
});
} else {
Toast.makeText(getActivity(), "You dont have internet !", Toast.LENGTH_SHORT).show();
loadingProgressBar.setVisibility(View.GONE);
}
return view;
}
}

And This is the view pager adapter :

public class MainActivityPagerAdapter extends FragmentStatePagerAdapter {
//============================================ Constructor
public MainActivityPagerAdapter(FragmentManager fm) {
super(fm);
}

//============================================ GetItem Method ()
@Override
public Fragment getItem(int position) {
Fragment frag = null;
switch (position) {
case 0:
frag = new Fragment_one();
break;
case 1:
frag = new Fragment_two();
break;
case 2:
frag = new Fragment_three();
break;
case 3:
frag = new Fragment_four();
break;
}
return frag;
}
//============================================= GetCount Method ()
@Override
public int getCount() {
return 4;
}
//============================================= GetPageTitle
@Override
public CharSequence getPageTitle(int position) {
String title = "";
switch (position) {
case 0:
title = "one";
break;
case 1:
title = "two";
break;
case 2:
title = "three";
break;
case 3:
title = "four";
break;
}
return title;
}
}

最佳答案

根据您的评论,这里有一个只实例化 fragment 一次的 fragment ,并且只返回要显示的 fragment :

public class MainActivityPagerAdapter extends FragmentStatePagerAdapter {
private Fragment_one fragment_one;
private Fragment_two fragment_two;
private Fragment_three fragment_three;
private Fragment_four fragment_four;

//============================================ Constructor
public MainActivityPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
fragment_one = new Fragment_one();
fragment_two = new Fragment_two();
fragment_three = new Fragment_three();
fragment_four = new Fragment_four();
}

//============================================ GetItem Method ()
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return (fragment_one);
case 1:
return (fragment_two);
case 2:
return (fragment_three);
case 3:
default:
return (fragment_four);
}
}
}

关于android - 避免在 Pager Sliding Tab 中加载所有 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37611261/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com