0

我有一个带有自定义适配器的列表视图。自定义适配器只是 TextView 和 2 个 ImageButtons。单击 ImageButton 时,我希望它的图像切换。但是,这有效,当我向下滚动时,一些随机项目也会被切换。

所以我认为这与屏幕上项目位置的相对性有关。所以我为每个 ImageButton 分配了一个标签,它对应于它在列表视图中的位置。然后我使用以下代码正确选择必要的项目:

    int firstVisiblePos = listView.getFirstVisiblePosition();
    int viewPos = 0;
    int position = Integer.parseInt(v.getTag().toString());
    Toast.makeText(this, v.getTag().toString(), Toast.LENGTH_SHORT).show();
    for (int i = 0; i < listView.getChildCount(); i++) {
        viewPos = firstVisiblePos + i;
        if (viewPos == position) {              
            relativeLayout = (RelativeLayout) listView.getChildAt(position);
            break;
        }
    }

但是我仍然遇到相同的行为。任何帮助表示赞赏。

升级版:

public class CustomAdapter extends BaseAdapter {
private ArrayList<String> data;
private Activity myActivity;
private LayoutInflater inflater;

public CustomAdapter(Activity activity, ArrayList<String> data) {
    this.myActivity = activity;
    this.data = data;
    this.inflater = (LayoutInflater) myActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_row, null);
    }

    TextView proverb = (TextView) convertView.findViewById(R.id.title);
    proverb.setText(data.get(position));
    proverb.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 14);
    proverb.setTypeface(MainActivity.tf, 1);
    ImageButton favBtn = (ImageButton) convertView.findViewById(R.id.btn_favorite);
    if(MainActivity.favorites.contains(data.get(position))){
        // favorite, set fav icon
        favBtn.setImageDrawable(myActivity.getResources().getDrawable(R.drawable.fav));         
    }       
    favBtn.setTag(position);
    return convertView;
}

}

4

3 回答 3

1

问题是当用户滚动列表视图时,您的列表项会被重用。因此,您必须每次都根据数据重建列表项。如果该项目是收藏​​项,则看起来您正在更改图像,但如果它不是收藏项,则您不会将其更改回来。

尝试改变

if(MainActivity.favorites.contains(data.get(position))){
    // favorite, set fav icon
    favBtn.setImageDrawable(myActivity.getResources().getDrawable(R.drawable.fav));         
}

if(MainActivity.favorites.contains(data.get(position))){
    // favorite, set fav icon
    favBtn.setImageDrawable(myActivity.getResources().getDrawable(R.drawable.fav));         
} else {
    // not favorite, set default icon
    favBtn.setImageDrawable(myActivity.getResources().getDrawable(R.drawable.notfav));
}

我假设当您单击图像按钮时,它会在您再次单击时添加data.get(position)MainActivity.favorites删除它。

于 2013-11-01T06:27:58.140 回答
1

像这样试试

首先创建一个模型来存储值并检查 Favurite

public class Data {

private String value;
private boolean favurite;

public Data(String value,boolean favurite){
    this.value=value;
    this.favurite=favurite;
}
public String getValue() {
    return value;
}
public void setValue(String value) {
    this.value = value;
}
public boolean isFavurite() {
    return favurite;
}
public void setFavurite(boolean favurite) {
    this.favurite = favurite;
}

}

然后,而不是在您的创建ArrayList<String>更改它ArrayList<Data>Activity

ArrayList<Data> list=new ArrayList<Data>();
        list.add(new Data("value1", false));
        list.add(new Data("value2", false));

并将其传递给 CustomAdapter 类

我更改了您的适配器类以使其正常工作

public class CustomAdapter extends BaseAdapter {
    private ArrayList<Data> data;
    private Activity myActivity;
    private LayoutInflater inflater;

    public CustomAdapter(Activity activity, ArrayList<Data> data) {
        this.myActivity = activity;
        this.data = data;
        this.inflater = (LayoutInflater) myActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_row, null);
    }
    final Data d=data.get(position);
    TextView proverb = (TextView) convertView.findViewById(R.id.title);
    proverb.setText(d.getValue());
    proverb.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 14);
    proverb.setTypeface(MainActivity.tf, 1);
    final ImageButton favBtn = (ImageButton) convertView.findViewById(R.id.btn_favorite);

    if(d.isFavurite())
        favBtn.setImageDrawable(myActivity.getResources().getDrawable(R.drawable.fav));    
    else
        favBtn.setImageDrawable(myActivity.getResources().getDrawable(R.drawable.Non_fav));

    favBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            d.setFavurite(true);
            favBtn.setImageDrawable(myActivity.getResources().getDrawable(R.drawable.fav));    
            notifyDataSetChanged();
        }
    });
    return convertView;
}

}

希望这能解决您的问题。

于 2013-11-01T06:09:02.540 回答
0

做一件事,,,不检查convertView是否为空,直接一一编码,,,像这样

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = inflater.inflate(R.layout.list_row, null);


    TextView proverb = (TextView) convertView.findViewById(R.id.title);
    proverb.setText(data.get(position));
    proverb.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 14);
    proverb.setTypeface(MainActivity.tf, 1);
    ImageButton favBtn = (ImageButton) convertView.findViewById(R.id.btn_favorite);
    if(MainActivity.favorites.contains(data.get(position))){
        // favorite, set fav icon
        favBtn.setImageDrawable(myActivity.getResources().getDrawable(R.drawable.fav));         
    }       
    favBtn.setTag(position);
    return convertView;
}
于 2013-11-01T06:37:04.390 回答