我有4个选项卡,其中相同的片段正在加载不同的数据。但现在我的问题是,当我更改选项卡或转到另一个选项卡时重新加载/刷新片段/页面。我在viewpager中使用了SlidingTabLayout。
请帮我解决这个问题。任何帮助将不胜感激。
我的适配器类如下:
public class HomePageAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
Context context;
private ArrayList<HomeFragment> tabs;
String[] types = {"latest", "nearby", "popular", "loved"};
HomeTab ht=null;
private Map.Entry<HomeTab,HomePageAdapter> ref;
FragmentManager fragmentManager;
private ArrayList<Fragment> oPooledFragments;
public HomePageAdapter(Context context, FragmentManager fm, CharSequence mTitles[], int NumbOfTabsumb,HomeTab ht) {
super(fm);
this.ht=ht;
this.Titles = mTitles;
this.context = context;
this.NumbOfTabs = NumbOfTabsumb;
this.fragmentManager = fm;
ref = new MapEntry<>();
ref.setValue(this);
ref = new AbstractMap.SimpleEntry<>(ht, this);
this.tabs = new ArrayList();
for(int i=0;i<this.NumbOfTabs;i++)
{
this.tabs.add(new HomeFragment(this.types[i], ref));
}
}
@Override
public Fragment getItem(int position) {
try {
GlobalClass.closeKeyBoard(context);
} catch (Exception e) {
e.printStackTrace();
}
return this.tabs.get(position);
}
@Override
public int getCount() {
return NumbOfTabs;
}
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
}
我的片段也在这里给出:
public class HomeTab extends Fragment {
HomePageAdapter adapter;
ViewPager pager;
private Toolbar toolBar;
CharSequence Titles[] = {"LATEST", "NEARBY", "POPULAR", "LOVED"};
int Numboftabs = 4;
SlidingTabLayout tabs;
Context context;
int selectedTab;
private RecyclerView mRecyclerView;
private int pos;
private HomeFragment hm = new HomeFragment();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_tab, container, false);
context = container.getContext();
adapter = new HomePageAdapter(getActivity(), getChildFragmentManager(), Titles, Numboftabs,this);
pager = (ViewPager) rootView.findViewById(R.id.pager);
pager.setAdapter(adapter);
//Assigning the sliding layout tab View
tabs = (SlidingTabLayout) rootView.findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); //This makes the tab width evenly in the given space.
tabs.setViewPager(pager);
toolBar = (Toolbar) (getActivity()).findViewById(R.id.tool_bar);
tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
try {
Fragment f = (HomeFragment)adapter.getItem(position);
if (f != null) {
View fragmentView = f.getView();
//mine one is RecyclerView
mRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.my_recycler_view);
Log.v("recyclerView",mRecyclerView+"");
// if (mRecyclerView != null)
// mRecyclerView.smoothScrollToPosition(0);
}
} catch (NullPointerException npe) {
Log.v("exception",npe+"");
}
}
@Override
public void onPageSelected(int position)
{
try {
if (getActivity() instanceof DrawerContainer) {
filterList = (HomeFragment) adapter.getItem(position);
GlobalClass.setUpdatePosts((HomeFragment) adapter.getItem(position));
if (GlobalClass.pusherClass != null) {
GlobalClass.pusherClass.setUpadteUiOfActivity((HomeFragment) adapter.getItem(position));
}
Fragment f = (HomeFragment) adapter.getItem(position);
if (f != null) {
View fragmentView = f.getView();
//mine one is RecyclerView
mRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.my_recycler_view);
onResume();
// if (mRecyclerView != null)
// mRecyclerView.smoothScrollToPosition(0);
}
}
adapter.notifyDataSetChanged();
}
catch(NullPointerException npe){
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
if(getActivity() instanceof DrawerContainer && adapter != null && adapter.getCount() > 0)
{
filterList = (HomeFragment)adapter.getItem(0);
GlobalClass.setUpdatePosts((HomeFragment)adapter.getItem(0));
if (GlobalClass.pusherClass != null) {
GlobalClass.pusherClass.setUpadteUiOfActivity((HomeFragment)adapter.getItem(0));
}
}
(toolBar.findViewById(R.id.ic_icon)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mRecyclerView.smoothScrollToPosition(0);
}
});
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(android.R.color.white);
}
});
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
GlobalClass.fromScreen = "";
}
@Override
public void setUserVisibleHint(boolean visible) {
super.setUserVisibleHint(visible);
if (visible && isResumed()) {
onResume();
}
}
@Override
public void onResume() {
super.onResume();
if (!getUserVisibleHint()) {
return;
}
}
}