How to show fragment in android - android

I am using ViewPager to create 3 Fragment on Activity1,
this is my viewPager adapter :
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment1 tab1 = new Fragment1();
return tab1;
case 1:
Fragment2 tab2 = new Fragment2();
return tab2;
case 2:
Fragment3 tab3 = new Fragment3();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
this is Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
TabLayout tab_layout = (TabLayout) findViewById(R.id.tab_layout);
tab_layout.addTab(tab_layout.newTab().setText("Frag1"));
tab_layout.addTab(tab_layout.newTab().setText("Frag2"));
tab_layout.addTab(tab_layout.newTab().setText("Frag3"));
final ViewPager view_pager = (ViewPager) findViewById(R.id.viewpager);
final ViewPagerAdapter adapter = new ViewPagerAdapter
(getSupportFragmentManager(), tab_layout.getTabCount());
view_pager.setAdapter(adapter);
view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));
tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
view_pager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
When Activity1 is open it show Fragment1 . But I want to show Fragment2 or Fragment3 instead .
How I can to do that?

Add this line after viewpager.setAdapter(adapter); :
viewPager.setCurrentItem(index);
Index is depending on you , how many fragments you have. tabLayout.getTabCount(); will return you tab count. But index is starting from 0.
If you have 3 Fragment and want to show third Fragment you must use viewPager.setCurrentItem(2);

Set current position of viewpager to your desired possition which is 2 or 3 in your case.
new Handler().post(new Runnable() {
#Override
public void run() {
mViewPager.setCurrentItem(desiredPos);
}
});
seee more
Android ViewPager setCurrentItem not working after onResume

Related

call view of activity from tab fragment

I have a main activity which has a tab layout in which there are two framgents.Following is the code for same.
public void setViewPager(){
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
chipGroup.removeAllViews();
utils.createChip(getLayoutInflater(),R.layout.chipview,chipGroup,arrayListFilter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
Log.d("hgfdgf","+"+tab.getPosition());
if(tab.getPosition()==0){
fragment=new StudyNotes_fragment();
chipGroup.removeAllViews();
utils.createChip(getLayoutInflater(),R.layout.chipview,chipGroup,arrayListFilter);
//filter_iv_booklet.setVisibility(GONE);
}
else if (tab.getPosition()==1){
fragment2=new Specialbooklet_fragment();
chipGroup.removeAllViews();
utils.createChip(getLayoutInflater(),R.layout.chipview,chipGroup,arrayListFilter2);
//filter_iv_booklet.setVisibility(VISIBLE);
}
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onClick(View v) {
}
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
for (int i=0;i<=studyM.size()-1;i++){
arrayListFilter.add(studyM.get(i).get(i).getCSubjectName());
}
LayoutInflater li = LayoutInflater.from(context);
HashSet<String> hashSet = new HashSet<String>();
hashSet.addAll(arrayListFilter);
arrayListFilter.clear();
arrayListFilter.addAll(hashSet);
Bundle args = new Bundle();
args.putStringArrayList("array2",arrayListFilter);
args.putSerializable("array4",studyM);
args.putSerializable("tag",makeFragmentName(viewPager.getId(), 1));
fragment.setArguments(args);
return fragment;
case 1:
for (int i=0;i<=specialB.size()-1;i++){
arrayListFilter2.add(specialB.get(i).get(i).getCSubjectName());
}
LayoutInflater li1 = LayoutInflater.from(context);
HashSet<String> hashSet1 = new HashSet<String>();
hashSet1.addAll(arrayListFilter2);
arrayListFilter2.clear();
arrayListFilter2.addAll(hashSet1);
String fragmentTag2 = makeFragmentName(viewPager.getId(), 2);
Bundle args1 = new Bundle();
args1.putSerializable("array", specialB);
args1.putStringArrayList("array2",arrayListFilter2);
args1.putStringArrayList("array3",videosubtitlelist);
args1.putString("tag",fragmentTag2);
fragment2.setArguments(args1);
return fragment2;
//return Specialbooklet_fragment.newInstance(specialB,arrayListFilter2,videosubtitlelist,fragmentTag2);
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
I am using a button which is in toolbar of activity in fragment.I am accessing it in fragment by using getactivity. Below is my code.
rl_filter = activity.findViewById(R.id.rl_filter);
filter_iv = activity.findViewById(R.id.filter_iv);
chipGroup = activity.findViewById(R.id.chipgroup);
applyfilter_tv = activity.findViewById(R.id.applyfilter_tv);
clearfilter_tv = activity.findViewById(R.id.clearfilter_tv);
filter_iv.setOnClickListener(this::onClick);
rl_filter.setOnClickListener(this::onClick);
clearfilter_tv.setOnClickListener(this::onClick);
applyfilter_tv.setOnClickListener(this::onClick);
I am doing same thing in second fragment.My problem is that clicks of only second framgent is working when i am defining the views in both fragments. If i am removing listener and initialisation of buttons from second fragment then first fragment clicks are working. Is there any way i can use the view of activity in both the fragments.

TabLayout Preload next tab

I have a tablayout (tab1, tab2, tab3) with a ViewPager. It seems that on app startup the tab2 is preloaded which is fine.
However, I need to input some data on tab1 and I want the tab2 to update its fragment and hide some buttons based on the data input from tab1.
How to do that? Is there a way to not preload the tab so that when I swipe to tab2, it will be getting the latest data instead of the old data?
Here's my code
private ViewPager viewPager;
private PagerAdapter adapter;
private TabLayout tabLayout;
in my onCreate method within an activity.
tabLayout.addTab(tabLayout.newTab().setText("Input"));
tabLayout.addTab(tabLayout.newTab().setText("Detail"));
tabLayout.addTab(tabLayout.newTab().setText("Chart"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(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) {
}
});
Here's my pageadapater class
public class PagerAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
private InputFragment inputFragment;
private DetailFragment detailFragment;
private ChartFragment chartFragment;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
inputFragment = new InputFragment();
return inputFragment;
case 1:
detailFragment = new detailFragment();
return detailFragment;
case 2:
chartFragment = new ChartFragment();
return chartFragment;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
on your detailfragment fragment
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//check values on tab 1 and try to update your tab 2 views accordingly
}
}

