English 中文(简体)
点击 更改列表视图项目背景 - 和机器人
原标题:Change ListView Item Background on click - Android
  • 时间:2012-05-23 05:24:55
  •  标签:
  • android

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

""https://i.sstatic.net/WW4vX.png" alt="此处输入图像描述"/"

选中项的颜色是 Orange 。 这只是一个演示屏幕。 如果有人知道如何改变选中列表项的全部背景颜色, 请在浏览后加注 。 谢谢 。

我使用这个基础设计师类:

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

时 时

最佳回答

很简单 试试下面的代码

在您的列表适配器中 :

首先定义整数组

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

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

        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);
            }
问题回答

自从您已经实施了基础设计师, 其核心方法叫做“获取 View ”, 您可以很容易地在基础设计师中存储项目状态。 例如, 您可以使用 List 存储状态 。

然后,应当落实ListView的听众,正如以下文件所描述的:)。关于LiotclickListener的文件, < a href=http://developinger.android.com/android/widget/AdapterView.Ontlemleclicker.html ,你可以通过它获得点击的位置。

最后,在点击事件后更改项目状态,并在适配器中调用通知DataSetChanged ()通知数据已更改,然后查看经更新的选定项目。

“非常感谢您的帮助。 我做了这一切。 现在我发现当我点击列表中的一个项目时, 另一个项目也会自动被选中。 与 BaseAdapter 有任何问题吗?”

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

< a href=> https://stackoverflow.com/ questions/10190083/ how-to-taminment- a- button- thatt-gets-all-checkboxs-state-and-adds-value-f-c/101369#101969_101369> 如何执行一个按钮, 该按钮将所有复选框状态都接收到状态, 并将检查过的项目添加到数组列表中?





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签