Imageview 在滚动列表视图上自动更改
Posted
技术标签:
【中文标题】Imageview 在滚动列表视图上自动更改【英文标题】:Imageview getting automatically changed on scroll listview 【发布时间】:2015-11-18 16:47:02 【问题描述】:我是 android 新手,面临一个非常常见的 listview 问题,我在 Google 上搜索了很多,但没有找到任何对我的案例有用的东西。希望你们能告诉我正确的方向。每当我将图像从“加号”更改为单击“勾选”我的最后一个项目图像也会在滚动时自动更改。
我的 list_comapare_product XMl :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="@color/white_color"
android:orientation="vertical" >
<RelativeLayout
android:layout_
android:layout_
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/white_color"
android:gravity="center_vertical" >
<ImageView
android:id="@+id/img_product"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/crane" />
<com.mss.skyjack.custom.views.SkyjackCustomTextview
android:id="@+id/tv_product_name"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="test"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/img_plus"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/plus" />
</RelativeLayout>
<View
android:layout_
android:layout_
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/black_color" />
</LinearLayout>
这是我的适配器:-
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ComapreProductSelectionAdapter extends BaseAdapter
Activity activity;
List<SelectorTest> listSelector;
private LayoutInflater mLayoutInflater;
public ComapreProductSelectionAdapter(Activity activity,
List<SelectorTest> listSelector)
this.activity = activity;
this.listSelector = listSelector;
mLayoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount()
return listSelector.size();
@Override
public SelectorTest getItem(int position)
return listSelector.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
final ViewHolder viewHolder;
final int i = position;
final SelectorTest listItem = listSelector.get(position);
if (convertView == null)
convertView = mLayoutInflater.inflate(
R.layout.list_comapare_product, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvProductName = (TextView) convertView
.findViewById(R.id.tv_product_name);
viewHolder.imgProduct = (ImageView) convertView
.findViewById(R.id.img_product);
viewHolder.imgadd = (ImageView) convertView
.findViewById(R.id.img_plus);
convertView.setTag(viewHolder);
else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.imgadd.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
if (viewHolder.imgadd.getDrawable().getConstantState() == activity
.getResources().getDrawable(R.drawable.plus)
.getConstantState())
viewHolder.imgadd.setImageResource(R.drawable.tick);
else
viewHolder.imgadd.setImageResource(R.drawable.plus);
);
return convertView;
static class ViewHolder
TextView tvProductName;
ImageView imgProduct, imgadd;
View viewRightLine;
谢谢。
【问题讨论】:
每次滚动列表时都会调用视图函数,所以我认为我应该使用位置,但不知道如何......我什至尝试使用 viewHolder.imgadd.setTag(position).. .但还没有摆脱这个问题.. 老兄看看我的回答 查看我的更新答案。这是完整的答案。无论如何,您为什么将 Activity 传递给 Adapter?您应该使用 Context 而不是 Activity。 【参考方案1】:您需要跟踪哪些项目已被“勾选”。我复制了您的适配器并进行了必要的更改。您只需复制粘贴代码即可。
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ComapreProductSelectionAdapter extends BaseAdapter
Activity activity;
List<SelectorTest> listSelector;
private LayoutInflater mLayoutInflater;
private ArrayList<Integer> tickedItems = new ArrayList<Integer>();
public ComapreProductSelectionAdapter(Activity activity,
List<SelectorTest> listSelector)
this.activity = activity;
this.listSelector = listSelector;
mLayoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount()
return listSelector.size();
@Override
public SelectorTest getItem(int position)
return listSelector.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
final ViewHolder viewHolder;
final int i = position;
final SelectorTest listItem = listSelector.get(position);
if (convertView == null)
convertView = mLayoutInflater.inflate(
R.layout.list_comapare_product, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvProductName = (TextView) convertView
.findViewById(R.id.tv_product_name);
viewHolder.imgProduct = (ImageView) convertView
.findViewById(R.id.img_product);
viewHolder.imgadd = (ImageView) convertView
.findViewById(R.id.img_plus);
convertView.setTag(viewHolder);
else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.imgadd.setTag(position);
viewHolder.imgadd.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
if(tickedItems.contains((Integer)v.getTag())
//Already ticked, set to plus
viewHolder.imgadd.setImageDrawable(activity.getResources().getDrawable(R.drawable.plus));
tickedItems.remove((Integer)v.getTag());
else
tickedItems.add((Integer)v.getTag());
viewHolder.imgadd.setImageDrawable(activity.getResources().getDrawable(R.drawable.tick));
);
if(tickedItems.contains(position))
viewHolder.imgadd.setImageDrawable(activity.getResources().getDrawable(R.drawable.tick));
else
viewHolder.imgadd.setImageDrawable(activity.getResources().getDrawable(R.drawable.plus));
return convertView;
static class ViewHolder
TextView tvProductName;
ImageView imgProduct, imgadd;
View viewRightLine;
另外,不要使用ImageView.setImageResource()
,因为它效率低。
来自 Android 文档的关于 ImageView.setImageReource()
的引用
这会在 UI 线程上进行位图读取和解码,这可能会导致延迟中断。如果这是一个问题,请考虑使用 setImageDrawable(android.graphics.drawable.Drawable) 或 setImageBitmap(android.graphics.Bitmap) 和 BitmapFactory。
【讨论】:
嘿大卫谢谢你的回复,但是当我试图把 viewHolder.imgadd.setImageDrawable(R.drawable.plus);我收到一条错误消息,提示“ImageView 类型中的方法 setImageDrawable(Drawable) 不适用于参数 (int)”。 改用getResources().getString(R.string.try_again)
代替 setImageDrawable(R.drawable.plus) 尝试 getResources().getDrawable(R.string.try_again)以上是关于Imageview 在滚动列表视图上自动更改的主要内容,如果未能解决你的问题,请参考以下文章