TabLayout无法正常工作,当向右滚动时,它可以正常工作但在向左滚动后它会显示相同的数据。我在SectionPagerAdapter中调用了Same Fragment类九次并尝试在Recyclerview中动态更改数据。
public class ViewPagerFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager mViewPager;
private SectionsPagerAdapter mSectionsPagerAdapter;
// private ArrayList tabName;
private String[] tabName = {"bangladesh","international","economy" ,"opinion","sports","entertainment", "features","life-style","northamerica"};
public ViewPagerFragment() {
}
public ViewPagerFragment newInstance(String navigation) {
ViewPagerFragment fragment = new ViewPagerFragment();
Bundle args = new Bundle();
args.putString(Constants.FRAG_A, navigation);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_fragment, container, false);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
mViewPager = (ViewPager)view.findViewById(R.id.container);
for(int i=0; i<=8; i++){
tabLayout.addTab(tabLayout.newTab().setText(tabName[i]));
}
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager(),tabLayout.getTabCount(),tabName);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition()); Log.v("pagerF ^&%&^^&: ",tabName[tab.getPosition()]);
getChildFragmentManager().beginTransaction().addToBackStack(null).commit();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Note that we are passing childFragmentManager, not FragmentManager
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager(),tabLayout.getTabCount(),tabName);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public void onResume() {
super.onResume();
if (mSectionsPagerAdapter!=null){
mSectionsPagerAdapter.notifyDataSetChanged();
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
String[] tabName;
public SectionsPagerAdapter(FragmentManager fm, int NumOfTabs, String[] tabName) {
super(fm);
this.mNumOfTabs = NumOfTabs;
this.tabName=tabName;
}
@Override
public Fragment getItem(int position) {
HomeFragment comn = new HomeFragment();
return comn.newInstance(tabName[position-1]);
}
@Override
public int getCount() {
Log.v("Count*^&*^ ", String.valueOf(mNumOfTabs));
return mNumOfTabs;
}
}
public class HomeFragment extends Fragment {
public static String fragmentName = "";
HorizontalAdapter adapterhor; VerticalAdapter adapterver;
ProthomAlo prothomAlo;
RecyclerView recyclerViewh, recyclerViewv;
public HomeFragment newInstance(String navigation) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(Constants.FRAG_A, navigation);
fragment.setArguments(args);
fragmentName = navigation;
Log.v("home^^* :", navigation);
return fragment;
}
public HomeFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
recyclerViewh = view.findViewById(R.id.recycler_View_ho);
recyclerViewv = view.findViewById(R.id.recycler_View_ver);
prothomAlo = new ProthomAlo();
prothomAlo.getProthomAloPage(fragmentName, new ProthomAlo.ApiCallback() {
@Override
public void onSuccess(ArrayList<SingleVertical> result) {
adapterhor = new HorizontalAdapter(getActivity(), result);
adapterver = new VerticalAdapter(getActivity(), result);
recyclerViewh.setAdapter(adapterhor); recyclerViewv.setAdapter(adapterver);
recyclerViewv.setLayoutManager(new LinearLayoutManager(getActivity()));
LinearLayoutManager layoutManager
= new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerViewh.setLayoutManager(layoutManager);
// Log.v("$^&^$$^&", fragmentName);
}
});
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume() {
super.onResume();
//adapterver.notifyDataSetChanged(); adapterhor.notifyDataSetChanged();
Log.v("home^^* :", fragmentName);
}
}