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

标签 android colors android-custom-view

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

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]

最佳答案

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

 @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 中,您检查位置是否等于 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;
     }

}

关于android - 如何使用自定义适配器在 ListView 的特定行上设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20451156/

相关文章:

android - 如何创建 EAN13 条形码生成器,将 EAN 作为输入?

android - Android,libgdx,声音问题

python - 有没有办法以编程方式将二进制文件转换为二维码?

colors - JavaFX-如何为形状填充颜色?

r - ggplot2:更改geom_segment 甘特图中段的颜色?

android用颜色设置 ListView 的特定行背景

java - 使用 OpenCV java 包装器的 SVD solvez

android - 在 Android 中使用大尺寸可绘制对象和自定义 View

android - 使用通知计数指示器制作 "badge"自定义 View

android - 通过代码设置Android动态壁纸,无需用户交互