ViewPagerFragment Tab Error - android

i create a viewPager and is work , my problem is for select a page i just can drag page but I want to click on the tabs to select the pages . now i add some new code but i cant find this error
my code
public class MainActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
Pager adapter = new Pager(getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnAdapterChangeListener( new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
TabLayout.OnTabSelectedListener tabListener = 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) {
}
};

You cant use like viewPager.addOnAdapterChangeListener( new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); Because addOnAdapterChangeListener() method expect OnAdapterChangeListener as paramenter. You should use void addOnPageChangeListener (ViewPager.OnPageChangeListener listener) on viewPager instead.It has several callback method, So you can detect the event when page of viewPager is changed.
Reference

Related

Android Studio:Null Pointer Excecption occured while working on TabLayout

This is my code and the error that occured
I am a beginner and was trying to work on the tab layout. The app didn't give any error while compiling but crashes midway. My main activity code is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabLayout tabLayout= findViewById(R.id.tab_layout);
TabItem t= findViewById(R.id.t1);
final ViewPager viewPager= findViewById(R.id.pager);
new PageAdapter(getSupportFragmentManager(),4);
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) {
}
});
}
Please help me as I have to submit this as my assignment.
You need to make the declaration outside the oncreate function to make it global and seen by other methods, then do the initialization inside like this :
TabLayout tabLayout;
TabItem t;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabLayout= findViewById(R.id.tab_layout);
t= findViewById(R.id.t1);
viewPager= findViewById(R.id.pager);
new PageAdapter(getSupportFragmentManager(),4);

How to Disable Snackbar on Tab change in TabLayout

When application is started I am showing Snackbar if Internet connection is not available in my TabLayout Fragments.I have added Retry action to Snackbar to check for Internet.If Retry is pressed and Internet connection gets available Snackbar is dismiss but if internet connection is available and I switch the tabs the Snackbar is not dismissed. Again I need to press Retry Action button and then Snackbar is dismiss.How to resolve this ?
This is code for checkInternet
public void checkInternet(){
String NetworkStatus = biz.fyra.bookapp.utils.NetworkStatus.checkConnection(getContext());
if (NetworkStatus.equals("false")) {
snackbar=Snackbar.make(recyclerView,"You are Offline.",
Snackbar.LENGTH_INDEFINITE).setAction("Retry", new View.OnClickListener() {
#Override
public void onClick(View v) {
checkInternet();
}
});
snackbar.show();
}else{
getData();
}
}
This is code to display my TabLayout Fragments
public class ChooseTab extends Fragment {
// Tab Layout and ViewPager
TabLayout tabLayout;
ViewPager viewPager;
public ChooseTab() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.choose_tab, container, false);
int position=getArguments().getInt("TARGET_FRAGMENT_ID");
tabLayout = (TabLayout) v.findViewById(R.id.simpleTabLayout);
tabLayout.addTab(tabLayout.newTab().setText("QUEUE"));
tabLayout.addTab(tabLayout.newTab().setText("TABLES"));
tabLayout.addTab(tabLayout.newTab().setText("Reservation"));
viewPager = (ViewPager) v.findViewById(R.id.simpleViewPager);
viewPager.setOffscreenPageLimit(3);
final PagerAdapter adapter = new PagerAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setCurrentItem(position);
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) {
viewPager.setCurrentItem(tab.getPosition());
adapter.notifyDataSetChanged();
}
});
return v;
}
}
How to disable Snackbar if Tab is changed and Internet is already available ?
This is because the fragment is called when ViewPager has finished its setup, i.e adding the fragments from its adapter and displaying the fragments.
So you need to show Snackbar if only the fragment is visible or vice versa.
if you use support library for fragments, then you can use getUserVisibleHint() or override setUserVisibleHint() to check for fragment visibility.
You can do the following:
public class MyFragment extends Fragment {
...
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// show snake bar here.
} else {
}
}
...
}

start activity with fragment

