如何使用自定义适配器在listview的特定行上设置颜色

时间:2013-12-08 08:00:41

标签: android colors android-custom-view

![我正在使用自定义适配器进行列表视图,其中我有三个项目文本,按钮和单选按钮。我可以在单选按钮的帮助下一次只选择一行。现在我想要的,当我选择使用单选按钮的行应该将所选单选按钮的特定行设置为某种颜色。这是我的自定义适配器代码,其中包含所有项目。

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]

3 个答案:

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

修改

试试这个适配器类。在这个你有适配器中的selectedItemIndex,你每次选择一个列表项时都会更新并通知你的适配器,在getView中检查position是否等于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;
     }

}

答案 1 :(得分:0)

我相信你onClick(View v)方法中遗漏的所有内容都是:

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

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

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

编辑#2:你应该检查this answer

答案 2 :(得分:0)

一个简单的解决方案是在适配器中创建一个名为:

的自定义方法
public void setChosenPosition(int position){
    this.chosenPosition = position;
}

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

当然在监听器中,您调用方法setChosenPosition。我只是在听活动,但适配器也很好。