我正在开发一个包含项目列表的应用程序,并且从列表视图中选择特定项目,我想要更改所选整行的背景颜色,但在实现这一点时,我的所有行背景颜色都已更改。请有人帮帮我感谢Advace。这是我的Adaptor.xml
public class Adaptor_ListItem extends ArrayAdapter<MyItem> {
public Context mContext;
public ArrayList<MyItem> listItem;
public LayoutInflater inflater;
public int position1=-1;
public Adaptor_ListItem(Context context, int resource, List<MyItem> list,
int selectedPos) {
super(context, resource, list);
// TODO Auto-generated constructor stub
mContext = context;
listItem = (ArrayList<MyItem>) list;
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
position1 = selectedPos;
} // method ends
public View getView(int pos, View convertView, ViewGroup parent) {
View holder = convertView;
if (holder == null) {
holder = inflater.inflate(R.layout.adaptor_itemlist, null);
}
if (listItem.size() != 0) {
TextView txtName = (TextView) holder
.findViewById(R.id.ListItem_txtName);
TextView txtDays = (TextView) holder
.findViewById(R.id.ListItem_txtDays);
TextView txtRecurring = (TextView) holder
.findViewById(R.id.ListItem_txtRecuring);
MyItem objItem = listItem.get(pos);
if (objItem != null) {
String strName = objItem.itemName;
String strRecurring = objItem.recurring + "";
String strDays = objItem.days;
int itemId = objItem.itemId;
// for checking which item has notified
if ((pos== position1) && holder!=null ) {
LinearLayout linearLayout = (LinearLayout) holder
.findViewById(R.id.linearListItem);
txtName.setBackgroundColor(Color.RED);
txtDays.setBackgroundColor(Color.RED);
txtRecurring.setBackgroundColor(Color.RED);
System.out.println("Adaptor_ListItem.getView()//// method block");
}
if (strDays == null) {
txtName.setText(strName);
txtDays.setText("0 Days");
txtRecurring.setText(strRecurring + "");
} else {
txtName.setText(strName);
txtDays.setText(strDays + " Days");
txtRecurring.setText(strRecurring + "");
}
}
}
return holder;
}// method ends
} // final class ends
答案 0 :(得分:0)
请在adaptor_itemlist.xml
文件中提及主要布局的背景。
为背景编写选择器:
item_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/red"></item>
<item android:state_selected="false" android:drawable="@android:color/white"></item>
</selector>
将此文件应用于adapter_itemlist.xml
使用以下代码:
linearLayout.setSelected(true);
而不是:
txtName.setBackgroundColor(Color.RED);
txtDays.setBackgroundColor(Color.RED);
txtRecurring.setBackgroundColor(Color.RED);
答案 1 :(得分:0)
在自定义适配器的getview中添加此行。
listTextView.setTextColor(Color.parseColor("#aaaaaa"));
答案 2 :(得分:0)
使用以下代码代替您的代码:
public View getView(int pos, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder Holder;
if (v == null) {
v = inflater.inflate(R.layout.adaptor_itemlist, null);
Holder = new ViewHolder();
Holder.txtName = (TextView) v
.findViewById(R.id.ListItem_txtName);
Holder.txtDays = (TextView) v
.findViewById(R.id.ListItem_txtDays);
Holder.txtRecurring = (TextView) v
.findViewById(R.id.ListItem_txtRecuring);
MyItem objItem = listItem.get(pos);
v.setTag(Holder);
}
else
holder=(ViewHolder)v.getTag();
if (objItem != null) {
String strName = objItem.itemName;
String strRecurring = objItem.recurring + "";
String strDays = objItem.days;
int itemId = objItem.itemId;
// for checking which item has notified
if ((pos== position1)) {
LinearLayout linearLayout = (LinearLayout) v
.findViewById(R.id.linearListItem);
Holder.txtName.setBackgroundColor(Color.RED);
Holder.txtDays.setBackgroundColor(Color.RED);
Holder.txtRecurring.setBackgroundColor(Color.RED);
System.out.println("Adaptor_ListItem.getView()//// method block");
}
else
{
// set Default Value
}
if (strDays.equals(null)) {
Holder.txtName.setText(strName);
Holder.txtDays.setText("0 Days");
Holder.txtRecurring.setText(strRecurring + "");
} else {
Holder.txtName.setText(strName);
Holder.txtDays.setText(strDays + " Days");
Holder.txtRecurring.setText(strRecurring + "");
}
}
return v;
}// method ends
} // final class ends
和ViewHolder类是:
public static class ViewHolder()
{
public TextView txtName , txtDays, txtRecurring;
}
答案 3 :(得分:0)
int [] arrayColors={Color.GREEN,Color.BLUE,Color.Red}
此适配器的getview方法中的以下行调用。
yourView.setBackgroundColor(Color.parseColor(arrayColors[pos]));
答案 4 :(得分:0)
当(pos!= position1)..或if(pos = position1)语句的else子句时,需要将背景颜色设置回原始颜色。现在发生的事情是,一旦你将列表项设置为红色,它就会被回收到另一行,但它会保持红色,因为它在新的getView()调用中从未被设置过。
int feedbackColor = Color.WHITE; // or whatever your base color is
if ((pos== position1) && holder!=null ) {
LinearLayout linearLayout = (LinearLayout) holder
.findViewById(R.id.linearListItem);
feedbackColor = Color.RED;
}
txtName.setBackgroundColor(feedbackColor);
txtDays.setBackgroundColor(feedbackColor);
txtRecurring.setBackgroundColor(feedbackColor);