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

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

问题描述

我想打一个观点,我可以从列表视图中选择多个项目,也并排正在改变选择的列表项的颜色和保存该项目到我arraylist..My列表如下图所示的:

但是,当我用滚动它..这是显示我选择了1个项目,即使我不选择它喜欢的:

不过,我想只有那个列表项的颜色应该改变,我会点击...

我现在用的是code为:

 私有类ItemsAdapter扩展ArrayAdapter<字符串> {
 名单<字符串>项目;
  上下文语境;
  私人LayoutInflater充气;
  公共ItemsAdapter(上下文的背景下,列表与LT;字符串> part_array_list){
     超(背景下,R.layout.part_list,R.id.label,part_array_list);

     充气= LayoutInflater.from(上下文);
  }

  @覆盖
  公共查看getView(INT位置,查看convertView,ViewGroup中父){
      的TextView TextView的;
    字符串项=(字符串)this.getItem(位置);

      如果(convertView == NULL){
        convertView = inflater.inflate(R.layout.part_list,NULL);

        //找到孩子的意见。
        TextView的=(的TextView)convertView.findViewById(R.id.label);


        //优化:标记的行与它的孩子的意见,所以我们不必
        //调用findViewById()后,当我们再次使用该行。
        convertView.setTag(新ListViewHolder(TextView中));


      }
      //重用现有行观点
      其他 {
        //因为我们使用ViewHolder,我们避免调用findViewById()。
        ListViewHolder viewHolder =(ListViewHolder)convertView.getTag();

        的TextView = viewHolder.getTextView();

      }


      textView.setText(part_array_list.get(位置));

      返回convertView;


      }
      }
        / **存放子视图一行。 * /
           私有类ListViewHolder {

 私人的TextView TextView的;
 公共ListViewHolder(){}
 公共ListViewHolder(TextView中的TextView){

   this.textView = TextView的;
 }

 公众的TextView getTextView(){
   返回的TextView;
 }
 公共无效setTextView(TextView中的TextView){
   this.textView = TextView的;
 }
 

}

和中的OnCreate()方法,

 最后ArrayAdapter<字符串> part_list_adapter =新ItemsAdapter(AssetSearch.this,part_array_list);
        //PartNumber_List.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    PartNumber_List.setAdapter(part_list_adapter);

    PartNumber_List.setOnItemClickListener(新OnItemClickListener(){

    @覆盖
    公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,
            长ID){
         ListViewHolder viewHolder =(ListViewHolder)v.getTag();
         。viewHolder.getTextView()setBackgroundColor(R.color.result_image_border);

        字符串项=(字符串)part_list_adapter.getItem((INT)ID);
        });
 

解决方案

这里的问题是,你设置背景颜色的观点,那么当你滚动,你最终重用这一观点,由于使用 convertView 。这正是你应该做的事情,所以好工作。

但是,当然,这意味着列表项被选择时,他们不应该。为了解决这个问题,你的 getView()方法需要重新设置背景色为默认值。我不知道这个颜色是什么样的,可是我会认为它是透明的。所以,你会放:

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

所以,现在你设置的背景色为默认,但如果你所选择的项目滚动离开,然后回去吧,它将有一个透明的背景,而不是选择的背景。要解决这个问题,把一个的ArrayList 整数适配器类中。当一个项目被点击和 onItemClick()被触发,添加项目的位置到的ArrayList 。例如,假设你有:

 公开的ArrayList<整数GT; selectedIds =新的ArrayList<整数GT;();
 

在你的适配器类。然后,你的 onItemClick 的方法是这样的:

  @覆盖
公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){

     ArrayList的<整数GT; selectedIds =((ItemsAdapter)母公司).selectedIds;
     整数POS =新的整数(位置);
     如果(selectedIds.contains(POS){
         selectedIds.remove(POS);
     }
     其他 {
         selectedIds.add(POS);
     }

     parent.notifyDataChanged();
}
 

所以,最后,在你的 getView()的方法,加入这一行:

  textView.setBackground(selectedIds.contains(位置)R.color.result_image_border:androi.R.color.transparent);
 

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:

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...

I am using the code as:

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

}

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

解决方案

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.

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

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>();

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

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天全站免登陆