【问题标题】:how to set a color on particular row of listview using custom adapter如何使用自定义适配器在列表视图的特定行上设置颜色
【发布时间】:2013-12-08 08:00:41
【问题描述】:

![我正在为一个列表视图使用自定义适配器,其中我有三个项目文本、按钮和单选按钮。借助单选按钮,我一次只能选择一行。现在我想要什么,当我选择使用单选按钮的行 所选单选按钮的特定行应设置为某种颜色。这是我的自定义适配器代码,其中所有项目都在那里。

package com.pramod.customlistviewwithradiobutton;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends ArrayAdapter<Item> {

     private final Context context;
     private boolean userSelected = false;
     private RadioButton mCurrentlyCheckedRB;
     private final ArrayList<Item> itemList;

     public CustomAdapter(Context context, ArrayList<Item> itemList) {

         super(context, R.layout.row_item, itemList);

         this.context = context;
         this.itemList = itemList;
     }

     @Override
     public View getView(final int position, final View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        // Item rowItem = getItem(position);

         // 1. Create inflater 
         LayoutInflater inflater = (LayoutInflater) context
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         // 2. Get rowView from inflater
          View rowView = inflater.inflate(R.layout.row_item, parent, false);

         // 3. Get the two text view from the rowView
         Button btn = (Button) rowView.findViewById(R.id.button1);
         TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
         TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
         RadioButton radio = (RadioButton) rowView.findViewById(R.id.radioButton1);

         // 4. Set the text for textView 
         tv1.setText(itemList.get(position).getName());
         tv2.setText(itemList.get(position).getAddress());
         System.out.println(""+getCount());

         if (position == getCount() - 1 && userSelected == false) {
          //   radio.setChecked(true);
             mCurrentlyCheckedRB = radio;
         } else {
             radio.setChecked(false);
         }
         btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "b "+position, Toast.LENGTH_LONG).show();

            }
        });
         radio.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {

                 if (mCurrentlyCheckedRB != null) {
                     if (mCurrentlyCheckedRB == null)
                         mCurrentlyCheckedRB = (RadioButton) v;
                     mCurrentlyCheckedRB.setChecked(true);

                     Toast.makeText(context, ""+position, Toast.LENGTH_LONG).show();

                     convertView.setBackgroundColor(Color.BLUE);
                 }

                 if (mCurrentlyCheckedRB == v)
                     return;

                 mCurrentlyCheckedRB.setChecked(false);

                 ((RadioButton) v).setChecked(true);
                 mCurrentlyCheckedRB = (RadioButton) v;
             }
         });

         return rowView;
     }

}
Here ,Note i don't have to put onclickitem on listview ,because i have a click on button and textview.][1]

