Android 如何启用和禁用每个列表项上的图像按钮单击“惰性适配器”
在ListView中选择项目时出现问题,单击每个列表项目上我试图启用的按钮。按钮我想使选择模式单一,即使在xml中实现选择模式单一。它允许我选择多个ImageButton。我想在这里实现的是,一次只有用户可以选择一个ListView项目,但在这里ImageButton保持单击状态以进行ListView的多个选择。 有人能帮我解决这个问题吗Android 如何启用和禁用每个列表项上的图像按钮单击“惰性适配器”,android,listview,lazy-loading,Android,Listview,Lazy Loading,在ListView中选择项目时出现问题,单击每个列表项目上我试图启用的按钮。按钮我想使选择模式单一,即使在xml中实现选择模式单一。它允许我选择多个ImageButton。我想在这里实现的是,一次只有用户可以选择一个ListView项目,但在这里ImageButton保持单击状态以进行ListView的多个选择。 有人能帮我解决这个问题吗 public class LazyAdapter extends BaseAdapter{ private Activity activity; priva
public class LazyAdapter extends BaseAdapter{
private Activity activity;
private String[] data;
private String[] imageNames;
private String[] creatives;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public String assetId;
public LinearLayout creativeSaveChanges,cancelCreative;
public LazyAdapter(Activity a, String[] imageurl,String[] names,String[] creativeId) {
activity = a;
data=imageurl;
imageNames = names;
creatives = creativeId;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.boards_creative_custom_listview, null);
cancelCreative = (LinearLayout)vi. findViewById(R.id.cancelCreative);
creativeSaveChanges = (LinearLayout)vi. findViewById(R.id.creativeSaveChanges);
TextView text=(TextView)vi.findViewById(R.id.specialTxtFridaySpecialTroopsList);;
ImageView image=(ImageView)vi.findViewById(R.id.imageboardsCreativeList);
final ImageButton selectbutton = (ImageButton) vi.findViewById(R.id.imageButtonRefreshListCreative);
text.setText(imageNames[position]);
imageLoader.DisplayImage(data[position], image);
vi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectbutton.setBackgroundResource(R.drawable.rightblue);
assetId = creatives[position];
Log.e("creatives", assetId);
for (int i = 0; i <creatives.length; i++) {
if(i == position){
Log.e("creatives.length", i+" ,"+position+"");
}else if(i != position){
selectbutton.setVisibility(View.GONE);
Log.e("creatives.length", i+" and "+position+"");
}
}
}
});
return vi;
}
}
我找到了答案,我想感谢shaiful回答的以下链接帮助我找到了答案。
image.setenablefalse或image.setenabletrueNope不能以这种方式工作,因为我们正在尝试单击每个列表项。假设我单击了列表项1按钮应启用,单击列表项2时,项目1按钮应禁用,并在项目2中启用。单击列表视图项时使用值true或false单击使用notifidatachange更新您的列表视图更改可能是其逻辑井其惰性适配器配合,无法调用项单击。我们可以在vi.onclicklistener.com中实现的唯一一件事是向延迟适配器传递一个参数集,如true或false数组,并在单击listview并重新加载listview时更新其值
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.igreens.fliphound.imageutils.ImageLoader;
public class LazyAdapter extends BaseAdapter{
private Activity activity;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public String assetId;
private int selectedIndex;
ArrayList<HashMap<String, Object>> listValue =new ArrayList<HashMap<String, Object>>();
private int checkCount = 0;
private ArrayList<Boolean> check;
private LazyAdapter adapter;
public LazyAdapter(Activity a, ArrayList<HashMap<String, Object>> listValues) {
activity = a;
listValue = listValues;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
checkCount = listValues == null ? 0 : listValues.size();
check = new ArrayList<Boolean>(checkCount);
selectedIndex = -1;
this.adapter = this;
}
public int getCount() {
return listValue.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public void setSelectedIndex(int ind)
{
selectedIndex = ind;
notifyDataSetChanged();
}
private class ViewHolder{
TextView name;
ImageView boardImage;
ImageButton selectButton;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.boards_creative_custom_listview, null);
holder = new ViewHolder();
holder.name=(TextView)vi.findViewById(R.id.specialTxtFridaySpecialTroopsList);;
holder.boardImage=(ImageView)vi.findViewById(R.id.imageboardsCreativeList);
holder.selectButton = (ImageButton) vi.findViewById(R.id.imageButtonRefreshListCreative);
holder.name.setText(listValue.get(position).get("creativeName").toString());
/*final LinearLayout cancelCreative = (LinearLayout)vi. findViewById(R.id.cancelCreative);
final LinearLayout creativeSaveChanges = (LinearLayout)vi. findViewById(R.id.creativeSaveChanges);*/
imageLoader.DisplayImage(listValue.get(position).get("creativeImage").toString(), holder.boardImage);
check.add(position, true);
Log.e("position", ""+position);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if(selectedIndex!= -1 && position == selectedIndex)
{
holder.selectButton.setBackgroundResource(R.drawable.rightblue);
}
else
{
holder.selectButton.setBackgroundResource(R.drawable.righttick);
}
vi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
adapter.setSelectedIndex(position);
Log.e("position", ""+position);
}
});
return vi;
}
}