I have 3 tabs each are sharing a single fragment. But this fragment contains a viewPager. So all the three tabs will have a UI with a viewPager and viewPager have three different fragments. After following the examples available I managed to implement Tab layout with single fragment. But when I tried to implement a viewPager inside this fragment I am facing issue. Tab is not scrolling smoothly and its not moving to third tab. I am also not able to see my viewPager fragment.
Main Fragment with tablayout implementation:
public class DashboardFragment extends android.support.v4.app.Fragment {
Activity mActivity;
private TabLayout tabLayout;
private ViewPager viewPager;
private static final String TAG = "DashboardFragment";
public DashboardFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = getActivity();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dashboard_fragment, container, false);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
viewPager = (ViewPager)view.findViewById(R.id.viewpager);
tabLayout.addTab(tabLayout.newTab().setText("Yesterday"));
tabLayout.addTab(tabLayout.newTab().setText("Last Week"));
tabLayout.addTab(tabLayout.newTab().setText("This Month"));
setupViewPager(viewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
DashboardPagerAdapter:
public class DashboardTabPagerAdapter extends FragmentStatePagerAdapter {
private int mNoOfTabs;
public DashboardTabPagerAdapter(FragmentManager fm, int tabCount) {
super(fm);
mNoOfTabs=tabCount;
}
#Override
public Fragment getItem(int position) {
DashboardYesterdayFragment fragment = new DashboardYesterdayFragment();
Bundle args = new Bundle();
switch (position) {
case 0:
args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_YESTERDAY);
break;
case 1:
args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_LAST_WEEK);
break;
case 2:
args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_THIS_MONTH);
break;
default:
return null;
}
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return mNoOfTabs;
}
}
TabFragment which contains a childViewPager:
public class DashboardYesterdayFragment extends android.support.v4.app.Fragment {
private String title;
private Bundle bundle;
ViewPager mViewPager;
public DashboardYesterdayFragment(){
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
bundle = getArguments();
}
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dashboard_yesterday_layout, container, false);
mViewPager = view.findViewById(R.id.vpPager);
setupViewPager(mViewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
DashboardViewPagerAdapter adapter = new DashboardViewPagerAdapter( getChildFragmentManager(),bundle);
mViewPager.setAdapter(adapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
mViewPager.setPageMargin(24);
}
ChildViewPagerAdapter:
public class DashboardViewPagerAdapter extends FragmentStatePagerAdapter {
private static int NUM_ITEMS = 3;
private Bundle bundle;
public DashboardViewPagerAdapter(FragmentManager fm, Bundle bundle) {
super(fm);
this.bundle=bundle;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return IncidentsFragment.newInstance(bundle);
case 1:
return VehicleUtilizationFragment.newInstance(bundle);
case 2:
return FleetPerformanceFragment.newInstance(bundle);
default:
return null;
}
}
#Override
public int getCount() {
return NUM_ITEMS;
}
}
ChildViewPager first fragment:
public class IncidentsFragment extends Fragment {
private TextView mOverSpeeding,mGeoFence,mIdlingCase,mFuelFilling,mMosetIncidences, mLeastIncidences;
public static IncidentsFragment newInstance(Bundle bundle) {
IncidentsFragment fragmentFirst = new IncidentsFragment();
Bundle args = new Bundle();
args.putBundle("bundle", bundle);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.incident_layout, container, false);
initViews(view);
return super.onCreateView(inflater, container, savedInstanceState);
}
private void initViews(View view) {
mOverSpeeding = (TextView)view.findViewById(R.id.over_speeding_value);
mGeoFence = (TextView)view.findViewById(R.id.geofence_value);
mIdlingCase = (TextView)view.findViewById(R.id.idling_value);
mFuelFilling = (TextView)view.findViewById(R.id.fuel_value);
mMosetIncidences = (TextView)view.findViewById(R.id.vehicle_reg_no);
mLeastIncidences = (TextView)view.findViewById(R.id.least_incidences_value);
}
}
I am confused what is going wrong.
Change this:
DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager(),tabLayout.getTabCount());
to this:
DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getChildFragmentManager(),tabLayout.getTabCount());
Related
I created two fragments: 'scheduleFragment' and 'clockFragment'. After setting up
public class ClockFragment extends AppCompatActivity implements View.OnClickListener {
private int notificationId = 1;
in MainActivity.java, the pageAdapter
scheduleFragment = new ScheduleFragment();
clockFragment = new ClockFragment();
tabLayout.setupWithViewPager(viewPager);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), 0);
viewPagerAdapter.addFragment(clockFragment, "Clock");
viewPagerAdapter.addFragment(scheduleFragment, "Schedule");
viewPager.setAdapter(viewPagerAdapter);
creates an error that states I have to switch refactor the clockFragment into the scheduleFragment so one fragment is created.
I need these two fragments because, in the ClockFragment (tab), the clock must appear to users and on the ScheduleFragment (tab), the schedule of users is present. I am not sure how I can separate the two tabs without causing an error.
Some explanation over why this error occurs and a solution will be greatly appreciated.
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager viewPager;
private TabLayout tabLayout;
private ScheduleFragment scheduleFragment;
private ClockFragment clockFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = findViewById(R.id.view_pager);
tabLayout = findViewById(R.id.tab_layout);
scheduleFragment = new ScheduleFragment();
clockFragment = new ClockFragment();
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.ic_baseline_add_alarm_24);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_baseline_assignment_turned_in_24);
}
private class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments = new ArrayList<>();
private List<String> fragmentTitle = new ArrayList<>();
public ViewPagerAdapter(#NonNull FragmentManager fm, int behavior) {
super(fm, behavior);
}
public ViewPagerAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
public void addFragment(ClockFragment fragment, String title) {
fragments.add(fragment);
fragmentTitle.add(title);
}
#NonNull
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitle.get(position);
}
}
}
First ClockFragment extends Activity which means it is not a fragment.
I will give you an example of how you should do things:
public class ChildFragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.child_fragment_1_layout, container, false);
Button buttonInFragment1 = rootView.findViewById(R.id.button_1);
buttonInFragment1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), "button in fragment 1", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
Now do Fragment 2:
public class ChildFragment2 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.child_fragment_1_layout, container, false);
Button buttonInFragment1 = rootView.findViewById(R.id.button_1);
buttonInFragment1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), "button in fragment 2", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
Now add a new class for the adapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return new ChildFragment1(); //ChildFragment1 at position 0
case 1:
return new ChildFragment2(); //ChildFragment2 at position 1
}
return null; //does not happen
}
#Override
public int getCount() {
return 2; //three fragments
}
}
And your MainActivity would look something like this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
}
}
I'm a beginner who learning Android basic course
I want to make 6 fragments that have same structure (only title, image, texts will be different) by using single fragment.
I tried this code but I can see only one fragment.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.fragment_container);
MoviePagerAdapter adapter = new MoviePagerAdapter(getSupportFragmentManager());
mainfragment = new MainFragment();
adapter.addItem(mainfragment);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(6);
}
ViewPager Adapter (in MainActivity)
class MoviePagerAdapter extends FragmentStatePagerAdapter {
public ArrayList<Fragment> items = new ArrayList<Fragment>();
#Override
public Fragment getItem(int position) {
return items.get(position);
}
public MoviePagerAdapter(#NonNull FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
public void addItem(Fragment item) {
items.add(item);
}
#Override
public int getCount() {
return items.size();
}
}
MainFragment.java
public class MainFragment extends androidx.fragment.app.Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.main_frargment, container,
false);
//...
'i want to make 6fragments with datas below '
movieData = new MovieData("title1", "rate1", "grade1");
addData(movieData);
movieData = new MovieData("title2", "rate2", "grade2");
addData(movieData);
movieData = new MovieData("title3", "rate3", "grade3");
addData(movieData);
movieData = new MovieData("title4", "rate4", "grade4");
addData(movieData);
movieData = new MovieData("title5", "rate5", "grade5");
addData(movieData);
movieData = new MovieData("title6", "rate6", "grade6");
addData(movieData);
return rootView;
}
public void addData(MovieData movieData){
Bundle bundle = new Bundle();
bundle.putParcelable("data", movieData);
MainFragment mainFragment = new MainFragment();
mainFragment.setArguments(bundle);
title.setText(movieData.title);
reservation_rate.setText(movieData.getReservation_rate());
grade.setText(movieData.getGrade());
image.setImageResource(R.drawable.image6);
}
}
change your code like this:
public class MainActivity extends AppCompatActivity {
private MoviePagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.fragment_container);
adapter = new MoviePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(6);
for(int i =0;i<6;i++){
createFrag(i);
}
}
}
private void createFrag(int number) {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
args.putInt("NUMBER",number);
fragment.setArguments(args);
adapter.addItem(fragment);
adapter.notifyDataSetChanged();
}
class MoviePagerAdapter extends FragmentStatePagerAdapter {
public ArrayList<Fragment> items = new ArrayList<Fragment>();
#Override
public Fragment getItem(int position) {
return items.get(position);
}
public MoviePagerAdapter(#NonNull FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
public void addItem(Fragment item) {
items.add(item);
}
#Override
public int getCount() {
return items.size();
}
}
and change your Fragment like this:
public class MainFragment extends androidx.fragment.app.Fragment {
//1st
private int number;
#Nullable
//this is variant 1 - with static Movie data
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.main_frargment,
container,
false);
//...
//2d
assert getArguments() != null;
number = getArguments().getInt("NUMBER");
switch (number) {
case 0:
MmovieData movieData1 = new MovieData("title1", "rate1",
"grade1");
addData(movieData1);
break;
case 1:
MmovieData movieData2 = new MovieData("title2", "rate2",
"grade2");
addData(movieData2);
break;
//...
}
return rootView;
}
public void addData(MovieData movieData){
title.setText(movieData.title);
reservation_rate.setText(movieData.getReservation_rate());
grade.setText(movieData.getGrade());
image.setImageResource(R.drawable.image6);
}
}
and this is variant 2 ( with dynamic data and listener)
public class MainFragment extends androidx.fragment.app.Fragment {
//1st
private int number;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.main_frargment,
container,
false);
//...
//2d
assert getArguments() != null;
number = getArguments().getInt("NUMBER");
return rootView;
}
//some method of listener changed
public void dataChanged(MovieData movieData, int position){
if(number==position){
title.setText(movieData.title);
reservation_rate.setText(movieData.getReservation_rate());
grade.setText(movieData.getGrade());
image.setImageResource(R.drawable.image6);
}
}
I am working on a simple implementation of ViewPager in android but everytime I swap my screen addOnPageChangeListener works fine but I see unusual values in my logs inside getItem of my FragmentStatePagerAdapter . and my page swaps but I am unable to change fragment into it. and inside my fragment Slider val (log value) remains same and it hits to zero so each-time when Viewpager is swapped i get the same result.
Activity:
public class OnBoardingActivity extends AppCompatActivity {
.....
.....
static final int ITEMS = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_boarding);
ButterKnife.bind(this);
StatusBarUtil.setTransparent(this);
StatusBarUtil.setLightMode(this);
initPager();
}
private void initPager() {
sliderAdapter = new SliderAdapter(getSupportFragmentManager());
if(sliderAdapter!=null){
onboard_pager.setAdapter(sliderAdapter);
}
onboard_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position==0){
indicators.setImageResource(R.drawable.step1_dots);
}
else if(position==1){
indicators.setImageResource(R.drawable.step2_dots);
}else if(position==2){
indicators.setImageResource(R.drawable.step3_dots);
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
public static class SliderAdapter extends FragmentStatePagerAdapter {
public SliderAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
Log.d("OnBoarding","position "+position);
return OnBoardingSlider.newInstance(position);
}
#Override
public int getCount() {
return ITEMS;
}
}
}
Fragment:
public class OnBoardingSlider extends Fragment {
View mView;
int index;
ImageView img;
TextView head_text,sub_head_text;
public static OnBoardingSlider newInstance(int index){
OnBoardingSlider f = new OnBoardingSlider();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.frag_on_boarding_page,container,false);
return mView;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
img = mView.findViewById(R.id.onboard_pager_img);
head_text = mView.findViewById(R.id.head_text);
sub_head_text = mView.findViewById(R.id.sub_head_text);
super.onViewCreated(view, savedInstanceState);
switch (index){
case 0:
Log.d("Slider val",""+index);
img.setImageResource(R.drawable.purpleimage_slider);
head_text.setText(getString(R.string.ask));
sub_head_text.setText(getString(R.string.ask_sub_head));
head_text.setTextColor(ContextCompat.getColor(getActivity(),R.color.purple_head));
sub_head_text.setTextColor(ContextCompat.getColor(getActivity(),R.color.purple_head_sub));
break;
case 1:
Log.d("Slider val",""+index);
img.setImageResource(R.drawable.blueimage_slider);
head_text.setText(getString(R.string.ask));
sub_head_text.setText(getString(R.string.ask_sub_head));
head_text.setTextColor(ContextCompat.getColor(getActivity(),R.color.blue_head));
sub_head_text.setTextColor(ContextCompat.getColor(getActivity(),R.color.blue_head_sub));
break;
case 2:
Log.d("Slider val",""+index);
img.setImageResource(R.drawable.greenimage_slider);
head_text.setText(getString(R.string.ask));
sub_head_text.setText(getString(R.string.ask_sub_head));
head_text.setTextColor(ContextCompat.getColor(getActivity(),R.color.green_head));
sub_head_text.setTextColor(ContextCompat.getColor(getActivity(),R.color.green_head_sub));
break;
default:
Log.d("Slider val",""+index);
img.setImageResource(R.drawable.purpleimage_slider);
head_text.setText(getString(R.string.ask));
sub_head_text.setText(getString(R.string.ask_sub_head));
head_text.setTextColor(ContextCompat.getColor(getActivity(),R.color.purple_head));
sub_head_text.setTextColor(ContextCompat.getColor(getActivity(),R.color.purple_head_sub));
}
}
}
You are not getting the arguments after setting them. so
in onCreate
Bundle bundle=getArguments();
index = bundle.getInt("index");
TabLayout not working properly, when scrolling right it works correctly sometime but after scrolling left it shows same data. I'm calling Same Fragment class nine times in SectionPagerAdapter and trying to change data dynamically in Recyclerview.
ViewPagerFragment Class
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();
}
}
SectionsPagerAdapter Class
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;
}
}
HomeFragment Class
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);
}
}
I have a Viewpager contains Recylerview which has 2 different holders. One of them is also view pager. Hence I have nested viewpager nad nested fragments. When I tap the second tab and tap back the first tab it is fine .But when I tap the third tab and tap back the first tab. It gives following error:
java.lang.IllegalArgumentException: No view found for id 0x7f0f00ce (:id/viewPagerComments) for fragment CommentFragment
MainAcitivty
viewPager.setAdapter(new MainViewPagerAdapter(getSupportFragmentManager(),models.size()));
navigationTabBar.setModels(models);
navigationTabBar.setViewPager(viewPager, 0);
MainViewPagerAdapter
private int size;
public MainViewPagerAdapter(FragmentManager fm, int size) {
super(fm);
this.size = size;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return DashboardtFragment.newInstance(size, position);
default:
return TempFragment.newInstance(size, position);
}
}
DashboardtFragment
public static DashboardtFragment newInstance(int size, int index) {
Bundle args = new Bundle();
args.putInt(ARG_I,index);
args.putInt(ARG_S,size);
DashboardtFragment fragment = new DashboardtFragment();
fragment.setArguments(args);
return fragment;
}
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
final View v = inflater.inflate(R.layout.dashboard_fragment, null);
return v;
}
#Override
public void showRecyleview(DashboardResponseModel model) {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
DashboardRecyleViewAdapter adapter = new DashboardRecyleViewAdapter(getActivity(),getChildFragmentManager(),model);
recyclerView.setAdapter(adapter);
}
DashboardRecyleViewAdapter
public static class ViewHolder1 extends RecyclerView.ViewHolder {
public ViewHolder1(View itemView) {
super(itemView);
viewPager = itemView.findViewById(R.id.viewPagerComments);
}
}
case 1 :
return new ViewHolder1( layoutInflater.inflate(R.layout.dashboard_comments, parent,
false));
case 1:
ViewHolder1 viewHolder1 = (ViewHolder1) holder;
viewPager.setAdapter(new CommentViewPagerAdapter(
childFragmentManager,
model.getCommentList().size(),
model.getCommentList()
));
break;
CommentViewPagerAdapter
public CommentViewPagerAdapter(FragmentManager fm, int size,
List<CommentList> list) {
super(fm);
this.size = size;
this.list=list;
}
#Override
public Fragment getItem(int position) {
return CommentFragment.newInstance(size,position,list.get(position));
}
CommentFragment
public static CommentFragment newInstance(int size, int index, CommentList
comment) {
Bundle args = new Bundle();
args.putString(ARG_C,new Gson().toJson(comment));
args.putInt(ARG_I,index);
args.putInt(ARG_S,size);
CommentFragment fragment = new CommentFragment();
fragment.setArguments(args);
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState)
{
final View v = inflater.inflate(R.layout.comment_content, null);
}
PagerAdapter mPagerAdapter;
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
super.setAdapter(null);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (mPagerAdapter != null) {
super.setAdapter(mPagerAdapter);
}
}
#Override
public void setAdapter(PagerAdapter adapter) {
}
public void storeAdapter(PagerAdapter pagerAdapter) {
mPagerAdapter = pagerAdapter;
}
first create a customviewpager than add the above code. Use storeAdapter instead of setAdapter.