1

Based on android Creating Swipe Views with Tabs

http://developer.android.com/training/implementing-navigation/lateral.html

How can I Take listview with custom adapters and use in this example? I mean instead showing simple textview , display as.

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Description"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:text="Example application"
        android:textSize="16sp" />

  </RelativeLayout>

Result should be as Play store application.

Thanks.

user3004288
  • 17
  • 1
  • 5

1 Answers1

2

You can try to check my other posts:

Android List view layout Similar to Google play

Unable to use AndroidDrawer (sidebar like facebook)

Maybe this is what you are looking for:

How to use swipe gesture in a view pager's page with android?

Try these links..maybe this is what you are looking for.

Here's an Update

1) Make an Adapter to handle your fragments:

public class MyAdapter extends FragmentStatePagerAdapter {

    public MyAdapter(FragmentManager fm) {

        super(fm);

    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:

            return new FirstFragment();

        case 1:

            return new SecondFragment();
        case 2:

            return new ThirdFragment();

        }
        return null;

    }

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

}

2) Add fragments: I am just adding the code for one fragment. You can follow that.

public class FirstFragment extends Fragment {

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

    View view = inflater.inflate(R.layout.somefragment, container, false);//You will have your custom layout and add more items. 

    return view;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    TextView tv = (TextView)getActivity().findViewById(R.id.textview1);
    //You can add different items in here according to your needs. 
    //You will have your own code to do stuff. Like to use a listview or a gridview etc. You will make a separate adapter in a separate class and use that adapter in this onActivityCreated() method.
}


}

3) Modify the MainActivity:

public class AllActivities extends FragmentActivity implements ActionBar.TabListener {

    public ViewPager viewPager;
    private MyAdapter mAdapter;
    private ActionBar actionBar;
    private String [] tabs = {"FirstFragment","SecondFragment","ThirdFragment"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Initializing all stuff
        viewPager = (ViewPager)findViewById(R.id.pager);

        actionBar = getActionBar();
        mAdapter = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);



        //Add the tabs here
        for(String tab_name:tabs){
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){

            @Override
        public void onPageSelected(int position){

                //on Page change, that particular page should be selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
                public void onPageScrolled(int arg0,float arg1,int arg2){

            }
            @Override
        public void onPageScrollStateChanged(int position){

            }

        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {

        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {



    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {

        viewPager.setCurrentItem(tab.getPosition());

    }
}

After you have implemented this example, you don't have to worry about this MainActivity. All you have to add more fragments is that add fragments to your adapter and build pages according to that. Also, add private String [] tabs = {"FirstFragment","SecondFragment","ThirdFragment","You other fragments when you will define"};

To make google play like navigation drawer, you can refer to my first two links and make then work as you like.

Hope this one helps for what you were looking for.

Community
  • 1
  • 1
mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • Hi ,thanks for reply. I read both your link . my problem is still unresolved to me. If I have ViewPager and each view showing one simple element TextView and all this done in single Activity main, how do I know create custom complex layout different for each view ? Is that still done in single activity ? – user3004288 May 26 '14 at 18:20
  • You can just do it by adding different layouts for each view. For example, in one view you can have a text view and an image view, in second view you can have maybe an image view and a listview and it goes on and on. I'll update my answer a bit for more explanation. – mike20132013 May 26 '14 at 18:46
  • Yes that my point, I am looking for example that do exactly what you sad . Do I need to define custom adapter for each layout ? And this done in MainActivity ? or need to define subActivities ? – user3004288 May 26 '14 at 18:53
  • You only need one activity to do that because you are just inflating different layouts in the adapter. You need just one adapter to achieve this. But, if you are planning to use those layouts for some other purpose, why not just use fragments. It's gonna be much easier to modify it. You can have as many fragments you want and customize your fragments to your needs. – mike20132013 May 26 '14 at 19:00
  • For instance, you can have 5 fragments with different items on each fragment and you will need only one adapter to achieve that. – mike20132013 May 26 '14 at 19:02
  • ok, Is there example that do that... I just need to look kind of example to understand all process – user3004288 May 26 '14 at 19:03
  • I am posting a simple code to this answer in few minutes. – mike20132013 May 26 '14 at 19:05
  • Have a look at the updated answer. I have added a sample code for better understanding. – mike20132013 May 26 '14 at 19:24
  • ok , thanks. if fragment layout is listview and this listview updated/created dynamically where in yours code you create listview ? – user3004288 May 26 '14 at 19:38
  • To have a listview in any one of the fragments, just add the listview to the matching fragment's layout. For example, if you add listview to FirstFragment's layout, write the code for your listview in the FirstFragment's onActivityCreate() method. Similarly you can add your listview to any of the fragments. Now, important thing is that, you have to make your separate adapter for your listview and then use it any of the fragments. Does that answer your question ? – mike20132013 May 26 '14 at 19:48
  • Now, you can define listviews in the layouts for all your fragments and use the same adapter in all your fragments. – mike20132013 May 26 '14 at 19:50
  • Yes , I think that's what I am searching for:) – user3004288 May 26 '14 at 19:56
  • If the above code works according to your needs, don't forget to accept/upvote the answer for future viewers .. :) – mike20132013 May 26 '14 at 20:01