【问题讨论】:

    标签: android colors android-custom-view


    【解决方案1】:

    试试下面的代码: 在获取视图中,将背景设置为 rowView 而不是 convertView,因为从 getView 返回的视图就是显示的视图。

     @Override
         public View getView(final int position, final View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
    
            // Item rowItem = getItem(position);
    
            final View rowView;
             // 1. Create inflater 
             if(convertView==null){
             LayoutInflater inflater = (LayoutInflater) context
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
             // 2. Get rowView from inflater
              rowView = inflater.inflate(R.layout.row_item, parent, false);
            }else{
              rowView=convertView;
            }
             // 3. Get the two text view from the rowView
             Button btn = (Button) rowView.findViewById(R.id.button1);
             TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
             TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
             RadioButton radio = (RadioButton) rowView.findViewById(R.id.radioButton1);
    
             // 4. Set the text for textView 
             tv1.setText(itemList.get(position).getName());
             tv2.setText(itemList.get(position).getAddress());
             System.out.println(""+getCount());
    
             if (position == getCount() - 1 && userSelected == false) {
              //   radio.setChecked(true);
                 mCurrentlyCheckedRB = radio;
             } else {
                 radio.setChecked(false);
             }
             btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "b "+position, Toast.LENGTH_LONG).show();
    
                }
            });
             radio.setOnClickListener(new OnClickListener() {
    
                 @Override
                 public void onClick(View v) {
    
                     if (mCurrentlyCheckedRB != null) {
                         if (mCurrentlyCheckedRB == null)
                             mCurrentlyCheckedRB = (RadioButton) v;
                         mCurrentlyCheckedRB.setChecked(true);
    
                         Toast.makeText(context, ""+position, Toast.LENGTH_LONG).show();
    
                         rowView.setBackgroundColor(Color.BLUE);
                     }
    
                     if (mCurrentlyCheckedRB == v)
                         return;
    
                     mCurrentlyCheckedRB.setChecked(false);
    
                     ((RadioButton) v).setChecked(true);
                     mCurrentlyCheckedRB = (RadioButton) v;
                 }
             });
    
             return rowView;
         }
    

    编辑

    试试这个适配器类。在此,您在适配器中选择了ItemIndex,每次选择列表中的一项时您都会更新它并通知您的适配器,并在getView中检查位置是否等于 selectedItemIndex 然后设置蓝色,否则设置默认颜色。

    import java.util.ArrayList;
    
    import android.content.Context;
    import android.graphics.Color;
    import android.graphics.Point;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class CustomAdapter extends ArrayAdapter<Item> {
    
         private final Context context;
         private boolean userSelected = false;
         private RadioButton mCurrentlyCheckedRB;
         private final ArrayList<Item> itemList;
         private int selectedItemIndex=-1;
    
         public CustomAdapter(Context context, ArrayList<Item> itemList) {
    
             super(context, R.layout.row_item, itemList);
    
             this.context = context;
             this.itemList = itemList;
         }
    
     @Override
         public View getView(final int position, final View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
    
            // Item rowItem = getItem(position);
    
            final View rowView;
             // 1. Create inflater 
             if(convertView==null){
             LayoutInflater inflater = (LayoutInflater) context
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
             // 2. Get rowView from inflater
              rowView = inflater.inflate(R.layout.row_item, parent, false);
            }else{
              rowView=convertView;
            }
             // 3. Get the two text view from the rowView
             Button btn = (Button) rowView.findViewById(R.id.button1);
             TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
             TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
             RadioButton radio = (RadioButton) rowView.findViewById(R.id.radioButton1);
    
             if(position==selectedItemIndex){
                 rowView.setBackgroundColor(Color.BLUE);
                         radio.setChecked(true);//Check here
             }else{
                 rowView.setBackgroundColor(Color.WHITE);//Color when not selected
                         radio.setChecked(false);//Uncheck here
             }
             // 4. Set the text for textView 
             tv1.setText(itemList.get(position).getName());
             tv2.setText(itemList.get(position).getAddress());
             System.out.println(""+getCount());
    
             if (position == getCount() - 1 && userSelected == false) {
              //   radio.setChecked(true);
                 mCurrentlyCheckedRB = radio;
             } else {
                 radio.setChecked(false);
             }
             btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "b "+position, Toast.LENGTH_LONG).show();
    
                }
            });
             radio.setOnClickListener(new OnClickListener() {
    
                 @Override
                 public void onClick(View v) {
    
                     if (mCurrentlyCheckedRB != null) {
                         if (mCurrentlyCheckedRB == null)
                             mCurrentlyCheckedRB = (RadioButton) v;
                         mCurrentlyCheckedRB.setChecked(true);
    
                         Toast.makeText(context, ""+position, Toast.LENGTH_LONG).show();
                         selectedItemIndex=position;
                 CustomAdapter.this.notifyDataSetChanged();
                     }
    
                     if (mCurrentlyCheckedRB == v)
                         return;
    
                     mCurrentlyCheckedRB.setChecked(false);
    
                     ((RadioButton) v).setChecked(true);
                     mCurrentlyCheckedRB = (RadioButton) v;
                 }
             });
    
             return rowView;
         }
    
    }
    

    【讨论】:

    • 谢谢,该行正在着色,但正如我在问题中提到的,它需要根据单选按钮一次只为一行着色。每当单击其他单选按钮时,应删除该行中的先前颜色。在这种情况下不会发生这种情况。
    • 感谢@vipul它正在工作,但是有一点问题,当行选择颜色时,单选按钮没有被选中。
    • 在您的 getView 中,您也可以对其进行检查。查看另一个编辑
    【解决方案2】:

    一个简单的解决方案是在您的适配器中创建一个自定义方法,称为:

    public void setChosenPosition(int position){
        this.chosenPosition = position;
    }
    

    然后在getView中,检查是否chosenPosition == position,然后用它来设置特定的背景。

    当然,在侦听器中,您调用方法setChosenPosition。我只会在活动中收听,但在适配器中也可以。

    【讨论】:

      【解决方案3】:

      我相信您在 onClick(View v) 方法中缺少的只是:

      View parentView = (View) v.getParent();
      parentView.setBackgroundColor(Color.BLUE);
      

      而不是 convertView.setBackgroundColor(Color.BLUE) ,即使它在 getView() 方法中,它也无法访问其参数。

      编辑:您还需要检查特定单选按钮何时变为未选中状态,以便使用相同的过程将背景颜色恢复为默认值。

      EDIT#2:你应该检查this answer

      【讨论】:

      • 如果radioRadioGroup 绑定请注意,那么您需要将(View)((View)v.getParent()).getParent() 放在第一行以获得适当的布局。
      • 我没有单选组,只有一个单选按钮。
      • 你可以告诉我如何检查这个代码中的条件,只有一个单选按钮,那么只应当拍摄颜色,只要检查另一个单选按钮,前一个是丢弃。
      • 在 Edit#2 中添加了一个链接
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      相关资源
      最近更新 更多