Viewpager和listView在同一个Activity上

时间:2013-08-25 14:25:36

标签: android-listview android-viewpager

我想制作一个包含viewpager和listview的活动,您可以在其中滚动它们......就像这些应用程序一样......任何人都可以提供帮助

The Verge app

2 个答案:

答案 0 :(得分:5)

嗯,我可以通过测量listView的高度并将listview和view pager添加到滚动视图中以某种方式来实现它这里是我做的样本

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高度的方法

  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>

答案 1 :(得分:0)

我会将ViewPagerTitle Strip结合使用。

在Android开发者网站上,您有a good tutorial for this

您需要以下物品:

  • FragmentActivity
  • FragmentStatePagerAdapter(控制您当前的视图和滑动动作)
  • Fragment(在onCreateView()中您可以声明您的SwipeViews布局)

试试教程,如果您对某些问题有更多疑问,请告诉我。

UPDAT 2013-08-26

好的,下次你应该尝试确定你的问题,如果你有代码而不是显示它。因此,SO社区可以为您提供更准确,更快速的帮助:)

正如您在评论中所述,您可以使用ScrollView代替LinearLayout。 比你有两个选项让你的listView在scrollView中滚动: