使用 ViewPager 通过向左/向右滑动来切换 View 。
我有一个 pagerview 里面有各种 View (自定义 ListView 、 ImageView 等),当 pagerview 滑动时,子值应该根据索引更改滚动。我的问题是,当我运行我的项目时,滑动索引后的值显示为第 1 个索引而不是第 0 个索引也不合适。幻灯片大小将取决于 sqllite 数据库中的值。
调用寻呼机
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 个位置
最佳答案
您可以像这样编写页面更改监听器类:
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
}
});
关于android - 从 ViewPager 获取焦点 View 无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15518066/