我使用View-pager
设置标签。当我点击任何类别加载来自互联网的数据时,我会隐藏recycleview
内的所有类别和数据加载。但现在当我点击类别选项卡时,我希望在查看之前可见。请注意:我想点击相同的标签,每次需要刷新。请检查图像以便更好地理解。请检查我的采用者类。
viewPager = (CustomViewPager) findViewById(R.id.tabs_viewpager);
// viewPager.setPagingEnabled(false);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabFragmentPagerAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this, viewPager);
tabFragmentPagerAdapter.addFrag(new FeatureFragment(), "Feature");
tabFragmentPagerAdapter.addFrag(new DiscoverFragment(), "Discover");
tabFragmentPagerAdapter.addFrag(new TopFragment(), "Top");
tabFragmentPagerAdapter.addFrag(new CategoryFragment(), "Category");
// setupViewPager(viewPager);
viewPager.setAdapter(tabFragmentPagerAdapter);
My Adopter课程
public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mfragmentlist = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
FragmentManager fragmentManager;
final int[] ICONS = new int[]{
R.drawable.feature,
R.drawable.discover,
R.drawable.top,
R.drawable.category};
public Context context;
public ViewPager viewPager;
public TabFragmentPagerAdapter(FragmentManager fm, Context context, ViewPager viewPager) {
super(fm);
this.context = context;
this.viewPager = viewPager;
this.fragmentManager = fm;
}
public View getTabView(int position) {
// Given you have a custom layout in `res/layout/custom_tab.xml` with a TextView and ImageView
View v = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
final TextView tv = (TextView) v.findViewById(R.id.textViewMainTabFrag);
tv.setText(mFragmentTitleList.get(position));
ImageView img = (ImageView) v.findViewById(R.id.imgView);
img.setImageResource(ICONS[position]);
return v;
}
public void SetOnSelectView(TabLayout tabLayout, int position) {
TabLayout.Tab tab = tabLayout.getTabAt(position);
View selected = tab.getCustomView();
TextView iv_text = (TextView) selected.findViewById(R.id.textViewMainTabFrag);
iv_text.setTextColor(context.getResources().getColor(R.color.apps_color));
}
public void SetUnSelectView(TabLayout tabLayout, int position) {
TabLayout.Tab tab = tabLayout.getTabAt(position);
View selected = tab.getCustomView();
TextView iv_text = (TextView) selected.findViewById(R.id.textViewMainTabFrag);
iv_text.setTextColor(context.getResources().getColor(R.color.black));
}
public void addFrag(Fragment fragment, String title) {
mfragmentlist.add(fragment);
mFragmentTitleList.add(title);
}
/* @Override
public Fragment getItem(int position) {
return ItemFragment.newInstance();
}*/
@Override
public Fragment getItem(int position) {
return mfragmentlist.get(position);
}
@Override
public int getCount() {
return mFragmentTitleList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
主要活动OnTabSelectedListener
这里是重新加载activity
的好选择。但是如何以及如何看到我的观点
@Override
public void onTabUnselected(TabLayout.Tab tab) {
int c = tab.getPosition();
tabFragmentPagerAdapter.SetUnSelectView(tabLayout, c);
}
答案 0 :(得分:2)
您可以使用setUserVisibleHint
:
在fragment
:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser){
//do something //Load or Refresh Data
}
}
OR
在Activity
:
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// do something //Load or Refresh Data
}
});
检查this解决方案
答案 1 :(得分:1)
试试这个......
public class ListCanali extends Fragment implements OnPageChangeListener{
private MyAdapter mAdapter;
@Override
public View OnCreateView (...) {
...
if(mAdapter == null) {
mAdapter = new MyAdapter(getChildFragmentManager());
}
viewPager.setAdapter(mAdapter);
viewPager.setOnPageChangeListener(this);
...
}
@Override
public void OnPageSelected (int position) {
switch (position){
case 0 :
((FragmentCanali)adapter.getItem(position)).refresh();
case 1 :
((FragmentPreferiti)adapter.getItem(position)).refresh();
}
}