I want to start activity with three fragments
Intent intent = new Intent(NewMealActivity.this, MainActivity.class);
intent.putExtra("id", 1);
startActivity(intent);
MainActivity includs three fragments (Frag1, Frag2, Frag3).
I want to open Frag3, but opens Frag1
This is my MainActivity with three tabs
public class MainActivity extends FragmentActivity {
private String txtFragmentMyData;
private String txtFragmentProductList;
private String txtFragmentCalorieCalculator;
private TabLayout tabLayout;
private long tabId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtFragmentMyData = getString(R.string.txt_fragment_my_data);
txtFragmentProductList = getString(R.string.txt_fragment_product_list);
txtFragmentCalorieCalculator = getString(R.string.txt_fragment_calorie_calculator);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentMyData));
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentProductList));
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentCalorieCalculator));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
Bundle extras = getIntent().getExtras();
if (extras != null) {
tabId = extras.getLong("id");
}
if (tabId > 0){
viewPager.setCurrentItem(2);
} else {
viewPager.setCurrentItem(0);
}
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) {
}
});
}
}
I want, that at the first start opens first tab, but if I click on buttom in another activity opens third tab

viewPager does not respond to tab click

this is my Activity
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout tablayout;
private int[] tableIcons = {android.R.drawable.ic_menu_camera,
android.R.drawable.ic_menu_add, android.R.drawable.ic_media_next};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tablayout = (TabLayout) findViewById(R.id.tab);
setUpwithviewpager(viewPager);
tablayout.setupWithViewPager(viewPager);
tablayout.getTabAt(0).setIcon(tableIcons[0]);
tablayout.getTabAt(1).setIcon(tableIcons[1]);
tablayout.getTabAt(2).setIcon(tableIcons[2]);
tablayout.setTabMode(TabLayout.MODE_FIXED);
}
public void setUpwithviewpager(ViewPager viewpager) {
ViewPageradapter adapter = new ViewPageradapter(getSupportFragmentManager());
adapter.addFragment(OneFragment.newInstance("1"), "one");
adapter.addFragment(TwoFragment.newInstance("2"), "two");
adapter.addFragment(ThreeFragment.newInstance("3"), "three");
viewpager.setAdapter(adapter);
}
}
this the layout xml file
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"></android.support.v4.view.ViewPager></android.support.design.widget.CoordinatorLayout>
My tablayout does not respond when I change the viewpager, and the viewpager also does not respond when I click on the tabs. I do not know why, I can't find a solution to solve this problem.
Try with below code :
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) {
}
});
You need to add a tab listener to the tab layout, and an onPageChangeListener to the viewPager. This is now usually done with ActionBar.TabListener and ViewPager.OnPageChangeListener. Please see the Android article on Creating Swipe Views with Tabs.
You need to have something like
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// When the tab is selected, switch to the
// corresponding page in the ViewPager.
viewpager.setCurrentItem(tab.getPosition());
}
and
viewpager = (ViewPager) findViewById(R.id.pager);
viewpager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
getActionBar().setSelectedNavigationItem(position);
}
});
This issue also affected me for hours. What worked for me was making my ViewPagerAdapter extend FragmentStatePagerAdapter instead of FragmentPagerAdapter. I hope this helps.

android: how to refresh a tab after button click

I have page with a button and tabs below. I want the tab which is opened to reflect the change in variable data after i click the button. I am not able to understand it.
The following is my code:-
public class Level2MainActivity_grid1 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level2mainlayout_place1);
// I am putting a viewpager with fragments into them and put tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
Button buttonId = (Button) findViewById(R.id.planmytraveludupitown);
buttonId.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I want to save some things and also refresh the current tab
}
});
}
}
Below line also works but incase it don't work then you can try another option also
pagerAdapter.notifydatasetChanged();
Try below code :-
buttonId.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
});
Note:- when you get new data redefine your Titles and Numoftabs again
Couldn't you notify the dataset in your adapter ?
pagerAdapter.notifyDatasetChanged()

Categories

Resources