Viewpager和ListView在同一活动 [英] Viewpager and listView on the same Activity

查看:245
本文介绍了Viewpager和ListView在同一活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个包含下面的一个viewpager和ListView的活动,你可以滚动他们两个..喜欢这些应用程序......任何人都可以帮助

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

推荐答案

好吧,我可以用一些方法通过测量在ListView的高度,并添加列表视图和视图寻呼机进入滚动视图这里的样品我做了这样做

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" };

}

和这里的如何我衡量ListView的高度

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();
    }
    }

这是我的布局

  <?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>

这篇关于Viewpager和ListView在同一活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