I have problems with android tabs, when I loaded first time it loads perfectly, but when I go to third tab and come back to again first tab, then all data get disappeared?
This is my tabbed fragment.
package com.autoxtion.autoconnect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.FrameLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by NKCSL0015 on 9/23/2016.
*/
public class HomeActivity extends Fragment {
View root;
FrameLayout simpleFrameLayout;
private View categoryView;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.heart,
R.drawable.user,
R.drawable.heart
};
public HomeActivity() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
root = inflater.inflate(R.layout.home_content, null);
// toolbar = (Toolbar) root.findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) root.findViewById(R.id.viewpager);
setupViewPager(viewPager);
//viewPager.setCurrentItem(2, false);
tabLayout = (TabLayout) root.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
return root;
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null);
tabOne.setText("Vehicle");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.car, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null);
tabTwo.setText("Promotion");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.promotiontab1, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView tabThree = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null);
tabThree.setText("Request");
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.request_tab, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
TextView tabFour = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null);
tabFour.setText("Appointment");
tabFour.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.appointment, 0, 0);
tabLayout.getTabAt(3).setCustomView(tabFour);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(this.getChildFragmentManager());
adapter.addFrag(new VehicleFragmentTab(), "Vehicle");
adapter.addFrag(new PromotionFragment(), "Promotion");
adapter.addFrag(new RequestFragment(), "Request");
adapter.addFrag(new AppointmentFragment(), "Appointment");
viewPager.setAdapter(adapter);
if(RequestEditActivity.requestEditFlag==1){
viewPager.setCurrentItem(2);
}
if(AvailActivity.addpromotionrequest==1){
viewPager.setCurrentItem(2);
}
if(RequestAdapter.deleteflag==1){
RequestAdapter.deleteflag = 0;
viewPager.setCurrentItem(2);
}
if(VehcleAdapter.deleteVehicle == 1){
VehcleAdapter.deleteVehicle = 0;
viewPager.setCurrentItem(0);
}
/* if(CreateRequest.addrequestflag==1){
viewPager.setCurrentItem(2);
}*/
if(CreateRequest.successfullyadd==1){
viewPager.setCurrentItem(2);
}
if(VehicleDetails.updatesuccessfully==1){
viewPager.setCurrentItem(0);
}
if(AddVehicle.successfullyaddvehicle==1){
viewPager.setCurrentItem(0);
}
}
#Override
public void onResume() {
super.onResume();
for (Fragment fragment : getFragmentManager().getFragments()) {
if (fragment instanceof VehicleFragmentTab || fragment instanceof PromotionFragment || fragment instanceof RequestFragment || fragment instanceof AppointmentFragment) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(fragment);
ft.attach(fragment);
ft.commit();
}
}
}
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 addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Related
In my program, I have 2 implemented tabs using fragments. I've placed an EditText inside Tab2. I want to write all my main code for Tab2 inside onCreateView() of fragment Tab2, so that every time I swipe over to Tab2, this same code is executed. To test whether onCreateView() was being called every time I swiped to Tab2, I wrote the following statement in onCreateView() of Tab2:
text.setText("default");
Now when I run the app and swipe to Tab2- I see 'default' written. Now if I change this text manually, then swipe over to Tab1 and come back to Tab2- I still see this same text written. This obviously means that onCreateView() was not called again when I swiped to Tab2 for the 2nd time? How do I fix this?
Tab2.java
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Tab2 extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
setRetainInstance(false);
final View rootView;
rootView= inflater.inflate(R.layout.fragment_tab2, container, false);
text.setText("default");
return rootView;
}
}
MainActivity.java
package com.example.nirvan.diary30;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
public static int itemPosition;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(0);
}
private void setupViewPager(ViewPager viewPager)
{
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1(), "ONE");
adapter.addFragment(new Tab2(), "TWO");
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);
}
}
}
If you are using ViewPager
ViewPager viewPager;
Try to use setOnPageChangeListenerin MainActivity
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(mFragmentList.size() > position && mFragmentList.get(position) instanceof Tab2)
{
((Tab2)mFragmentList.get(position)).changeText("changed text")
}
// you can use interface insted
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
in Tab2
public void changeText(String string){
text.setText(string);
}
Update MainActivity.java
public class MainActivity extends AppCompatActivity
{
public static int itemPosition;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
ViewPagerAdapter adapter;
private final List<Fragment> mFragmentLists = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(0);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(mFragmentList.size() > position && mFragmentList.get(position) instanceof Tab2)
{
((Tab2)mFragmentList.get(position)).changeText("changed text")
}
// you can use interface insted
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setupViewPager(ViewPager viewPager)
{
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1(), "ONE");
adapter.addFragment(new Tab2(), "TWO");
viewPager.setAdapter(adapter);
mFragmentLists = adapter.getFragements();
}
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 List<Fragment> getFragements()
{
return mFragmentList;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
how to switch between Fragment A and Fragment B in android TabLayout.
how to switch to next Fragment when Fargment Button Onclick is called.
Please help me
package com.digi.global.globalhub.fragment.offer;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.digi.global.globalhub.R;
import com.digi.global.globalhub.Utility.M;
import com.digi.global.globalhub.listener.FilterListener;
import com.digi.global.globalhub.listener.FragmentMove;
import com.digi.global.globalhub.listener.OfferFilter;
public class OfferFragmentActivity extends Fragment implements View.OnClickListener{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
PagerAdapter adapter;
ViewPager viewPager;
TabLayout tabLayout;
Context context;
Toolbar toolbar;
ImageView bizMenu;
PopupMenu popup;
public static OfferFragmentActivity mInstance = null;
public OfferFragmentActivity() {
// Required empty public constructor
}
public static OfferFragmentActivity newInstance(String param1, String param2) {
OfferFragmentActivity fragment = new OfferFragmentActivity();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
context = this.getContext();
mInstance = this;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view =inflater.inflate(R.layout.fragment_offer, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
adapter = new PagerAdapter(getChildFragmentManager(), getContext());
tabLayout = (TabLayout) view.findViewById(R.id.tablayout);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
bizMenu = (ImageView) view.findViewById(R.id.bizMenu);
bizMenu.setVisibility(View.VISIBLE);
bizMenu.setOnClickListener(this);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
//toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.biz_ic, android.R.style.Theme_DeviceDefault));
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final Drawable upArrow = getResources().getDrawable(R.mipmap.ic_menu_white_36dp);
upArrow.setColorFilter(Color.parseColor("#B68426"), PorterDuff.Mode.SRC_ATOP);
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeAsUpIndicator(upArrow);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(getString(R.string.Offers));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(final TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
#Override
public void onClick(View v) {
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Fragment fragment = (Fragment) adapter.instantiateItem(viewPager, viewPager.getCurrentItem());
if(fragment instanceof OfferFilter){
switch (item.getItemId()) {
case R.id.bizTravel:
((OfferFilter) fragment).onFilterBizTravel("bizTravel");
return true;
case R.id.bizAssistance:
((OfferFilter) fragment).onFilterAssistance("bizAssistance");
return true;
case R.id.bizSolution:
((OfferFilter) fragment).onFilterSolution("bizSolution");
return true;
case R.id.partners:
((OfferFilter) fragment).onFilterPartner("partners");
return true;
case R.id.favoutites:
((OfferFilter) fragment).onFilterFavouritesBiz("favoutites");
return true;
}
return false;
}
return super.onOptionsItemSelected(item);
}
class PagerAdapter extends FragmentStatePagerAdapter {
String tabTitles[] = getResources().getStringArray(R.array.offerTab);
Context context;
public PagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return tabTitles.length;
}
#Override
public Fragment getItem(int position) {
/* FragmentMove fragmentChanges = (FragmentMove) adapter.instantiateItem(viewPager, 2);
if(fragmentChanges instanceof FragmentMove) {
((FragmentMove) fragmentChanges).fragmentChanges();
}*/
switch (position) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTWO();
case 2:
return new FragmentThree();
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
/* public View getTabView(int position) {
View tab = LayoutInflater.from(getContext()).inflate(R.layout.tab_text_layout, null);
TextView tv = (TextView) tab.findViewById(R.id.tabText);
tv.setText(tabTitles[position]);
return tab;
}*/
}
}
You can have a FragmentPagerAdapter to manager fragments and set this adapter into viewPager to slide fragments when you click on tabs or swipe to change tabs.
FragmentPagerAdapter:
public class TabLayoutViewPagerAdapter extends FragmentPagerAdapter {
private final int PAGE_COUNT = 3;
private final String[] mTabsTitle = {"Fragment A", "Fragment B, "Fragment C"};
private Context context;
private int[] mTabsIcons = {
R.drawable.a,
R.drawable.b,
R.drawable.c};
public TabLayoutViewPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
public View getTabView(int position) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
TextView title = (TextView) view.findViewById(R.id.title);
title.setText(mTabsTitle[position]);
title.setSingleLine(true);
ImageView icon = (ImageView) view.findViewById(R.id.icon);
icon.setImageResource(mTabsIcons[position]);
return view;
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
}
return null;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public CharSequence getPageTitle(int position) {
return mTabsTitle[position];
}}
How you use it:
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
TabLayoutViewPagerAdapter pagerAdapter = new TabLayoutViewPagerAdapter(getSupportFragmentManager(), this);
if (viewPager != null) {
viewPager.setAdapter(pagerAdapter);
}
You need a TabLayout:
mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
if (mTabLayout != null) {
mTabLayout.setupWithViewPager(viewPager);
//populate your tabs
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
TabLayout.Tab tab = mTabLayout.getTabAt(i);
if (tab != null)
tab.setCustomView(pagerAdapter.getTabView(i));
}
TabLayout.Tab tab = mTabLayout.getTabAt(0);
if (tab != null && tab.getCustomView() != null) {
tab.getCustomView().setSelected(true);
}
}
How to select a page:
private void selectPage(int pageIndex) {
mTabLayout.setScrollPosition(pageIndex, 0f, true);
viewPager.setCurrentItem(pageIndex);
}
Do not forget to create your customTab.xml.
Best regards.
In Fragment A's onClick() method, try this :
TabActivity tabs = (TabActivity) getParent();
tabs.getTabHost().setCurrentTab(2);
package com.agri.kishan;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
import android.widget.Toast;
import com.agri.kishan.OneFragment;
import java.util.ArrayList;
import java.util.List;
import com.agri.kishan.OneFragment;
public class ScrollableTabsActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
public static int tabsele;
public static String realqus="this is a sample qus";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollable_tabs);
//
//
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
Toast.makeText(getApplicationContext(),tab.getPosition()+"",Toast.LENGTH_SHORT).show();
tabsele=tab.getPosition();
/////////////////////////////////////////
/////////////////////////////////
///////////////////
I want to set text according to the position,I get position successfully
if(tabsele==0)
{
OneFragment.tv.setText("hai");
} else if(tabsele==1)
{
OneFragment.tv.setText("bye");
}
//////////////////
////////////////////////////////
///////////////////////////////////////////
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
for(int i=0;i<=Card_display.counttabs;i++) {
adapter.addFrag(new OneFragment(), "Qus:"+i);
}
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 addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Fragment page
the fragment page is a simple fragment page which has an text view in it,
i get the postion of the tab from activity
i want to change the text to the textview in the fragment according to the position from the activity
Fragement page
package com.agri.kishan;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.agri.kishan.ScrollableTabsActivity;
public class OneFragment extends Fragment {
/////////////////////////
////////////////////////////
//////////////////
public static TextView tv;
////////////////////////
////////////////////////
/////////////////////
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one,
container, false);
tv=(TextView)view.findViewById(R.id.realqus);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
Try this approach
Inside your onTabSelected
OneFragment page = (OneFragment)viewPager.getAdapter().getItem(tab.getPosition());
page.tv.setText("Hi");
I am working on viewPager with fragment and it was working all things perfect before I have start implementing runtime permission for different module.
Here is my viewPager Adapter.
public class DashboardAdapter extends FragmentStatePagerAdapter {
public DashboardAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FunkyInboxFragment.newInstance(position);
case 1:
return FunkyTakePhotoFragment.newInstance(position);
case 2:
return FunkyAddFriendsFragmnet.newInstance(position);
default:
return FunkyTakePhotoFragment.newInstance(position);
}
}
#Override
public int getCount() {
return 3;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
}
Here different fragments required different permissions so I have made a common activity class for it. It's working in the same fragment but after asking for a permission when I swipe the viewPager the fragment after next fragment and a fragment before previous fragment get blank. i have made all possible change in adapter but did not get a proper outcome.
Please can anyone help with it!
#Override
public int getCount() {
return 2;
}
//try this may be it work
package com.example.madhavgor.actionbardemolast;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.mipmap.ic_launcher,R.mipmap.ic_launcher_bluetooth
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewPager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new SecondFragment(), "BLUETOOTH");
adapter.addFragment(new ThirdFragment(), "THREE");
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);
}
}
}
//refer this code ....may be it will help you..
In my app I want to use swipe views of four tabs inside a fragment. The four tabs are contains different fragments each and all the four fragments are sliding by swipe from right to left or vice versa. The fragments are working fine but the tabs are not visible within the fragment. Anyone have any solution for this. Thanks in advance :)
this is the main fragment which contains the tabs:-
public class DashboardTabFragment extends Fragment implements ActionBar.TabListener {
private static final String ARG_SECTION_NUMBER = "arg_section_number";
private String[] tabTitle = {"Cleanness", "Product Display", "Hygiene", "Asm Visits"};
public static DashboardTabFragment newInstance(int position) {
DashboardTabFragment fragment = new DashboardTabFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, position);
fragment.setArguments(args);
return fragment;
}
private ViewPager viewPager;
public DashboardTabFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dashboard_tab, container, false);
setHasOptionsMenu(true);
ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
assert actionBar != null;
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
TabPageAdapter tabPageAdapter = new TabPageAdapter(getActivity().getSupportFragmentManager(), getActivity());
viewPager.setAdapter(tabPageAdapter);
for (String aTabTitle : tabTitle)
actionBar.addTab(actionBar.newTab().setText(aTabTitle).setTabListener(this));
return rootView;
}
This is the adapter for fragments:-
public class TabPageAdapter extends FragmentPagerAdapter {
Context context;
public TabPageAdapter(FragmentManager fm,Context context) {
super(fm);
this.context = context;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new CleannessChartFragment(context);
case 1:
return new ProductDisplayChartFragment(context);
case 2:
return new HygieneChartFragment(context);
case 3:
return new AsmVisitsChartFragment(context);
}
return null;
}
#Override
public int getCount() {
return 4;
}
I found the solution. I used TabHost for tabs with viewpager.
This is my fragment :-
package com.itpp.trt;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabHost;
public class DashboardTabFragment extends Fragment implements ViewPager.OnPageChangeListener {
private static final String ARG_SECTION_NUMBER = "arg_section_number";
private ViewPager mViewPager;
private TabHost tabHost;
private String[] tabSpec = {"Tab_1", "Tab_2", "Tab_3", "Tab_4"};
private String[] tabTitle = {"Cleanness", "Product Display", "Hygiene", "Asm Visits"};
private TabHost.TabContentFactory mFactory = new TabHost.TabContentFactory() {
#Override
public View createTabContent(String tag) {
View v = new View(getActivity());
v.setMinimumHeight(0);
return v;
}
};
public static DashboardTabFragment newInstance(int position) {
DashboardTabFragment fragment = new DashboardTabFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, position);
fragment.setArguments(args);
return fragment;
}
public DashboardTabFragment() {
tabHost = null;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dashboard_tab, container, false);
mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
mViewPager.setAdapter(new TabPageAdapter(getChildFragmentManager(), getActivity()));
tabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
tabHost.setup();
mViewPager.setOnPageChangeListener(this);
for (int i = 0; i < tabSpec.length; i++) {
tabHost.addTab(tabHost.newTabSpec(tabSpec[i]).setIndicator(tabTitle[i]).setContent(mFactory));
}
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if (tabId.equals("Tab_1")) {
mViewPager.setCurrentItem(0);
} else if (tabId.equals("Tab_2")) {
mViewPager.setCurrentItem(1);
} else if (tabId.equals("Tab_3")) {
mViewPager.setCurrentItem(2);
} else if (tabId.equals("Tab_4")) {
mViewPager.setCurrentItem(3);
}
}
});
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MyActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
tabHost.setCurrentTab(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onDetach() {
super.onDetach();
}
}
This is my tabpager adapter :-
package com.itpp.trt;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
/**
* Created by biswajit on 28-11-14.
*/
public class TabPageAdapter extends FragmentStatePagerAdapter{
Context context;
public TabPageAdapter(FragmentManager fm,Context context) {
super(fm);
this.context = context;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new CleannessChartFragment(context);
case 1:
return new ProductDisplayChartFragment(context);
case 2:
return new HygieneChartFragment(context);
case 3:
return new AsmVisitsChartFragment(context);
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
Hope this helps other. Thanks :)