当ViewPager(ListPager)用作ListItem时,ViewPager不会正确切换页面

时间:2013-03-13 07:02:50

标签: android listview android-viewpager

自从过去2天以来,我一直在努力解决ViewPager页面的滚动问题。请尽力帮助我。

我在listView中使用ViewPager作为ListItem。每个ListItem都有不同的ViewPagers。我创建此ListView和绑定ViewPagers的方式如下所示。

MainActivity - > ListAdapter - >来自listView getView()方法设置PagerAdapter。我认为这种流程是正确的。

现在,我正面临着ViewPager上的滚动问题。很难进入下一页。当我尝试通过按住ViewPager滚动时,下一页将到达一半(大约)并返回。

我已经将ListView中的触摸事件重新路由到ViewPager侦听器。我请求您仔细阅读以下粘贴的代码并尝试帮助我。非常感谢你的帮助。

dash_board_list=(ListView)findViewById(R.id.dash_list);
    dash_board_list.addHeaderView(upFooter);

    // Getting adapter by passing xml data ArrayList
    dash_borad_adapter=new DashBoardAdapter(DashBoard.this,MENUITEMS, IMAGEIDS,mEmpId);        
    dash_board_list.setAdapter(dash_borad_adapter);
    dash_board_list.setSmoothScrollbarEnabled(false);
    dash_board_list.setScrollContainer(false);
    dash_board_list.setHorizontalScrollBarEnabled(false);
    dash_board_list.setVerticalScrollBarEnabled(false);



    dash_board_list.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            int id = (int) ((ListView)v).getSelectedItemId();
            DashBoardAdapter.listener.onTouch(v,event);
            return false;
        }
    });

我的ListAdapter和ViewPager(列表适配器的内部类)如下所示。

public DashBoardAdapter(Context cont, String[] titles, int[] images, String empId) {

    titleData= titles;
    imageData = images;
    context = cont;
    mEmpId = empId;
    listRowView = new View[titleData.length]; 


}

public int getCount() {
    return titleData.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {

    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

    View rowView ;

    View vi=convertView;   

    if(convertView==null){
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.dash_board_list_row, parent, false); 

        PageViewer pager = new PageViewer(position,rowView);
        listRowView[position] = rowView;

    }
    else{
        rowView = convertView;
    }

    return rowView;
} 
public class PageViewer extends ViewPager{

/*  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }*/

    public PageViewer(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public PageViewer(int position, View rowView) {
        super(context);
        myPager[position] = (ViewPager) rowView.findViewById(R.id.threepageviewer);
        // TODO Auto-generated constructor stub
        MyPagerAdapter adapter = new MyPagerAdapter(myPager[position],position, rowView);

    //  pager.setOffscreenPageLimit(3);
    //  pager.setAdapter(context);

        class ViewFlipperTouchListener implements OnTouchListener {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                float  x2, y2, dx, dy;
                String direction;
                switch(event.getAction()) {
                    case(MotionEvent.ACTION_DOWN):
                        downXvalue = event.getX();
                        downYvalue = event.getY();
                        break;
                    case(MotionEvent.ACTION_UP):
                    {
                        x2 = event.getX();
                        y2 = event.getY();
                        dx = x2-downXvalue;
                            dy = y2-downYvalue;

                            // Use dx and dy to determine the direction
                        if(Math.abs(dx) > Math.abs(dy)) {
                            if(dx>0) {
                                direction = "right";
                                //pager.setCurrentItem(0,true);
                            }
                            else {
                                direction = "left";
                            //  pager.setCurrentItem(1,true);
                            }
                        } else {
                            if(dy>0) {
                                direction = "down";
                            }
                            else {
                                direction = "up";
                            }
                        }

                   //    int currPage = ((ListView)v).getSelectedItemPosition();

                    }

                }
                return false;
            }

       }

        listener = new ViewFlipperTouchListener();

        myPager[position].setOnTouchListener(listener);

    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        if(true){
            Log.d("Dash","onInterceptTouchEvent");
        }
            return super.onInterceptTouchEvent(event);

    }


}

    public class MyPagerAdapter extends PagerAdapter {

    int iListPosition = -1;
    ViewPager myPager1;
    public int getCount() {
            return 3;
    }

    public MyPagerAdapter(View pager, int listPos, View rowView){
        iListPosition = listPos;
        //currentListRowView = rowView;

        myPager1 = (ViewPager) pager;
        myPager1.setOffscreenPageLimit(3);
        myPager1.setAdapter(this);
    //  myPager1.setCurrentItem(0,true);
        rowView.setId(listPos);   // to know the listpos which is used in reloading the graphs.
        //need to check whether it is conflicting with the actual ids
        myPager1.setTag(rowView);
        initButton(rowView);

           myPager1.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
                if(arg0 ==ViewPager.SCROLL_STATE_DRAGGING) {
                 }
            }
        });
    }


enter code here

0 个答案:

没有答案