I have a bottomnavigation in the main activity that has 5 tabs in each file
There are 5 tabs in each of the Fragment
Everything works fine
But when it comes to other Fragment bottomnavigation, viewpager is not updated
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
#Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void setupViewPager(ViewPager viewPager) {
FrgPlayer.ViewPagerAdapter adapter = new FrgPlayer.ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new FrgJadval(), "a");
adapter.addFragment(new FrgKholase(), "b");
adapter.addFragment(new FrgAmar(), "c");
adapter.addFragment(new FrgRokhdad(), "d");
viewPager.setAdapter(adapter);
}
I do not know why the cause of the non-updating of ViewPager
Try calling notifyDataSetChanged():
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
notifyDataSetChanged();
}
Related
I have an Activity inside the activity I have tab layout with two tabs(tab 1, tab 2). Each tab have recyclerview with list of items using Recycleradapter. I want to change the tab title when the user is clicking the recycler child item.
FYI :- I have a child clicklistener inside the adapter.
Activity code
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Fragment(), "Tab1");
adapter.addFragment(new Tab2Fragment(), "Tab2");
viewPager.setAdapter(adapter);
}
Adapter Code
holder.parentlayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I want to change the tab text here
});
You can get the Tab object from your TabLayout using following code and then you can apply your updated text in it:
mTabLayout.getTabAt(index).setText()
Try this way make one method for title update into viewPagerAdapter like below..
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
public void setTitle(String title)
{
mFragmentTitleList.add(title);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
mFragmentList = null;
mFragmentTitleList = null;
container.removeAllViews();
super.destroyItem(container, position, object);
}
public void updateTitle(String title,int position){
mFragmentTitleList.set(position,title);
notifyDataSetChanged();
}
#Override
public Parcelable saveState() {
return null;
}
}
then when you update title then call below method..
mViewPagerAdapter.updateTitle("Your update title");
I want to implement Tablayout addOnTabSelectedListener to open a new Activity on a certain tab click. There are 3 tabs and at present they are connected with fragments. I want to start an activity when I click position 1 tab. I can do that in onTabSelected but before opening activity it shows the fragment attached to that same position. How to remove that fragment?
public class MainActivity extends AppCompatActivity {
private TabLayout tabs;
private ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs = (TabLayout)findViewById(R.id.tabs);
viewpager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewpager);
tabs.addTab(tabs.newTab());
tabs.addTab(tabs.newTab());
tabs.addTab(tabs.newTab());
tabs.setupWithViewPager(viewpager);
tabs.addOnTabSelectedListener(onTabSelectedListener(viewpager));
}
private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager viewPager) {
return 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) {
}
};
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Frag1(), "ONE");
adapter.addFragment(new Frag2(), "TWO");
adapter.addFragment(new Frag3(), "THREE");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs));
onTabSelectedListener(viewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager viewPager) {
return 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) {
}
};
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Frag1(), "ONE");
adapter.addFragment(new Frag2(), "TWO");
adapter.addFragment(new Frag3(), "THREE");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs));
onTabSelectedListener(viewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
try this:
use a FragmentStatePagerAdapter instead the reason for this is that the FragmentPagerAdapter will keep all the views that it loads into memory forever. Where the FragmentStatePagerAdapter disposes of views that fall outside the current
Also Override the adapter method getItemPosition
For example:
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
//... your existing code
#Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
}
source
it will recreate your fragment each time instead of loading them from memory first..
To start activity do it in your particular fragment's onCreate()
Hope this helps..
I had same requirement, what i did is overlap textview on center of TabLayout using RelativeLayout & apply click event on textview. I can not post my layout please refer demo image
i'm using viewpager and FragmentPagerAdapter in my application . in the first activity , this is my code :
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setScrollPosition(2,0f,true);
viewPager.setCurrentItem(2);
private void setupViewPager(ViewPager viewPager) {
Bundle bundle = new Bundle();
bundle.putString("id",id);
Fragment_contacts contact=new Fragment_contacts();
contact.setArguments(bundle);
Fragment_info info=new Fragment_info();
info.setArguments(bundle);
Fragment_Menu menu=new Fragment_Menu() ;
menu.setArguments(bundle);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(contact, "تماس با ما");
adapter.addFragment(info, "درباره ما");
adapter.addFragment(menu, "منو");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I'm using setCurrentItem because my application is no in english and the default selected tab should be on the right .
When I use setCurrentItem , the Fragment_contacts is not loading and it's not showing any thing , but when I remove setCurrentItem , it loads perfectly.
what is the problem ? how can I solve this ?
Your ViewPager will take some time at initialization before you could use setCurrentItem. So set it after the intialization of your ViewPager by calling it in a thread, like this:
viewPager.post(new Runnable() {
#Override
public void run() {
viewPager.setCurrentItem(2);
}
});
Instead of keeping reference to your fragment in your activity, try to do the same in your ViewPager
The view pager is probably already initialized when you add your item in the list and there is a kind of "synchronization" issue between the two.
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager, Bundle bundle) {
super(manager);
Fragment_contacts contact=new Fragment_contacts();
contact.setArguments(bundle);
Fragment_info info=new Fragment_info();
info.setArguments(bundle);
Fragment_Menu menu=new Fragment_Menu() ;
menu.setArguments(bundle);
adapter.addFragment(contact, "تماس با ما");
adapter.addFragment(info, "درباره ما");
adapter.addFragment(menu, "منو");
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I am using ViewPager,
I have to display more data in two fragments, but it does not show anything.
Is There an alternative layout to do it?
this is layout code:
<android.support.v4.view.ViewPager
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.8">
</android.support.v4.view.ViewPager>
this is adapter:
public class HitoeAdapter extends FragmentPagerAdapter {
private static HitoeAdapter adapter;
private final HeartRateFragment mHeartRateFragment;
private final ECGFragment mECGFragment;
public HitoeAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
mHeartRateFragment = new HeartRateFragment();
mECGFragment = new ECGFragment();
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return mHeartRateFragment;
case 1:
return mECGFragment;
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
}
Please help me.
Thanks.
Create the adapter class like below--
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Now add this method to set your fragments with viewpager -
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HeartRateFragment(), "HeartRateFrag");
adapter.addFragment(new ECGFragment(), "ECGFragment");
viewPager.setAdapter(adapter);
}
I have Three different Fragments to show using View Pager. I have added the fragment to the adapter as follows. Title shown is correct but on click its picking the wrong fragment
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new AcceptsFragment(), "Accepts");
adapter.addFragment(new AskedFragment(), "Asked");
adapter.addFragment(new HelpedFragment(), "Helped");
viewPager.setAdapter(adapter);
}
Here is my adapter implementation.
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
Log.d("Fragment Clicked ",String.valueOf(position));
return mFragmentList.get(position);
}
#Override
public int getCount() {
//return mFragmentList.size();
return mFragmentist.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}