我在PagerSlidingTab
中有几个片段。因为所有片段一次加载,它没有良好的性能和质量(有时加载正确,有时会进入Volley onResponseError
)。
在片段中,我加载json数据并用Json数据填充RecyclerView
。
我的一个片段:
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;
}
}
这是视图寻呼机适配器:
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;
}
}
答案 0 :(得分:1)
根据您的评论,这里只是一段实例化片段,只返回要显示的片段:
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);
}
}
}
答案 1 :(得分:1)
您可以通过覆盖setUserVisibleHint来加载特定片段
boolean isVisible;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
isVisible= isVisibleToUser;
if (isVisible) {
//callApi
} else {
Log.d(TAG, "this fragment is now invisible");
}
}