简体   繁体   中英

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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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