具有自定义适配器状态持久性的ListView

时间:2013-11-01 05:41:27

标签: android android-listview custom-adapter

我有一个带自定义适配器的列表视图。自定义adater只是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;
        }
    }

然而,我仍然会遇到同样的行为。 任何帮助表示赞赏。

UPD:

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;
}

}

3 个答案:

答案 0 :(得分: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;
}

}

希望这能解决您的问题。

答案 1 :(得分: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,并在您再次点击它时将其删除。

答案 2 :(得分: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;
}