我的应用程序中有一个 ListView,其中只有 ImageViews。根据随机选择方法,每行显示 1、2 或 3 个图像。但是我现在面临的问题是,当我滚动时,图像的顺序会发生变化,而我希望它们始终保持不变。另外,即使我使用了高效的“int”迭代器来检索和显示该行中的下一个图像,图像也会重复。
我检查了其他处理类似问题的问题,但它们不处理 ImageViews。我是新手,所以任何帮助将不胜感激。
public class YPAdapter extends BaseAdapter{
public Context context;
public ArrayList<ImageDetails> flowImages = new ArrayList<ImageDetails>();
LayoutInflater inflater;
private ArrayList mData = new ArrayList();
public int i = Home.getStart();
YPAdapter(Context context, ArrayList<ImageDetails> name, int start) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.flowImages = name;
}
public void addItem(final int item) {
mData.add(item);
notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public int getItemViewType(int position) {
int x = 0;
if (Integer.parseInt(mData.get(position).toString()) == 1) x = 0;
else if(Integer.parseInt(mData.get(position).toString()) == 2) x = 1;
else if(Integer.parseInt(mData.get(position).toString()) == 3) x = 2;
return x;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int mode = getItemViewType(position);
if(convertView == null)
{
switch(mode){
case 0:
convertView = inflater.inflate(R.layout.image_container_1, parent, false);
if(i < (flowImages.size()))
{
ImageView imageView = (ImageView) convertView.findViewById(R.id.logo_1);
ArrayList<ImageDimensions> dimensions = new ArrayList<ImageDimensions>();
dimensions = ImageSorter.getLayout(1);
// get parameters
// set image with url
i++;
}
break;
case 1:
convertView = inflater.inflate(R.layout.image_container_2, parent, false);
if(i < (flowImages.size()-1))
{
ImageView imageView1 = (ImageView) convertView.findViewById(R.id.logo_2_1);
ImageView imageView2 = (ImageView) convertView.findViewById(R.id.logo_2_2);
ArrayList<ImageDimensions> dimensions = new ArrayList<ImageDimensions>();
// get parameters for 2 images
// set image for first imageview
i++;
// set image for second imageview
i++;
}
break;
case 2:
convertView = inflater.inflate(R.layout.image_container_3, parent, false);
if(i < (flowImages.size()-2))
{
ImageView imageView1 = (ImageView) convertView.findViewById(R.id.logo_3_1);
ImageView imageView2 = (ImageView) convertView.findViewById(R.id.logo_3_2);
ImageView imageView3 = (ImageView) convertView.findViewById(R.id.logo_3_3);
ArrayList<ImageDimensions> dimensions = new ArrayList<ImageDimensions>();
// get parameters for 3 images
// set image with url for first imageview
i++;
// set image for second imageview
i++;
// set image for third imageview
i++;
}
break;
}
}
return convertView;
}
public Object getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
}
这是 ListView 的 XML:
<ListView
android:id = "@+id/home_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>