简体   繁体   English

ListView中列表项的背景色

[英]Background Color of a list item in ListView

I am creating a audio player in android and showing a list of songs in a list view. 我在android中创建音频播放器,并在列表视图中显示歌曲列表。 I wants to change background color of currently playing song in list view. 我想更改列表视图中当前正在播放的歌曲的背景颜色。 The song is changed when song is completed and in this case set background color of this song and remove background color from previous selected song in the list. 歌曲完成后会更改歌曲,在这种情况下,请设置该歌曲的背景颜色,并从列表中先前选择的歌曲中删除背景颜色。

                yourview = v;
                v.setBackgroundResource(R.color.transparent_green);

请使用完整的源代码检查此Customized ListView项目的选择颜色如何更改列表视图项目的背景颜色。

package com.finallys.sandy;

import java.util.ArrayList;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    ArrayList<String> arr = new ArrayList<String>();

    public HelloBase mAdapter;

    LayoutInflater li;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView mListView = new ListView(this);
        li = getLayoutInflater();

        for (int i = 0; i < 100; i++) {
            arr.add("this is my song" + i);
        }
        mAdapter = new HelloBase();
        mListView.setAdapter(mAdapter);
        setContentView(mListView);
    }

    class HelloBase extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arr.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        class ViewHolder {
            TextView txt;
        }

        ViewHolder vhHolder;

        @SuppressWarnings("null")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            vhHolder = new ViewHolder();
            if (convertView == null) {
                convertView = li.inflate(R.layout.activity_main, null);
                vhHolder.txt = (TextView) convertView
                        .findViewById(R.id.webView1);
                convertView.setTag(vhHolder);
            } else {
                vhHolder = (ViewHolder) convertView.getTag();
            }
            vhHolder.txt.setText(arr.get(position));
            vhHolder.txt.setOnClickListener(new OnMyClick(vhHolder.txt));
            return convertView;
        }

        class OnMyClick implements OnClickListener {

            TextView txt;

            OnMyClick(TextView txt) {
                this.txt = txt;
            }

            @Override
            public void onClick(View v) {
                txt.setTextColor(Color.RED);
                mAdapter.notifyDataSetChanged();

                if (preTextView != null)
                    preTextView.setTextColor(Color.BLACK);

                preTextView = txt;

            }

        }
    }

    TextView preTextView;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM