I made one GridView with DotsPageIndicator.
private GridViewPager pager;
private DotsPageIndicator dotsPageIndicator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teste);
pager = (GridViewPager) findViewById(R.id.pager);
pager.setAdapter(new GridPagerAdapter(getFragmentManager()));
dotsPageIndicator = (DotsPageIndicator) findViewById(R.id.page_indicator);
dotsPageIndicator.setDotFadeWhenIdle(false);
dotsPageIndicator.setPager(pager);
}
#Override
public void onResume() {
super.onResume();
pager.getAdapter().notifyDataSetChanged();
}
When I use notifyDataSetChanged() the position of dots is lost and it returns to the first. How can I change dots position programmatically?
Related
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);
I have a main activity with three tabs, and the three tabs are being populated with a recycler view while fetch data using a JSONHttp Request with Volley. However, the problem is that the first time the data is fetched, it get's populated. However, I am unable to find a way to fetch data again (like refresh it) when I switch from one tab to another.
A snippet of what I am doing is below:
public class MainActivity extends AppCompatActivity {
// Class name TAG to be used for logging
private static final String TAG = MainActivity.class.getSimpleName();
// Variables
private User user;
private static ArrayList<> items;
private Fragment Fragment1, Fragment2;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
user = getIntent().getParcelableExtra("USER");
fetchItems();
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position) {
Log.d("Main", "Switching tab");
// Update the view
super.onPageSelected(position);
}
});
}
public void fetchJobs(){
items = new ArrayList<>();
JsonArrayRequest getItems = new JsonArrayRequest(Request.Method.GET, url + "items.json?", null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray o) {
// Get JSon from Http request, this code works fine..
// items array is populated with the desired response
mSectionsPagerAdapter.notifyDataSetChanged();
....
}
If I call fetchItems() when I switch to a tab to refresh the list it does not get repopulated with the new entries.
Try Using
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//Do what you want to do here
}
}
in each child fragment of viewPager,so each time the fragment is shown in viewpager it will get called. and can make the necessary changes. thanks if it doesn't wokr please note down.
how can I save and return the tab position when user press the Back button?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ViewPager vpPager = (ViewPager) findViewById(R.id.viewpager);
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);
// Give the TabLayout the ViewPager
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setupWithViewPager(vpPager);
// Attach the page change listener inside the activity
vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(POSITION, tabLayout.getSelectedTabPosition());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
viewPager.setCurrentItem(savedInstanceState.getInt(POSITION));
}
Do I use the onBackPressed() to do this?
Help is much appreciated. Thanks!
You can do it with back press in the following way:
#Override
public void onBackPressed() {
int pos = mViewPager.getCurrentItem();
}
and when you back in screen you can set position of pager
#Override
public void onResume(){
super.onResume();
// put your code here...
if(mViewPager !=null)
mViewPager.setCurrentItem(pos);
}
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()
I'm using the v7 widget Toolbar and setting it as my support action bar in my AppCompatAcivity. When I'm on a different tab and start a new activity, I can't restore the last tab I was on with onSaveInstanceState. onCreate's savedInstanceState would be null and onRestoreInstanceState is never called when coming from activity 2 to my activity with tabs. How can I solve this?
public class ContactsActivity extends AppCompatActivity {
private static String TAB_POSITION = "POSITION";
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.logo_spokeo);
viewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new RecentContactsFragment(), "Recent Contacts");
adapter.addFragment(new AddressBookFragment(), "Address Book");
adapter.addFragment(new RecentContactsFragment(), "Settings");
viewPager.setAdapter(adapter);
if (savedInstanceState != null) {
viewPager.setCurrentItem(savedInstanceState.getInt(TAB_POSITION, 0), false);
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(TAB_POSITION, tabLayout.getSelectedTabPosition());
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
viewPager.setCurrentItem(savedInstanceState.getInt(TAB_POSITION), true);
}
}