从ListFragment获取对图像视图的访问权



我有一个布局为a的ListFragment,对于列表中的每个项目,我都有它的布局,也称为B。在B中,我有一个id为"imgVi"的图像视图:

<ImageView
    android:id="@+id/imgVi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

从我的ListFragment在方法onCreateView我想获得访问这个ImageView改变图像src。我该怎么做呢?因为这个imageView不是在ListFragment布局,我不能这样做:

ImageView imgEmp = (ImageView) view.findViewById(R.id.imageViewEmp);
imgEmp.setBackgroundResource(R.drawable.ic_tab_emp_selected);

但是布局B在布局a中,因为它是一个包含行的列表

任何帮助将不胜感激。我是Android新手。

编辑:我得到了它的工作,只是按照这个教程http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/

public class ListClientsCursorAdapter extends SimpleCursorAdapter{
private Context context;
private int layout;
public ListClientsCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to, int flags) {
    super(context, layout, c, from, to, flags);
    this.context = context;
    this.layout = layout;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    Cursor c = getCursor();
    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout, parent, false);
    int nameCol = c.getColumnIndex("name");
    String name = c.getString(nombreCol);
    int telfCol = c.getColumnIndex("telf");
    String telf = c.getString(telfCol);
    /**
     * Next set the name of the entry.
     */    
    TextView name_text = (TextView) v.findViewById(R.id.textViewNombEmp);
    if (name_text != null) {
        name_text .setText(name);
    }
    TextView telf_text = (TextView) v.findViewById(R.id.textViewTelfEmp);
    if (telf_text != null) {
        telf_text.setText(telf);
    }
    ImageView imgEmp = (ImageView) v.findViewById(R.id.imageViewEmp);
    if (imgEmp != null) {
        imgEmp.setBackgroundResource(R.drawable.ic_tab_emp_selected);
    }
    return v;
}

}

然后在ListFragment的onCreateView中调用:

ListClientCursorAdapter notes = new ListClientCursorAdapter(context,R.layout.activity_fil_client, mCursor, from, to, 0);
setListAdapter(notes);

代替SimpleCursorAdapter

如果您有ListFragment,则必须为列表设置适配器。在该适配器中,您可以覆盖getView()方法,并且可以在那里访问ImageView,您不能从片段中的onCreateView方法访问它。

最新更新