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.
问问题
810 次
3 回答
0
使用完整的源代码检查此自定义 ListView 项的选择颜色如何更改列表视图项的背景颜色。
于 2012-10-06T09:58:33.373 回答
0
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;
}
于 2012-10-06T11:10:27.823 回答
0
yourview = v;
v.setBackgroundResource(R.color.transparent_green);
于 2012-10-06T09:53:35.643 回答