【问题标题】:Change ListView Item Background on click - Android点击时更改 ListView 项目背景 - Android
【发布时间】:2012-05-23 05:24:55
【问题描述】:

我已经使用 BaseAdapter 准备了一个自定义列表视图。现在我想在单击事件上更改列表视图的选定项目的颜色。并且应该选择多个项目。这里我给出一个演示:

所选项目的颜色是橙色。这只是一个演示屏幕。如果有人知道如何更改所选列表项的整个背景颜色,请发布他们的评论。谢谢。

我正在使用这个 BaseAdapter 类:

public class MyListAdapter extends BaseAdapter {

private Activity activity;
private String[] title, artist, duration, rowNumber;
private static LayoutInflater inflater=null;
ViewHolder holder;

View vi;

public MyListAdapter (Activity context, String[] songTitle,String[] songArtist, String[] songDuration )
{
   try
   {
    activity = context;
    title = songTitle;
    artist = songArtist;
    duration = songDuration;

    rowNumber = new String[title.length];
    for(int i=0;i<title.length; i++){
        rowNumber[i] = Integer.toString(i+1);
    }
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   }
   catch (NullPointerException e) 
   {
       e.printStackTrace();
   }
}

public int getCount() {
    return title.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    private TextView txtSongNumber, txtSongTitle, txtSongArtist, txtSongDuration;

}

public View getView(int position, View convertView, ViewGroup parent) 
{
    try
    {
        vi=convertView;

       // System.out.println("Value of position"+position);
        if(convertView==null)
        {
            vi = inflater.inflate(R.layout.list_songs, null);
            holder=new ViewHolder();
            holder.txtSongNumber = (TextView)vi.findViewById(R.id.txtSongNumber);
            holder.txtSongTitle = (TextView)vi.findViewById(R.id.txtSongTitle);
            holder.txtSongArtist = (TextView)vi.findViewById(R.id.txtSongArtist);
            holder.txtSongDuration = (TextView)vi.findViewById(R.id.txtSongDuration);

            vi.setTag(holder);
        }
        else

            holder=(ViewHolder)vi.getTag();

        holder.txtSongNumber.setText(rowNumber[position]);
        holder.txtSongTitle.setText(title[position]);
        holder.txtSongArtist.setText(artist[position]);

        holder.txtSongDuration.setText(duration[position]);

    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    return vi;
}

}

【问题讨论】:

标签: android


【解决方案1】:

很简单...试试下面的代码...

在您的列表适配器中:

先定义一个整数数组

ArrayList<Integer> itemPos = new ArrayList<Integer>();

然后在您的 getView 方法中使用此代码:

        if (itemPos.contains(position)) {
            holder.txtOne.setTextColor(Color.BLUE);
        } else {
            holder.txtOne.setTextColor(Color.WHITE);
        }

现在在文本视图的点击事件中使用此代码:

            if (!itemPos.contains(position)) {
                holder.txtOne.setTextColor(Color.BLUE);
                itemPos.add(position);
                notifyDataSetChanged();
            } else {
                holder.txtOne.setTextColor(Color.WHITE);
                notifyDataSetChanged();
                int po = itemPos.indexOf(position);
                itemPos.remove(po);
            }

【讨论】:

    【解决方案2】:

    由于您已经实现了 BaseAdapter,它有一个名为 getView 的核心方法,您可以轻松地将项目的状态存储在 BaseAdapter 中。例如,您可以使用 List 来存储状态。

    然后,应该实现 ListView 的侦听器,如http://developer.android.com/reference/android/widget/AdapterView.html#setOnClickListener(android.view.View.OnClickListener 中所述)。 OnItemClickListener 的文档在这里,http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html,通过它可以获取点击的位置。

    最后,在点击事件后改变item的状态,在adapter中调用notifyDataSetChanged()来通知数据已经改变,你会看到更新后的selected items。

    【讨论】:

    • 非常感谢您的帮助。我已经完成了这一切。现在我发现当我点击列表中的一个项目时,另一个项目也会被自动选中。 BaseAdapter 有问题吗?
    • 不,BaseAdapter 运行良好。但是您需要检查您的代码以确保您使用了正确的 OnItemClickListener 参数。在 OnItemClickListener 中,position 表示“视图在适配器中的位置”,如文档中所述。
    • 我正在获得选定项目的位置。这是正确的。但是为什么我只选择了一项却选择了不止一项呢?
    • 您可以尝试使用 Log.w 打印 onItemClick(...) 被调用的次数,以及被点击项目的位置。
    【解决方案3】:

    “非常感谢您的帮助。我已经完成了所有这些。现在我发现当我单击列表中的一个项目时,另一个项目也会被自动选中。BaseAdapter 有什么问题吗?”

    对于这个问题,您需要保存列表视图行的状态,即在 getview 中检查该行是否被选中,然后将行的颜色设置为选中状态。每当您选择或取消选择任何行时,您也需要保存状态。这是一个类似的例子,而不是复选框,行会在你的情况下......希望它会帮助你我给你一个链接......

    How to implement a button that gets all checkbox's state and adds the value of checked item into arraylist?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 2012-03-10
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多