cham3333
cham3333

Reputation: 103

How to handle listview event while using new android sliding tabs technique?

I tried searching internet but I found all tutorials to be using old tabhost technique. I am using new toolbar and adding tabs under it. I want to display contents fetched from database in listview dynamically when that particular tab is focused. Following is my Activity code: [EDITED]

 public class PostStory extends ActionBarActivity implements ViewPager.OnPageChangeListener   {



        // Declaring Your View and Variables



        Toolbar toolbar;

        ViewPager pager;

        ViewPagerAdapter adapter;

        SlidingTabLayout tabs;

        CharSequence Titles[]={"Explore","Publish","Profile"};

        int Numboftabs =3;



        @Override

        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_post_story);



            // Creating The Toolbar and setting it as the Toolbar for the activity



            toolbar = (Toolbar) findViewById(R.id.tool_bar);

           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

            pager = (ViewPager) findViewById(R.id.pager);

            pager.setAdapter(adapter);

            pager.setOnPageChangeListener(this);

            // Assiging the Sliding Tab Layout View

            tabs = (SlidingTabLayout) findViewById(R.id.tabs);

            tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width



            // Setting Custom Color for the Scroll bar indicator of the Tab View

            tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {

                @Override

                public int getIndicatorColor(int position) {

                    return getResources().getColor(R.color.tabsScrollColor);

                }

            });



            // Setting the ViewPager For the SlidingTabsLayout

            tabs.setViewPager(pager);


            Log.d("tabsid ",Integer.toString(tabs.getId()));




        }





        @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_main, 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;

            }



            return super.onOptionsItemSelected(item);

        }
        @Override
        public void onPageSelected(int position) {
          //  if(position == ?) fetchAndSetData(); // Here fetch and set the data
            Log.d("pos ",Integer.toString(position));
            Toast.makeText(this,"page changed",Toast.LENGTH_LONG);
        }

        @Override
        public void onPageScrollStateChanged(int state) {}

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}



    }

This is my tab activity xml code

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context="aru.cramine.studio.aru.PostStory">

     <item android:id="@+id/action_publish" android:title="@string/Publish"
        android:orderInCategory="1" app:showAsAction="always" />
    <item android:id="@+id/action_save" android:title="@string/Save"
        android:orderInCategory="2" app:showAsAction="always"

        />


    <item android:id="@+id/action_cancel" android:title="@string/Cancel"
        android:orderInCategory="3" app:showAsAction="ifRoom" />
</menu>

This is my tab.java code public class Tab1 extends Fragment {

    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v =inflater.inflate(R.layout.tab_1,container,false);



        return v;

    }

}

And tab.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </ListView>

</RelativeLayout>

Upvotes: 0

Views: 553

Answers (1)

mrtn
mrtn

Reputation: 889

You can add an OnPageChangeListener and if a specific tab gets selected you fetch the data and fill the ListVIew with it.

public class PostStory extends ActionBarActivity implements ViewPager.OnPageChangeListener {
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(this);
         ...
     } 

     @Override
     public void onPageSelected(int position) {
         if(position == ?) fetchAndSetData(); // Here fetch and set the data
     }

     @Override
     public void onPageScrollStateChanged(int state) {}

     @Override
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

     ...
}

Upvotes: 1

Related Questions