我已经prepared使用BaseAdapter的自定义列表视图。现在我想改变列表视图中选择项目的色单击事件。和多个项目应选择。在这里,我给一个演:
I've prepared a custom listview using BaseAdapter. Now I want to change color of selected item of listview on click event. And multiple items should be selected. Here I am giving one demo :
选择的项目的颜色为橙色。这仅仅是一个演示画面。如果有人知道如何更改选择的列表项的整个背景颜色,那么请发表自己的评论。谢谢你。
Selected item's color is Orange. This is just a demo screen. If anybody knows to how change entire background color of a selected list item then please post their reviews. Thanks.
我用这BaseAdapter类:
I am using this BaseAdapter Class:
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;
}
}
推荐答案
它非常简单...只是尝试以下code ...
Its very simple... Just try the following code...
在您的列表适配器:
首先定义一个整数数组
ArrayList<Integer> itemPos = new ArrayList<Integer>();
然后用这个code在getView方法:
then use this code in your getView Method :
if (itemPos.contains(position)) {
holder.txtOne.setTextColor(Color.BLUE);
} else {
holder.txtOne.setTextColor(Color.WHITE);
}
现在使用code在你的文本视图点击事件:
Now use this code in click event of your Text View :
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);
}
相关推荐
最新文章