2

I want to make an activity that contains a viewpager and listview below it which you can scroll them both .. like these apps ... anyone can help

The Verge app

mnagy
  • 1,085
  • 1
  • 17
  • 36
  • Hi! What have you tried so far? – Rémi F Aug 25 '13 at 19:26
  • I have made a pager with a listView below it .. but I couldn't make the pager scroll up and down with the listView .. that's my biggest problem so far – mnagy Aug 26 '13 at 09:19
  • am also looking this type.viewpager of s;idable images and below recyclerview.in one fragment that fragment resides in activity having toolbar and framelayout plan to place the new fragment with viewpager and recyclerview floating action button in this framelayout please help me – Harsha Nov 10 '15 at 05:37

2 Answers2

5

Well I could do it with some way by measuring the height of the listView and adding the listview and the view pager into scroll view here's the sample I've made

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);

myPager.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                PointF downP = new PointF();
                PointF curP = new PointF();
                int act = event.getAction();
                if (act == MotionEvent.ACTION_DOWN
                        || act == MotionEvent.ACTION_MOVE
                        || act == MotionEvent.ACTION_UP) {
                    ((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
                    if (downP.x == curP.x && downP.y == curP.y) {
                        return false;
                    }
                }
                return false;
            }
        });

Aadpater aa = new Aadpater(this, text);
final ListView ll = (ListView) findViewById(R.id.listView);

ll.setAdapter(aa);

Utility.setListViewHeightBasedOnChildren(ll);
}

private int imageArra[] = { R.drawable.about_logo, R.drawable.ic_launcher,
        R.drawable.nagy_cv, R.drawable.rewrew };

private String text[] = { "one", "two", "three", "four", " five", "six",
        "seven", "eight", "one", "two", "three", "four", " five", "six",
        "seven", "eight", "one", "two", "three", "four", " five", "six",
        "seven", "eight", "one", "two", "three", "four", " five", "six",
        "seven", "eight", "one", "two", "three", "four", " five", "six",
        "seven", "eight" };

}

and here's how I measure the listView height

  public class Utility {

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {
            // pre-condition
            return;
        }


        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
                MeasureSpec.UNSPECIFIED);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
    }

and here's my layout

  <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <android.support.v4.view.ViewPager
            android:id="@+id/myfivepanelpager"
            android:layout_width="fill_parent"
            android:layout_height="300dp" />

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="0dp" >
        </ListView>
    </LinearLayout>

</ScrollView>
mnagy
  • 1,085
  • 1
  • 17
  • 36
  • 1
    Hi, Am trying to implement the same, can you please share the code in GitHub or DropBox. Am trying my best to achieve, but... :( – VenomVendor Sep 03 '13 at 09:18
0

I would use a ViewPager in combination with a Title Strip.

On the android developers site you have a good tutorial for this.

You need the following objects:

  • FragmentActivity
  • FragmentStatePagerAdapter (controlls your current view and swipe-movements)
  • Fragment (in onCreateView() you can declare your Layout of the SwipeViews)

Just try the tutorial and let me know if you have more question to certain issues.

UPDAT 2013-08-26

Ok, next time you should try to precise your question and if you have code than show it. So the SO community can give you a more precise and fast help :)

As you describe in your comment you can use a ScrollView instead of a LinearLayout. Than you have two options to make your listView scrolling inside a scrollView:

Community
  • 1
  • 1
owe
  • 4,890
  • 7
  • 36
  • 47
  • I did it by other way .. I put a viewPager with a ListView in a Linear layout .. my problem is .. how to make the viewpager scroll up and down with the listview .. since it won't work if I put the listview inside a scroll view – mnagy Aug 26 '13 at 09:42
  • I updated my answer: you have to use ScrollView and override one of the methos above (option1/option2). One question to your listview: is it in all your tabs the same? As I understand you swipe only the things above the listview, is this right? – owe Aug 26 '13 at 10:58