gpt4 book ai didi

android - 如何在 android 的 PagerAdapter 中不重复对象 instantiateItem()

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:44:49 26 4
gpt4 key购买 nike

我的 logcat 在打印中第一次在重复此方法后占据位置 0 并占据位置 1,我不需要重复此方法。

public  Object instantiateItem(ViewGroup container, int position) {

Context context = MainActivity.this;

Log.d("position","position" +position);
ImageView imageView = new ImageView(context);

int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);

((ViewPager) container).addView(imageView, 0);

return imageView;

}

图像幻灯片中的这段代码成功运行,但图像幻灯片的第一张和最后一张错误打印了 logcat 中的位置。请帮助我。谢谢。

编辑:

public class MainActivity extends Activity {

ViewPager viewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
adapter.notifyDataSetChanged();
viewPager.setAdapter(adapter);

}

private class ImagePagerAdapter extends PagerAdapter {

private int[] mImages = new int[] {
R.drawable.chiang_mai,
R.drawable.himeji,
R.drawable.petronas_twin_tower,
R.drawable.ulm
};
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}


@Override
public int getCount() {
return mImages.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

Context context = MainActivity.this;

Log.d("position","position" +position);
ImageView imageView = new ImageView(context);

int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);

((ViewPager) container).addView(imageView, 0);

return imageView;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// viewPager.getAdapter().notifyDataSetChanged();
((ViewPager) container).removeView((ImageView) object);
}
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub

super.onDestroy();
}

}

当我左右滑动图像时,此代码在 logcat 中的打印位置错误。当位置增加时我需要右侧幻灯片图像,而左侧幻灯片图像减少位置。请帮助我。感谢。

最佳答案

你需要解决这段代码中的问题

public class MainAdapter extends BaseAdapter {
public Activity activity;
ArrayList<String> data = new ArrayList<String>();

public MainAdapter(Activity a, ArrayList<String> d) {
activity = a;
data = d;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View cellView = convertView;
if (cellView == null)
{
cellView = new View(activity);
Log.d(TAG, "cellView null");
} else
{
Log.d(TAG, "cellView not null");
cellView = (View) convertView;
}
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cellView = inflater.inflate(R.layout.textviewinfo, null);

textView = (TextView) cellView.findViewById(R.id.textView1);

/*LinearLayout layout = (LinearLayout)cellView.findViewById(R.id.l1);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 100));*/

LinearLayout.LayoutParams parms2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
parms2.gravity=Gravity.CENTER_HORIZONTAL;
//parms2.gravity=Gravity.CENTER_VERTICAL;
textView.setLayoutParams(parms2);
textView.setText(data.get(position));
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setTextColor(Color.YELLOW);
return cellView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
//return 0;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
//return 0;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
//return 0;
}
}

关于android - 如何在 android 的 PagerAdapter 中不重复对象 instantiateItem(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14907213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com