Android 如何使用SimpleAdapter使ImageView在ListView中可单击?

Android 如何使用SimpleAdapter使ImageView在ListView中可单击?,android,listview,imageview,simpleadapter,Android,Listview,Imageview,Simpleadapter,我有一个使用SimpleAdapter生成的Listview。我的Listview在单击行时显示另一个Listview。我已在适配器中放置了一个ImageView。我想使ImageView可单击以更改ImageView的图片。有人可以建议我如何操作吗? 我在下面发布我的代码: public View onCreateView(LayoutInflater inflater1, ViewGroup container, Bundle savedInstanceState)

我有一个使用SimpleAdapter生成的Listview。我的Listview在单击行时显示另一个Listview。我已在适配器中放置了一个ImageView。我想使ImageView可单击以更改ImageView的图片。有人可以建议我如何操作吗? 我在下面发布我的代码:

public View onCreateView(LayoutInflater inflater1, ViewGroup container,
            Bundle savedInstanceState) {

   View rootView = inflater1.inflate(R.layout.listview_header_row, container, false);
   final ImageView im=(ImageView)rootView.findViewById(R.id.imageView1);
   SharedPreferences pref = getActivity().getPreferences(0);
   final String id = pref.getString("mynumber", "empty");
   Databasehandler db=new Databasehandler(getActivity().getApplicationContext());
   final ListView l=(ListView) rootView.findViewById(R.id.listViewad);
   val=db.getTaskSent(id);
   ListAdapter k=new SimpleAdapter(getActivity(),val,R.layout.r,new String[]{"TaskId","heading"},new int[]{R.id.textViews,R.id.textViews1});
   l.setAdapter(k);

   im.setOnClickListener(new OnClickListener() {
      @Override
       public void onClick(View arg0) {
          // TODO Auto-generated method stub
          Toast.makeText(getActivity(), "json", Toast.LENGTH_SHORT).show();
       }
   });

     l.setOnItemClickListener(new OnItemClickListener() {
           @Override
          public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
              TextView tv2=(TextView) arg1.findViewById(R.id.textViews);
              String t=tv2.getText().toString();
              Intent i4=new Intent(getActivity(),SentTaskOnclick.class);
              i4.putExtra("taskId",t);
              startActivity(i4);
          }
    });
