在我的Android应用程序用户中,只选择listview中的一个项目,并更改该特定项目textview颜色。如果用户在列表视图中选择其他项目,则上一项目颜色设置为默认值 我在用户选择时使用以下方法重置所有textview颜色,但当滚动随机文本颜色时,如果您有任何其他解决方案可以解决此问题,请使用
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
但getchildcount方法仅返回当前显示项目textview :-(
for(int i=0;i<lv.getChildCount();i++)
{
Log.i("text",((TextView)((LinearLayout)lv.getChildAt(i)).findViewById(R.id.TruckNumber)).getText()+"");
((TextView)((LinearLayout)lv.getChildAt(i)).findViewById(R.id.TruckNumber)).setTextColor(Color.LTGRAY);
}
((TextView)((LinearLayout)arg1).findViewById(R.id.TruckNumber)).setTextColor(Color.parseColor("#2a9bc8"));
changeHeader(0);
((LinearLayout)lv.getChildAt(0)).refreshDrawableState();
}
});
customAdapter
public class Search_Truck_Number_adapter扩展了BaseAdapter {
private static ArrayList<ListitemDetail> itemDetailarraylist;
private Context context=null;
LayoutInflater inflater;
ArrayList<TextView> TextViewObjects=new ArrayList<TextView>();
public Search_Truck_Number_adapter(ArrayList<ListitemDetail> result,Context c) {
itemDetailarraylist=result;
inflater = LayoutInflater.from(c);
context=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemDetailarraylist.size();
}
@Override
public Object getItem(int arg0) {
return itemDetailarraylist.get(arg0);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
convertView = inflater.inflate(R.layout.search_act_cell,null);
holder = new ViewHolder();
holder.txt_itemName = (TextView)convertView.findViewById(R.id.TruckNumber);
holder.position=position;
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
TextViewObjects.add(holder.txt_itemName);
holder.txt_itemName.setText(itemDetailarraylist.get(position).getName());
return convertView;
}
static class ViewHolder{
TextView txt_itemName;
Integer position;
}
}
答案 0 :(得分:1)
public class Search_Truck_Number_adapter extends BaseAdapter implements OnClickListener
{
private Integer no=null;
private static ArrayList<ListitemDetail> itemDetailarraylist;
private Context context=null;
LayoutInflater inflater;
public Search_Truck_Number_adapter(ArrayList<ListitemDetail> result,Context c) {
itemDetailarraylist=result;
inflater = LayoutInflater.from(c);
context=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemDetailarraylist.size();
}
@Override
public Object getItem(int arg0) {
return itemDetailarraylist.get(arg0);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
convertView = inflater.inflate(R.layout.search_act_cell,null);
holder = new ViewHolder();
holder.txt_itemName = (TextView)convertView.findViewById(R.id.TruckNumber);
holder.position=position;
convertView.setOnClickListener(this);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.position=position;
holder.txt_itemName.setText(itemDetailarraylist.get(position).getName());
if(no!=null)
{
if(no!=position)
{
holder.txt_itemName.setTextColor(Color.LTGRAY);
}
else
{
holder.txt_itemName.setTextColor(Color.parseColor("#2a9bc8"));
}
}
return convertView;
}
static class ViewHolder{
TextView txt_itemName;
Integer position;
Integer Select_Position;
}
@Override
public void onClick(View v) {
SearchActivity sa=new SearchActivity();
sa.changeHeader(0, context);
ListView lv=(ListView) v.getParent();
for(int i=0;i<lv.getChildCount();i++)
{
((TextView)((LinearLayout)lv.getChildAt(i)).findViewById(R.id.TruckNumber)).setTextColor(Color.LTGRAY);
}
no=((ViewHolder)v.getTag()).position;
((ViewHolder)v.getTag()).txt_itemName.setTextColor(Color.parseColor("#2a9bc8"));
Log.i("Position", ((ViewHolder)v.getTag()).position+"");
}
}
答案 1 :(得分:0)
因为getChildAt()仅适用于屏幕上显示的列表中的视图,所以可以先调用lv.getFirstVisiblePostion()(这是一个类的android.widget.AdapterView方法,ListView间接扩展(参见ListView)在android开发人员指南并查看继承树)),然后如果你知道视图相对于它在屏幕上的位置的索引,你可以将索引添加到getFirstVisiblePosition()的结果,例如getFirstVisiblePosition()+ index 。这将为您提供与整个列表相关的视图位置,而不仅仅是屏幕上显示的内容。您可以使用它来更改文本颜色。 - 希望这有帮助