Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

android - adding Viewpager as a header to a listView

I'm trying to program an activity that show a viewpager with some images and listview below with some data .. but my problem is that I can scroll only the listview on the last part of the screen I wanted to make the viewpager scrollable with the listview so I thought about putting it as header .. here's my XMl file

<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" />

and I write this in my class

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

when I run this code it gives me a duplicated pager .. fixed one with the data and an empty one as an header of my listview .. when I remove which include my pager it crashes when I try to set an adapter to the pager .. any idea ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

just set viewPager's onTouchListener,like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...