How to update-refresh Fragment in ViewPager on Tablayout

I have a tablayout (tab1, tab2, tab3) with a ViewPager. I want to input something in tab1, and depends on the input, tab2 and tab3 will show data accordingly.
It seems every time the app start, tab1 (fragment) and tab2 (fragment) are loaded with correct data. I'm using static variable to pass data from tab1 to tab2. However, after I change some data in tab1, tab2 doesn't update itself base on the data from tab1, it shows the old data. Here are my codes
private ViewPager viewPager;
private PagerAdapter adapter;
private TabLayout tabLayout;
in my onCreate method within an activity.
tabLayout.addTab(tabLayout.newTab().setText("Input"));
tabLayout.addTab(tabLayout.newTab().setText("Detail"));
tabLayout.addTab(tabLayout.newTab().setText("Chart"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(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) {
}
});
Here's my pageadapater class
public class PagerAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
private InputFragment inputFragment;
private DetailFragment detailFragment;
private ChartFragment chartFragment;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
inputFragment = new InputFragment();
return inputFragment;
case 1:
detailFragment = new detailFragment();
return detailFragment;
case 2:
chartFragment = new ChartFragment();
return chartFragment;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}

Tablayout tab with multiple fragment

I am working on an app which use tab layout with 5 tabs. My need is to navigate multiple fragment inside every tab, and open the first fragment when user again reselected the tab. Please help me.
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.scene_tab_selector));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.my_scene_tab_selector));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.conversation_tab_selector));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.notification_tab_selector));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.more_tab_selector));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdaptor adapter = new PagerAdaptor
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
if(tab.getPosition()==4)
{
// pageTitleLabel.setText("My Profile");
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
My PagerAdaptor is as follows
public class PagerAdaptor extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdaptor(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1ContainerFragment tab1 = new Tab1ContainerFragment();
return tab1;
case 1:
MyCuddllFragment tab2 = new MyCuddllFragment();
return tab2;
case 2:
// CuddllConversationFragment tab3 = new CuddllConversationFragment();
ConversationFragment conversationFragment = new ConversationFragment();
return conversationFragment;
case 3:
// CuddllNotificationsFragment tab4 = new CuddllNotificationsFragment();
Tab4ContainerFragment notificationPageFragment = new Tab4ContainerFragment();
return notificationPageFragment;
case 4:
MyProfileFragment tab5 = new MyProfileFragment();
return tab5;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
public void onTabSelected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
if(tab.getPosition()==0)
{
viewpager.setCurrentItem(0);
}else if(tab.getPosition()==1)
{
viewpager.setCurrentItem(1);
}else if(tab.getPosition()==2)
{
viewpager.setCurrentItem(2);
}else if(tab.getPosition()==3)
{
viewpager.setCurrentItem(3);
}
}

properly remove fragments from FragmentStatePagerAdapter

hey guys i implemented a tab layout(an action bar with tabs) , basically it is made of using pagerAdapter and FragmentStatePagerAdapter below is my code :
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
case 3:
TabFragment4 tab4 = new TabFragment4();
return tab4;
case 4:
TabFragment5 tab5 = new TabFragment5();
return tab5;
default:
return null;
}
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
super.destroyItem(container, position, object);
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
}
#Override
public int getCount() {
return mNumOfTabs;
}}
my mainActivty:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.addTab(tabLayout.newTab().setText("Tab4"));
tabLayout.addTab(tabLayout.newTab().setText("Tab5"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(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) {
}
});
now i want to remove the tabs(fragments) and for that am just removing them from here
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.addTab(tabLayout.newTab().setText("Tab4"));
if (condition == true){ // do nothing which means don't add the fragment }
else {
tabLayout.addTab(tabLayout.newTab().setText("Tab5")); }
and is working but not as it supposed to , even after removing the the tab from here tabLayout.addTab(tabLayout.newTab().setText("Tab5")); the fragments are still there and actively performing their task , i want to shut them completely , am new in android so have no idea about this that its a good approach or not guys is it enough or not please let me know any guidance will be so helpful for me

Categories

Resources