创建视图时的公共视图(布局、充气机1、视图组容器、, Bundle savedInstanceState){ 视图根视图=充气器1.充气(R.layout.listview\u header\u row,container,false); 最终ImageView im=(ImageView)rootView.findViewById(R.id.imageView1); SharedReferences pref=getActivity().getPreferences(0); 最终字符串id=pref.getString(“mynumber”、“empty”); Databasehandler db=新的Databasehandler(getActivity().getApplicationContext()); 最终ListView l=(ListView)rootView.findViewById(R.id.listViewad); val=db.getTaskSent(id); ListAdapter k=new SimpleAdapter(getActivity(),val,R.layout.R,新字符串[]{“TaskId”,“heading”},新int[]{R.id.textViews,R.id.textViews1}); l、 setAdapter(k); im.setOnClickListener(新的OnClickListener(){ @凌驾 公共void onClick(视图arg0){ //TODO自动生成的方法存根 Toast.makeText(getActivity(),“json”,Toast.LENGTH_SHORT.show(); } }); l、 setOnItemClickListener(新的OnItemClickListener(){ @凌驾 公共链接(AdapterView arg0、视图arg1、, 整数arg2,长arg3){ TextView tv2=(TextView)arg1.findViewById(R.id.TextView); 字符串t=tv2.getText().toString(); Intent i4=新的Intent(getActivity(),SentTaskOnclick.class); i4.putExtra(“任务ID”,t); 星触觉(i4); } });
我将分享我的一些代码,我想你可以对你的问题有一个想法。 您还可以自己下载并测试代码:

这是我的单元格类,我在其中管理单击并更改图像颜色(在本例中,我只需要将图片设置为灰度)

公共类CategoryCell扩展RelativeLayout实现OnClickListener{

private Category category;
private ImageView icon;
private boolean selected = true;

public CategoryCell(Context context, AttributeSet attrs) {
    super(context,attrs);
    LayoutInflater.from(getContext()).inflate(R.layout.category_cell, this, true);
    setOnClickListener(this);
}

public void init(Category category){
    this.category = category;
    icon = (ImageView)findViewById(R.id.category_icon);
    icon.setImageDrawable(category.getIcon(getContext()));
    icon.getDrawable().clearColorFilter();
    if(isSelected())
        GlobalState.getInstance().addCategory(category);
}

public CategoryCell(Context context) {
    this(context,null);
}

@Override
public void onClick(View v) {
    setSelected(!isSelected());
}

public boolean isSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    if(category==null)
        return;
    this.selected = selected;
    if(!isSelected()){
        ColorMatrix matrix = new ColorMatrix();
        matrix.setSaturation(0);
        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
        icon.getDrawable().setColorFilter(filter);
        icon.invalidate();
        GlobalState.getInstance().removeCategory(category);
    }else{
        GlobalState.getInstance().addCategory(category);
        icon.getDrawable().clearColorFilter();
        icon.invalidate();
    }
}

public Category getCategory() {
    return category;
}
}

在这里,您可以查看我的Row类,我只是将单元格添加到视图中

public class CategoryRow extends LinearLayout {

private LinearLayout root;
private Map<Category,CategoryCell> cells = new HashMap<Category, CategoryCell>();


public CategoryRow(Context context, AttributeSet attrs) {
    super(context, attrs);
    root = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.category_row, this, true);
}

public CategoryRow(Context context) {
    super(context);
    root = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.category_row, this, true);
}

public static CategoryRow create(ViewGroup parent, LayoutInflater inflater) {
    final CategoryRow view = new CategoryRow(parent.getContext());
    return view;
}

public void init(Type type, List<Category> cells) {
    LinearLayout ll = (LinearLayout)root.getChildAt(0);
    for(int i = 0;i<cells.size();i++) {
        CategoryCell cell = ((CategoryCell)ll.getChildAt(i));
        cell.init(cells.get(i));
        this.cells.put(cells.get(i),cell);
    }
}

public Map<Category,CategoryCell> getCells() {
    return cells;
}

}
公共类CategorRow扩展了LinearLayout{
私有线性布局根;
私有映射单元格=新HashMap();
公共类别(上下文、属性集属性){
超级(上下文,attrs);
root=(LinearLayout)LayoutInflater.from(context).膨胀(R.layout.category_行,this,true);
}
公共类别(上下文){
超级(上下文);
root=(LinearLayout)LayoutInflater.from(context).膨胀(R.layout.category_行,this,true);
}
公共静态类别创建(视图组父级、布局更平坦充气器){
final CategoryRow视图=新CategoryRow(parent.getContext());
返回视图;
}
公共void init(类型,列表单元格){
LinearLayout ll=(LinearLayout)root.getChildAt(0);
对于(int i=0;i
public class CategoryList extends ListView {

private List<List<Category>> categories = new ArrayList<List<Category>>();
private Map<Category,CategoryCell> cells = new HashMap<Category, CategoryCell>();
private Type type;

public CategoryList(Context context, AttributeSet attrs) {
    super(context, attrs);
    setBackgroundColor(getResources().getColor(R.color.wet_asphalt));
    setDivider(null);
    setVerticalFadingEdgeEnabled(false);
    setVerticalScrollBarEnabled(false); 
    setHorizontalScrollBarEnabled(false);
    Utils.changeRoundedCornersColor(this, R.color.carrot,false);
    if(isInEditMode())
        init(Type.BAR);
}

public void init(Type type){
    this.type = type;
    categories.clear();
    if(getAdapter()!=null)
        ((CategoryAdapter)getAdapter()).notifyDataSetChanged();
    setAdapter(null);
    categories = loadCells();
    setAdapter(new CategoryAdapter());
    ((CategoryAdapter)getAdapter()).notifyDataSetChanged();
}

private List<List<Category>> loadCells() {
    List<List<Category>> cells = new ArrayList<List<Category>>();
    int rowLimitCounter = 0;
    ArrayList<Category> categoryCells = null;
    Category[] categories = Category.values();
    for(int i = 0;i<categories.length;i++){
        if(rowLimitCounter==0)
            categoryCells = new ArrayList<Category>();
        categoryCells.add(categories[i]);
        rowLimitCounter++;
        if(rowLimitCounter>3 || i == (categories.length-1)){
            cells.add(categoryCells);
            rowLimitCounter=0;
        }
    }
    return cells;
}

private class CategoryAdapter extends BaseAdapter{

    private final LayoutInflater inflater;

    public CategoryAdapter() {
        inflater = LayoutInflater.from(getContext());
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override 
    public int getCount() {
        return categories.size();
    }

    @Override 
    public List<Category> getItem(int position) {
        return categories.get(position);
    }

    @Override 
    public long getItemId(int position) {
        return position;
    }

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) {
        CategoryRow row = (CategoryRow) convertView;
        if (row == null) {
            row = CategoryRow.create(parent, inflater);
        }
        row.init(type, categories.get(position));
        cells.putAll(row.getCells());
        return row;
    }
}
}