3

使用 ViewPager 通过左/右滑动切换视图。

我有一个 pagerview,里面有各种视图(自定义列表视图、图像视图等),当滑动 pagerview 时,应根据索引更改子值以滚动。我的问题是,当我运行我的项目时,在滑动索引后显示第一个索引而不是第 0 个索引的值也不合适。幻灯片大小将取决于 sqllite 数据库中的值。

调用pagerview

pager = (ViewPager) findViewById(R.id.panelPager);
        adapter = new MyPagerAdapter();     
        pager.setAdapter(adapter);
        pager.setCurrentItem(0);

这是我的寻呼机适配器类

public class MyPagerAdapter extends PagerAdapter {

        @SuppressWarnings("deprecation")
        @Override 
        public Object instantiateItem(View collection,int position) {   
            Log.d("Pos",""+position);
            PagerView = new View(collection.getContext());

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            PagerView = inflater.inflate(R.layout.newmainviewpager, null, false);
            lvMenu=(ListView)PagerView.findViewById(R.id.lvMenuItem);
            imgMainItem=(ImageView)PagerView.findViewById(R.id.imgDisplay);
            edQty=(TextView)PagerView.findViewById(R.id.edQty1);
            edQty.setText("1");
            btnAdd=(Button)PagerView.findViewById(R.id.btnAdd);
            btnSub=(Button)PagerView.findViewById(R.id.btnsub);
            tvDesc=(TextView)PagerView.findViewById(R.id.tvDesc);
            btnPurchaseOrder=(Button)PagerView.findViewById(R.id.btnPlaceOrder);
            btnPurchaseOrder.setOnClickListener(RevisedMainMenu.this);
            imgfbConnect=(Button)PagerView.findViewById(R.id.imgfbConnect);
            imgfbConnect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FetchImagePath fetchimage = new FetchImagePath(RevisedMainMenu.this);
                    fetchimage.execute("http://192.168.1.6/dat/imagepath.php");
                }
            });
            displayMenu(position);
            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int qty=Integer.parseInt(edQty.getText().toString().trim());
                    qty=qty+1;
                    edQty.setText(String.valueOf(qty));
                }
            });
            btnSub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int qty=Integer.parseInt(edQty.getText().toString().trim());
                    if(qty>1)
                    {
                        qty=qty-1;
                        edQty.setText(String.valueOf(qty));
                    }
                    else
                        Toast.makeText(getApplicationContext(), "Invalid Operation",Toast.LENGTH_LONG).show();
                }
            });
            lvMenu.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
                    SubMenuid12=ExtraSubMenuIdlst.get(position);
                    byte[] photo =datasource.getImagePathFromSubMenuId(ExtraSubMenuIdlst.get(position));
                    ByteArrayInputStream imageStream = new ByteArrayInputStream(photo);
                    Bitmap theImage= BitmapFactory.decodeStream(imageStream);
                    Bitmap bitmapScaled = Bitmap.createScaledBitmap(theImage, 300,250, true);
                    Drawable drawable = new BitmapDrawable(bitmapScaled);
                    imgMainItem.setBackgroundDrawable(drawable);      
                    String Desc=datasource.getSubMenuDescriptionFromSubMenuId(SubMenuid12);
                    tvDesc.setText(Desc);
                    selectionPos=position;
                    Log.d("SelPos",""+selectionPos);   //.d("das","dasdas");
//                  
                }
            });
//          }
//          Log.d("MenuID",MenuIdlst.get(position));

            ((ViewPager) collection).addView(PagerView, 0);
            return PagerView;

        }
        @Override
        public void destroyItem(final View arg0, final int arg1,
                final Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);

        }

        @Override
        public boolean isViewFromObject(final View arg0, final Object arg1) {
            return arg0 == ((View) arg1);

        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return  MenuIdlst.size();
        }

    }

这是第 1 个显示的索引 1, 在此处输入图像描述

这是第 0 个索引位置,应该显示在第 1 个位置 在此处输入图像描述

4

2 回答 2

1

您可以像这样编写页面更改侦听器类:

pager.setOnPageChangeListener(new OnPageChangeListener() {

    @Override
    public void onPageSelected(int pageNum) {
        // TODO Auto-generated method stub
        Log.d("index", "onpagechange: " + pageNum);
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

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

    }
});
于 2013-03-25T09:48:50.793 回答
1

采用

setPrimaryItem(View container, int position, Object object)

在您的适配器中(覆盖它)

以此目的。

于 2013-03-31T18:35:49.270 回答