将Viewpager作为标题添加到listView

时间:2013-06-13 12:36:23

标签: android android-listview android-viewpager

我正在尝试编写一个活动,显示一个带有一些图像的viewpager和下面的listview以及一些数据..但我的问题是我只能在屏幕的最后一部分滚动列表视图我想制作viewpager使用listview可滚动,所以我想把它作为标题..这是我的XMl文件

<TableLayout 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"
tools:context=".MainActivity" >

<com.devsmart.android.ui.HorizontalListView
    android:id="@+id/sectionsList"
    android:layout_width="wrap_content"
    android:layout_height="25dp"
    android:background="@drawable/menu_bg" />

<include layout="@layout/main_screen_components" />

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

我在课堂上写这个

@Override
    protected void onPostExecute(List<News> result) {
        Utils.pagerNews(result);
        Utils.listNews(result);

        ImagePagerAdapter pAdapter = new ImagePagerAdapter(
                appManager.getPagerNews());
        pager.setAdapter(pAdapter);

        View headerView = getLayoutInflater().inflate(R.layout.main_screen_components , null , false);

        NewsListAdapter adapter = new NewsListAdapter(getBaseContext(),
                appManager.getListNews());
        listView.addHeaderView(headerView);
        listView.setAdapter(adapter);
        progress.dismiss();
    }

当我运行这段代码时,它给了我一个重复的分页器..修复了一个数据和一个空的一个作为我的listview的标题..当我删除其中包括我的寻呼机它崩溃当我尝试设置适配器传呼机..任何想法?

2 个答案:

答案 0 :(得分:20)

只需设置viewPager的onTouchListener,就像这样:

viewPager.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;
  }

答案 1 :(得分:0)

使用以下自定义viewpager类:

public class CustomViewPager extends ViewPager {
 boolean xScored = false, yScored = false;

 public CustomViewPager(Context context) {
    super(context);
 }

 public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
 }

 @Override
 public boolean onTouchEvent(MotionEvent ev) {
    mGestureDetector.onTouchEvent(ev);
    if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
        xScored = false;
        yScored = false;
        requestDisallowInterceptTouchEvent(false);
    }
    return super.onTouchEvent(ev);
 }

 GestureDetector.SimpleOnGestureListener mOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        if (!yScored && Math.abs(distanceX) > Math.abs(distanceY)) {
            xScored = true;
            yScored = false;
        }
        if (xScored) {
            requestDisallowInterceptTouchEvent(true);
        } else if (!xScored && Math.abs(distanceY) > Math.abs(distanceX)) {
            xScored = false;
            yScored = true;
            requestDisallowInterceptTouchEvent(false);
        }
        return true;
    }

 };
 GestureDetector mGestureDetector = new GestureDetector(getContext(), mOnGestureListener);
}