从ViewPager获取焦点视图无法正常工作

时间:2013-03-20 07:53:15

标签: android android-viewpager

使用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, enter image description here

这是索引第0个位置,应该显示在第1位  enter image description here

2 个答案:

答案 0 :(得分: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

    }
});

答案 1 :(得分:1)

使用

setPrimaryItem(View container, int position, Object object)
适配器中的

(覆盖它)

为此目的。