交换选项卡时未获取数据
Posted
技术标签:
【中文标题】交换选项卡时未获取数据【英文标题】:Data is not fetching when swap the tab 【发布时间】:2018-05-26 01:27:08 【问题描述】:我已经实现了一个应用程序,其中有 7 个选项卡,当用户滚动列表时,我使用 addOnScrollListener
来获取数据。我的问题是当我将一个fragment
或标签移动到另一个fragment
然后返回到之前的标签时,数据不会第二次出现在该标签上。
下面是我的片段类
public class EI_Pending_Fragment extends Fragment
SessionManager session;
private List<SuperHero> listSuperHeroes;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
public ProgressBar progressBar;
private RequestQueue requestQueue;
private int requestCount1 = 1;
private Boolean isStarted = false;
private Boolean isVisible = false;
public String email;
public EI_Pending_Fragment()
@Override
public void onStart()
super.onStart();
isStarted = true;
if (isVisible && isStarted)
getData();
@Override
public void setUserVisibleHint(boolean isVisibleToUser)
super.setUserVisibleHint(isVisibleToUser);
isVisible = isVisibleToUser;
if (isStarted && isVisible)
getData();
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view= inflater.inflate(R.layout.activity_main_test, container, false);
session = new SessionManager(getActivity());
// get user data from session
HashMap<String, String> user = session.getUserDetails();
email = user.get(SessionManager.KEY_EMAIL);
return view;
public void onViewCreated(View v, Bundle savedInstanceState)
super.onViewCreated(v, savedInstanceState);
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
listSuperHeroes = new ArrayList<>();
requestQueue = Volley.newRequestQueue(getContext());
// getData();
adapter = new CardAdapter(listSuperHeroes, getActivity());
recyclerView.setAdapter(adapter);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int
newState)
super.onScrollStateChanged(recyclerView, newState);
if (isLastItemDisplaying(recyclerView))
if(requestCount1==1)
exit();
else
getData();
);
private JsonArrayRequest getDataFromServer(int requestCount)
Log.e("email",email);
final String DATA_URL = "http://192.168.2.110/xp/ei_pending_received.php?matri_id="+email+"&page=";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL + String.valueOf(requestCount),
new Response.Listener<JSONArray>()
@Override
public void onResponse(JSONArray response)
Log.e("response",response.toString());
if(response.length()==0)
requestCount1 = 1;
else
parseData(response);
progressBar.setVisibility(View.GONE);
,
new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
);
return jsonArrayRequest;
private void getData()
requestQueue.add(getDataFromServer(requestCount1));
requestCount1++;
private void parseData(JSONArray array)
for (int i = 0; i < array.length(); i++)
//Log.e("array",array.toString().trim());
SuperHero superHero = new SuperHero();
JSONObject json = null;
try
json = array.getJSONObject(i);
superHero.setImageUrl(json.getString(Config_Test.TAG_IMAGE_URL));
superHero.setMglId(json.getString(Config_Test.TAG_MGLID));
superHero.setAge(json.getString(Config_Test.TAG_AGE));
superHero.setAgeHeight(json.getString(Config_Test.TAG_HEIGHT));
superHero.setCommunity(json.getString(Config_Test.TAG_COMMUNITY));
superHero.setCaste(json.getString(Config_Test.TAG_CASTE));
superHero.setOccupation(json.getString(Config_Test.TAG_OCCUPATION));
superHero.setIncome(json.getString(Config_Test.TAG_INCOME));
superHero.setStatus(json.getString(Config_Test.TAG_STATUS));
superHero.setRequested_On(json.getString(Config_Test.TAG_REQUESTED_ON));
catch (JSONException e)
e.printStackTrace();
listSuperHeroes.add(superHero);
adapter.notifyDataSetChanged();
private boolean isLastItemDisplaying(RecyclerView recyclerView)
if (recyclerView.getAdapter().getItemCount() != 0)
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
return true;
return false;
void exit()
Toast.makeText(getContext(),"no more data avilable",Toast.LENGTH_SHORT).show();
下面是我添加所有选项卡的 Activity 类
private void setupViewPager(ViewPager viewPager)
int numberOfPages=7;
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new NewMatches_Tab(), "New matches");
adapter.addFragment(new Broader_Match_Tab(), "Broader Matches");
adapter.addFragment(new Similar_Matchs_Tab(), "Similar Matches");
adapter.addFragment(new ShortlistTab(), "ShortListed");
adapter.addFragment(new Viewed_My_Profile(), "View My Profile");
adapter.addFragment(new ShortListedMeTab(), "ShortListed Me");
adapter.addFragment(new Photo_Request_Received(), "Photo Request Received");
//viewPager.setOffscreenPageLimit(numberOfPages);
viewPager.setAdapter(adapter);
【问题讨论】:
发布你的活动,它在一个浏览器中包含所有 7 个片段 尊敬的先生,检查我更新的问题 将你的片段onCreate
代码移动到onResume
或活动viewpager.setoffscreenlimit(7)
取消注释这一行 viewPager.setOffscreenPageLimit(7);
当我取消注释这一行时,当应用程序打开时,所有片段的数据都会在第一次加载
【参考方案1】:
您可以在活动中添加一个 OnPageChangeListener 并将其注册到您的视图寻呼机并在 OnPageChangeListener 实例中进行所有必要的“设置”。
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener()
@Override
public void onPageSelected(int position)
//Do your fragment refreshing here
mCurrentPage = position;
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
@Override
public void onPageScrollStateChanged(int arg0)
;
@Override
public void setFragmentsForContents(List<Content> contents) //bundle data for your different fragments
if(contents!=null)
NUM_PAGES = contents.size();
mContentPager.addOnPageChangeListener(viewPagerPageChangeListener);
mContentPager.setAdapter(mScreenSlidePagerAdapter);
mContentPager.setCurrentItem(mCurrentPage);
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
ScreenSlidePagerAdapter(FragmentManager fm)
super(fm);
@Override
public Fragment getItem(int position)
return ContentFragment.newInstance(mContents.get(position).getContent());
@Override
public int getCount()
return NUM_PAGES;
@NonNull
@Override
public Object instantiateItem(ViewGroup container, int position)
return super.instantiateItem(container, position);
@Override
public float getPageWidth(int position)
return 1.0f;
【讨论】:
以上是关于交换选项卡时未获取数据的主要内容,如果未能解决你的问题,请参考以下文章
当用户返回到 UITabBarController 选项卡时如何刷新表数据