gpt4 book ai didi

android - 我如何在listview android中切换三个以上的图像

转载 作者:行者123 更新时间:2023-11-29 01:45:00 24 4
gpt4 key购买 nike

我有一个 ListView ,每行都有一个图像,现在我想要的是我想在点击时更改该图像,现在如果它是两个图像,那将非常简单。我可以使用 bool 值并设置项目的位置要更改的图像。但我的案例涉及两张以上的图片第一次点击图像更改为 image0第二次单击图像更改为 image1第三次单击图像更改为 image2并在第 4 次单击图像变回图像图像切换得很好,但是一旦重新加载列表,所有内容都会随着切换图像位置从 0 变为 8 等而发生变化。我也尝试设置默认行为。所以,这是我的示例代码

     @Override
public View getView( int position, View convertView, ViewGroup parent) {

View vi=convertView;


if(convertView==null){

/********** Inflate tabitem.xml file for each row ( Defined below ) ************/
vi = inflater.inflate(R.layout.tabitem, null);

/******** View Holder Object to contain tabitem.xml file elements ************/
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);
holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.image=(ImageView)vi.findViewById(R.id.image);
holder.image.setOnClickListener(this);
holder.image.setTag(position);

/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();

if(data.size()<=0)
{
holder.text.setText("No Data");

}
else
{
/***** Get each Model object from Arraylist ********/

tempValues = (ListModel) data.get(position);

Log.i("Mainly at", Integer.toString(position)+"state is "+tempValues.getStatus());
/************ Set Model values in Holder elements ***********/
holder.text.setText(tempValues.getCompanyName());
holder.text1.setText(tempValues.getUrl());


// holder.image.setImageResource(R.drawable.image0);
if(tempValues.getStatus().equals(ListModel.imageStateNone))
{
holder.image.setImageResource(R.drawable.image0);
}

if(tempValues.getStatus().equals(ListModel.imageStateAnotherCase))
{


if(tempValues.getImageStateAtPos()==0){
holder.image.setImageResource(R.drawable.image0);// or what it should be in default
}


if(tempValues.getImageStateAtPos()==1){
holder.image.setImageResource(R.drawable.plus);// or what it should be in default
}

if(tempValues.getImageStateAtPos()==2){
holder.image.setImageResource(R.drawable.minus);// or what it should be in default
}


}


/******** Set Item Click Listner for LayoutInflater for each row ***********/
// vi.setOnClickListener(new OnItemClickListener(position));
}
return vi;
}

@Override
public void onClick(View v) {
int position = (Integer)v.getTag();
tempValues.setStateAsChange();
Log.e("clicked at", Integer.toString(tempValues.getImageStateAtPos())+"Changed state is "+tempValues.getStatus()+"And position"+Integer.toString(position));
tempValues.setImageStateAtPos(tempValues.getImageStateAtPos()+1);

if(tempValues.getImageStateAtPos()>2){
tempValues.setImageStateAtPos(0);
}
// holder.image.setImageResource(R.drawable.plus);
LinearLayout rl = (LinearLayout)v.getParent();
holder.image = (ImageView)rl.getChildAt(0);
// ((ImageView) v).setImageResource(R.drawable.plus);
if(tempValues.getImageStateAtPos()==0){
holder.image.setImageResource(R.drawable.image0);// or what it should be in default
}


if(tempValues.getImageStateAtPos()==1){
holder.image.setImageResource(R.drawable.plus);// or what it should be in default
}

if(tempValues.getImageStateAtPos()==2){
holder.image.setImageResource(R.drawable.minus);// or what it should
}
if(tempValues.getImageStateAtPos()==3){
holder.image.setImageResource(R.drawable.image10);
}



}


}

ListModel代码在这里 私有(private)字符串状态="";

  public static final String imageStateNone="none"; 
public static final String imageStateAnotherCase="changed";
private int state;
/****
HashMap<Integer, String> set=new HashMap<Integer, String>();

public void setStateAsnone(int pos)
{
set.put(pos,imageStateNone );
}


public void setStateAsChange(int pos)
{
set.put(pos, imageStateAnotherCase);
//this.status = imageStateAnotherCase;
}

public String getStatus(int pos)
{
return set.get(pos);
}



public void setImageStateAtPos(int state){
this.state=state;
}

public int getImageStateAtPos(){
return state;
}





public void setStateAsnone()
{
this.status = imageStateNone;
}


public void setStateAsChange()
{
this.status = imageStateAnotherCase;
}

public String getStatus()
{
return status;
}

最后在mainactivity中添加数据

    for (int i = 0; i < 11; i++) {

final ListModel sched = new ListModel();

/******* Firstly take data in model object ******/
sched.setCompanyName("Company "+i);
sched.setImage("image"+i);
sched.setUrl("http:\\\\www."+i+".com");
sched.setImageStateAtPos(0);
sched.setStateAsnone();
/******** Take Model Object in ArrayList **********/
CustomListViewValuesArr.add(sched);
}

enter code here

一切都像魅力一样,直到列表重新加载当列表重新加载尝试使用状态位置指示器使用 hashmap 时,一切都变得困惑。现在只想知道如何在重新加载 ListView 时停止更改图像切换的位置。帮忙。

最佳答案

在您的 ListModel 对象中放置一个数据,表示绑定(bind)到它的行项目是否发生了变化。然后将此语句添加到您的 getView() 中,如下所示:

getView(){

if(model.getImageSate()==ImageState.NONE){
holder.image.setImageResource(R.drawable.image);// or what it should be in default
}
if(model.getImageSate()==ImageState.YetAnotherState){,
holder.image.setImageResource(R.drawable.theOtherImage);
}
...
}

编辑

我假设您正在为您的列表项使用自定义对象。所以在这里

    class ListModel{

// you may use a String variable or an enumeration for this. ill use a
string field for this example:

public static final String imageStateNone="none";
public static final String imageStateAnotherCase="anothercase";
private String imageState;

//getter - setter for imageState field

}

以防您使用来自 sdk 或其他库的预定义类。继承它并像这样放置你的变量:

class ExtendedObject extends ListModel{


public static final String imageStateNone="none";
public static final String imageStateAnotherCase="anothercase";
private String imageState;

//getter - setter for imageState field



}

不要忘记使用 string.equals("") 来比较字符串。

关于android - 我如何在listview android中切换三个以上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21982330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com