如何启用和禁用每个列表项上的图像按钮单击“懒惰适配器”

时间:2014-08-05 07:53:06

标签: android listview lazy-loading

嘿,在ListView中选择项目时遇到问题,在每个列表项onClick()上我正在尝试启用buttonButton我想让选择模式单一,即使在xml中实现单一选择模式。它允许我多次选择我的ImageButton。我想在这里实现的目标是,只有用户可以一次选择一个ListView项,但是在ImageButton之后会保持点击以进行ListView的多选。 任何人都可以帮我解决这个问题

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;
}


}

1 个答案:

答案 0 :(得分:0)

我找到了答案,我要感谢shaiful回答的以下链接,这有助于找到答案。 Android - Keep ListView's item highlighted once one has been clicked

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;
}



}