从列表视图中选择多个项目并仅更改所选项目的颜色 [英] Select multiple items from listview and change color of selected item only

查看:13
本文介绍了从列表视图中选择多个项目并仅更改所选项目的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个视图,我可以从列表视图中选择多个项目,并且并排更改所选列表项目的颜色并将该项目保存到我的数组列表中..我的列表如下所示:

I want to make a view where I can select multiple items from listview and also side by side am changing the color of selected list item and saving that item into my arraylist..My list is shown as below as:

但是当我过去滚动它时..它显示我选择了另外 1 个项目,即使我没有像这样选择它:

But when I used to scroll it.. It is showing me 1 more item selected, even I am not selecting it like:

但我希望只有那个列表项颜色应该改变我将点击的位置...

But I want that only that list item color should change where I will click...

我使用的代码是:

     private class ItemsAdapter extends ArrayAdapter<String> {
 List<String> items;
  Context context;
  private LayoutInflater inflater;
  public ItemsAdapter(Context context, List<String> part_array_list) {
     super( context, R.layout.part_list, R.id.label,part_array_list );

     inflater = LayoutInflater.from(context) ;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      TextView textView ; 
    String item = (String) this.getItem( position ); 

      if ( convertView == null ) {
        convertView = inflater.inflate(R.layout.part_list, null);

        // Find the child views.
        textView = (TextView) convertView.findViewById( R.id.label );


        // Optimization: Tag the row with it's child views, so we don't have to 
        // call findViewById() later when we reuse the row.
        convertView.setTag( new ListViewHolder(textView) );


      }
      // Reuse existing row view
      else {
        // Because we use a ViewHolder, we avoid having to call findViewById().
        ListViewHolder viewHolder = (ListViewHolder) convertView.getTag();

        textView = viewHolder.getTextView() ;

      }


      textView.setText( part_array_list.get(position) );      

      return convertView;


      }
      }
        /** Holds child views for one row. */
           private class ListViewHolder {

 private TextView textView ;
 public ListViewHolder() {}
 public ListViewHolder( TextView textView ) {

   this.textView = textView ;
 }

 public TextView getTextView() {
   return textView;
 }
 public void setTextView(TextView textView) {
   this.textView = textView;
 }    

}

在 OnCreate() 方法中,

and in OnCreate() method,

         final ArrayAdapter<String> part_list_adapter=new ItemsAdapter(AssetSearch.this, part_array_list);
        //PartNumber_List.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    PartNumber_List.setAdapter(part_list_adapter);

    PartNumber_List.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position,
            long id) {
         ListViewHolder viewHolder = (ListViewHolder) v.getTag();
         viewHolder.getTextView().setBackgroundColor(R.color.result_image_border);

        String item=(String) part_list_adapter.getItem((int) id);
        });

推荐答案

这里的问题是您正在为视图设置背景颜色,然后当您滚动时,由于使用了 convertView,您最终会重复使用该视图.这正是你应该做的,干得好.

The issue here is that you are setting the background color to the view, then when you scroll, you end up reusing that view due to using convertView. This is exactly what you should be doing, so good job there.

但是,当然,这意味着列表项在不应该被选中时被选中.为了解决这个问题,您的 getView() 方法需要将背景颜色重置为其默认值.我不知道最初的颜色是什么,但我假设它是透明的.所以你会输入:

But, of course, that means list items are selected when they shouldn't be. In order to fix this, your getView() method needs to reset the background color to its default value. I don't know what the color was originally, but I'll assume it was transparent. So you would put:

textView.setBackgroundColor(android.R.color.transparent);

所以现在您将背景颜色设置为其默认值,但是如果您从所选项目滚动离开,然后再回到它,它将具有透明背景而不是所选背景.要解决此问题,请将 Integer 值的 ArrayList 放入您的适配器类中.当一个项目被点击并触发 onItemClick() 时,将项目位置添加到该 ArrayList.例如,假设您有:

So now you're setting the background color to its default, but if you scroll away from the selected item, then back to it, it will have a transparent background instead of the selected background. To resolve this, put an ArrayList of Integer values inside your adapter class. When an item is clicked and onItemClick() is triggered, add the item position to that ArrayList. For example, say you have:

public ArrayList<Integer> selectedIds = new ArrayList<Integer>();

在您的适配器类中.然后,您的 onItemClick 方法将如下所示:

in your adapter class. Then, your onItemClick method would look like this:

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

     ArrayList<Integer> selectedIds = ((ItemsAdapter) parent).selectedIds;
     Integer pos = new Integer(position);
     if(selectedIds.contains(pos) {
         selectedIds.remove(pos);
     }
     else {
         selectedIds.add(pos);
     }

     parent.notifyDataChanged();
}

所以,最后,在您的 getView() 方法中,添加以下行:

So, finally, in your getView() method, add this line:

textView.setBackground(selectedIds.contains(position) ? R.color.result_image_border : androi.R.color.transparent);  

这篇关于从列表视图中选择多个项目并仅更改所选项目的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