0

i´m using tablayout and i want to update the tab content when user clicks on the tab. basicly i want to do what the tab does when it is created (check what it is in database and put it on my listview).

here is my MainActivity:

 public class MainActivity extends AppCompatActivity{
        // Declaring Your View and Variables
        Toolbar toolbar;
        ViewPager viewPager;
        ViewPagerAdapter adapter;
        TabLayout tabLayout;
        CharSequence Titles[]={"Participantes","Torneio","Classificação"};
        int Numboftabs =3;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toolbar = (Toolbar) findViewById(R.id.tool_bar);
            viewPager = (ViewPager) findViewById(R.id.viewPager);
            tabLayout = (TabLayout)findViewById(R.id.tabLayout);


            // Creating The Toolbar and setting it as the Toolbar for the activity
            setSupportActionBar(toolbar);
            // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
            adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
            // Assigning ViewPager View and setting the adapter
            viewPager.setAdapter(adapter);


            tabLayout.setupWithViewPager(viewPager);
            tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorTextIcons));
            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    adapter.notifyDataSetChanged();
                }


                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            viewPager = null;
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
            if (id == R.id.action_info) {
                Toast.makeText(getApplicationContext(), "Contacto: Juniortalisson16@gmail.com", Toast.LENGTH_LONG).show();
                return true;
            }
            return super.onOptionsItemSelected(item);
        }


    }

Here is my ViewPageradapter:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
    int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created


    // Build a Constructor and assign the passed Values to appropriate values in the class
    public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
        super(fm);

        this.Titles = mTitles;
        this.NumbOfTabs = mNumbOfTabsumb;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
        FragmentManager manager = ((Fragment) object).getFragmentManager();
        FragmentTransaction trans = manager.beginTransaction();
        trans.remove((Fragment) object);
        trans.commit();
    }

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    //This method return the fragment for the every position in the View Pager
    @Override
    public Fragment getItem(int position) {

        switch(position) {// if the position is 0 we are returning the First tab
            case 0:
            Tab1 tab1 = new Tab1();
            return tab1;
            case 1:
            Tab2 tab2 = new Tab2();
            return tab2;
            case 2:
            Tab3 tab3 = new Tab3();
            return tab3;
            default:
                return null;
        }


    }

    // This method return the titles for the Tabs in the Tab Strip

    @Override
    public CharSequence getPageTitle(int position) {
        return Titles[position];
    }

    // This method return the Number of tabs for the tabs Strip

    @Override
    public int getCount() {
        return NumbOfTabs;
    }
}

i tried to use the getItemPosition but it didn´t work, my app crash when i select an tab.

Any ideias? please

Talisson Junior
  • 79
  • 2
  • 11
  • Just make a public method in your Fragment that updates the adapter data, see here: [Refresh data in ViewPager Fragment](http://stackoverflow.com/questions/36503779/refresh-data-in-viewpager-fragment) – Daniel Nugent Apr 26 '16 at 03:23
  • If I do understand your code Tab1, Tab2 and Tab3 are fragments. If so you can use the onResume method in each of those fragments to "refresh" the contents. – Gerard Frijters Apr 26 '16 at 03:25
  • @geraldo that won't work with a ViewPager, as it keeps the Fragments on either side of the current one alive. – Daniel Nugent Apr 26 '16 at 03:36
  • @Daniel, but suppose he is filling his ListViews with a CursorLoader then he can call restartLoader in the onResume, and adapter.swapCursor in the onLoadFinished which will refresh the contents of his ListView. – Gerard Frijters Apr 26 '16 at 03:55
  • @geraldo for example, say you have two tabs. You can swipe back and forth between tabs, and onResume() will never be called, since the FragmentPagerAdapter is keeping both Fragments alive. – Daniel Nugent Apr 26 '16 at 04:20
  • yeah, i tried onResume() Geraldo Freitas, and has Daniel said it didn´t work out, i will try to use a public method Daniel. thanks – Talisson Junior Apr 26 '16 at 11:56

0 Answers0