我正在开发一个项目,其中有一个自定义列表视图,每行有四个TextView。当我点击textview时,我想要更改textview的颜色。我已经尝试过如下所示的代码:< / p>
screenlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final TextView slot1 = (TextView) view.findViewById(R.id.slot1);;
slot1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slot1.setTextColor(Color.RED);
}
});
}
});
问题是需要两次点击才能设置颜色。如何确保在第一次点击时更改颜色?。
编辑:我在下面添加了适配器代码:
public class ScreenListAdapter extends BaseAdapter {
List<ScreensDataModel> list_screeen;
Context con;
LayoutInflater inflater;
ViewHolder vh;
ImageLoader imageLoader;
List<SlotsDataModel> slot_list;
public ScreenListAdapter(List<ScreensDataModel> list_screeen, Context con) {
this.con=con;
this. list_screeen= list_screeen;
inflater=(LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = AppController.getInstance().getImageLoader();
}
@Override
public int getCount() {
return list_screeen.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView=inflater.inflate(R.layout.screens_view_style, null);
vh=new ViewHolder();
vh.screenname= (TextView) convertView.findViewById(R.id.screenname);
vh.moviename= (TextView) convertView.findViewById(R.id.moviename);
vh.slots= (ViewGroup) convertView.findViewById(R.id.slotview);
vh.slot1= (TextView) vh.slots.findViewById(R.id.slot1);
vh.slot2= (TextView) vh.slots.findViewById(R.id.slot2);
vh.slot3= (TextView) vh.slots.findViewById(R.id.slot3);
vh.slot4= (TextView) vh.slots.findViewById(R.id.slot4);
convertView.setTag(vh);
}
else
{
vh=(ViewHolder) convertView.getTag();
}
vh.screenname.setText(list_screeen.get(position).getScreenname());
vh.moviename.setText(list_screeen.get(position).getMoviename());
slot_list=list_screeen.get(position).list_slots;
int i=slot_list.size();
if(i==4)
{
vh.slot1.setText(slot_list.get(0).getTime()+"\n"+slot_list.get(0).getPrice());
vh.slot2.setText(slot_list.get(1).getTime()+"\n"+slot_list.get(1).getPrice());
vh.slot3.setText(slot_list.get(2).getTime()+"\n"+slot_list.get(2).getPrice());
vh.slot4.setText(slot_list.get(3).getTime()+"\n"+slot_list.get(3).getPrice());
}
else if(i==3)
{
vh.slot1.setText(slot_list.get(0).getTime()+"\n"+slot_list.get(0).getPrice());
vh.slot2.setText(slot_list.get(1).getTime()+"\n"+slot_list.get(1).getPrice());
vh.slot3.setText(slot_list.get(2).getTime()+"\n"+slot_list.get(2).getPrice());
}
else if(i==2)
{
vh.slot1.setText(slot_list.get(0).getTime()+"\n"+slot_list.get(0).getPrice());
vh.slot2.setText(slot_list.get(1).getTime()+"\n"+slot_list.get(1).getPrice());
}
else if(i==1)
{
vh.slot1.setText(slot_list.get(0).getTime()+"\n"+slot_list.get(0).getPrice());
}
vh.slot1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vh.slot1.setBackgroundColor(Color.RED);
}
});
convertView.setBackgroundColor(Color.parseColor("#2B030303"));
return convertView;
}
public class ViewHolder
{
TextView screenname;
TextView moviename;
ListView slotlist;
ViewGroup slots;
TextView slot1;
TextView slot2;
TextView slot3;
TextView slot4;
}
}
答案 0 :(得分:0)
在TextView.setOnClickListener()
内的适配器中实施getView()
。您将拥有列表项的位置,您可以通过该位置识别单击了哪个项目。知道了吗?
答案 1 :(得分:0)
vh.slot1.setOnclickListener...
specClickListener spcl=new specClickListener();
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView=inflater.inflate(R.layout.screens_view_style, null);
vh=new ViewHolder();
vh.screenname= (TextView) convertView.findViewById(R.id.screenname);
vh.moviename= (TextView) convertView.findViewById(R.id.moviename);
vh.slots= (ViewGroup) convertView.findViewById(R.id.slotview);
vh.slot1= (TextView) vh.slots.findViewById(R.id.slot1);
vh.slot2= (TextView) vh.slots.findViewById(R.id.slot2);
vh.slot3= (TextView) vh.slots.findViewById(R.id.slot3);
vh.slot4= (TextView) vh.slots.findViewById(R.id.slot4);
vh.slot1.setOnClickListener(spcl);
vh.slot2.setOnClickListener(spcl);
vh.slot3.setOnClickListener(spcl);
vh.slot4.setOnClickListener(spcl);
convertView.setTag(vh);
}
else
{
vh=(ViewHolder) convertView.getTag();
}
vh.screenname.setText(list_screeen.get(position).getScreenname());
vh.moviename.setText(list_screeen.get(position).getMoviename());
slot_list=list_screeen.get(position).list_slots;
int i=slot_list.size();
if(i==4)
{
vh.slot1.setText(slot_list.get(0).getTime()+"\n"+slot_list.get(0).getPrice());
vh.slot2.setText(slot_list.get(1).getTime()+"\n"+slot_list.get(1).getPrice());
vh.slot3.setText(slot_list.get(2).getTime()+"\n"+slot_list.get(2).getPrice());
vh.slot4.setText(slot_list.get(3).getTime()+"\n"+slot_list.get(3).getPrice());
}
else if(i==3)
{
vh.slot1.setText(slot_list.get(0).getTime()+"\n"+slot_list.get(0).getPrice());
vh.slot2.setText(slot_list.get(1).getTime()+"\n"+slot_list.get(1).getPrice());
vh.slot3.setText(slot_list.get(2).getTime()+"\n"+slot_list.get(2).getPrice());
}
else if(i==2)
{
vh.slot1.setText(slot_list.get(0).getTime()+"\n"+slot_list.get(0).getPrice());
vh.slot2.setText(slot_list.get(1).getTime()+"\n"+slot_list.get(1).getPrice());
}
else if(i==1)
{
vh.slot1.setText(slot_list.get(0).getTime()+"\n"+slot_list.get(0).getPrice());
}
public class specClickListener implements View.OnClickListener {
public specClickListener (...) {
}
@Override public void onClick(View v) {
v.setbackgrouncolor(Color.Red);
}
}
convertView.setBackgroundColor(Color.parseColor("#2B030303"));
return convertView;
}