【问题标题】:Listview not holding drawable on scroll in choice mode "CHOICE_MODE_MULTIPLE"在选择模式“CHOICE_MODE_MULTIPLE”下,Listview 不能在滚动时保持可绘制
【发布时间】:2018-05-13 22:43:05
【问题描述】:

我正在尝试通过在 OnItemClickListener 中设置 drawable 来多选列表视图,但是当我滚动列表视图时,所选项目会丢失我在 Listview.OnitemClickListener 中的 ItemChecked 方法中更改的可绘制对象。

我都试过了:

1.在drawable中使用xml选择器

2.尝试在自定义适配器中更改选定项的可绘制对象。

以上方法均不适用于stackoverflow的任何解决方案。

这是我的适配器:

public class QuestionsOptionsAdapterMultipleSelectionLV extends BaseAdapter {
    Context context;
    ArrayList<QuestionOptionsModel> items;
    String which;
    CollegepondSandbox cs;
    ArrayList<String> arr;
    boolean isTouch = false;
    int positionselected;
    QuestionsOptionsAdapterMultipleSelectionLV adapter1;



 public QuestionsOptionsAdapterMultipleSelectionLV(Context context, 
        ArrayList<QuestionOptionsModel> items) {
        this.context = context;
        this.items = items;
        this.which = which;
        this.arr = arr;
        cs = new CollegepondSandbox();


    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

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

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
       // positionselected =items.indexOf(items.get(position));
        return items.indexOf(items.get(position));
    }

    @Override
    public int getViewTypeCount() {
        return getCount();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup 
    parent) {
            if(convertView==null){
        LayoutInflater inflater = (LayoutInflater) 
    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

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

        WebView tvOptiontext;
        ImageView ivOption;

        LinearLayout llOptionsMultiple;

        ivOption = (ImageView) convertView.findViewById(R.id.cbOption);
        tvOptiontext = (WebView) convertView.findViewById(R.id.tvOptiontext);
        llOptionsMultiple = (LinearLayout) convertView.findViewById(R.id.llOptionsMultiple);
        tvOptiontext.loadData(items.get(position).QOption, "text/html", "utf-8");

        return convertView;
        }
       }

这是我的 OnItemClickListener:

  lvOption.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            lvOption.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(final AdapterView<?> parent, View view, int position, long id) {

                    ivOption = (ImageView) view.findViewById(R.id.cbOption);

                       if (lvOption.isItemChecked(position)) {
                            ivOption.setImageResource(R.drawable.tick_2);
       multipleSelectedIDs.add(answersal.get(position).QIsCorrect);
                        } else {
                            ivOption.setImageResource(R.drawable.tick_1);
      multipleSelectedIDs.remove(answersal.get(position).QIsCorrect);
                        }
                });

【问题讨论】:

    标签: listview multipleselection


    【解决方案1】:

    在 stackoverflow @Shaiful 上发现了一个类似的问题,并根据我的需要进行了修改。

    步骤:

    1.将位置作为字符串存储在Arraylist中

    2.将数组列表传递给适配器。

    3.在适配器中而不是在活动中更改可绘制对象,如下面的代码所示:

    活动中 listview.OnItemClick 的变化:

      if (lvOption.isItemChecked(position)) {
                            multipleSelectedIDs.add(answersal.get(position).QIsCorrect);
                            selected.add(String.valueOf(position));
                        } else {
                            multipleSelectedIDs.remove(answersal.get(position).QIsCorrect);
                            selected.remove(String.valueOf(position));
                        }
                        adapterMultipleListview.setSelectedDrawable(selected);
    

    现在适配器的变化:

    //add this method in adapter
     public void setSelectedDrawable(ArrayList<String> ind) {
        selectedIndex = ind;
        notifyDataSetChanged();
    }
    //change drawable in getview() method
     if (selectedIndex.contains(String.valueOf(position))) {
            ivOption.setBackgroundResource(R.drawable.tick_2);
        } else {
            ivOption.setBackgroundResource(R.drawable.tick_1);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多