0

我的主要目的是在列表视图中制作一些按钮,以更改某些图像的状态(也在列表视图项目中)。所以我编写了这段代码,创建了一个像这样的自定义适配器:

public class CategoriesGridViewAdapter extends BaseAdapter {

private Activity context;

private List<CategoryHolder> mCategoriesList = new ArrayList<CategoryHolder>();
private CategoryHolder mCategoryHolder;

// @SuppressLint("UseSparseArrays")
// private Map<Integer, Boolean> favVisibilitiesMap = new HashMap<Integer,
// Boolean>();

// @SuppressLint("UseSparseArrays")
// private Map<Integer, Boolean> cartVisibilitiesMap = new HashMap<Integer,
// Boolean>();

private ViewHolder holder = null;

private int[] mTestingImagesForCategories = new int[] { R.drawable.pic1,
        R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5

};

public CategoriesGridViewAdapter(Activity activity,
        List<CategoryHolder> categoriesList) {
    this.context = activity;
    this.mCategoriesList = categoriesList;
}

@Override
public int getCount() {

    return mCategoriesList.size();
}

@Override
public Object getItem(int pos) {
    return mCategoriesList.get(pos);
}

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

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

    mCategoryHolder = mCategoriesList.get(pos);

    if (convertView == null) {
        convertView = context.getLayoutInflater().inflate(
                R.layout.cell_categories_gridview, parent, false);

        holder = new ViewHolder(convertView);

        holder.btn_cell_categoryAddCart
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        int getPosition = (Integer) v.getTag();

                        CategoryHolder localCategoryHolder = mCategoriesList
                                .get(getPosition);

                        localCategoryHolder.setCartVisible(!mCategoriesList
                                .get(getPosition).isCartVisible());

                        holder.iv_cell_categoryCart
                                .setVisibility(setImageVisibility(localCategoryHolder
                                        .isCartVisible()));

                    }
                });

        holder.btn_cell_categoryAddFav
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        int getPosition = (Integer) v.getTag();

                        CategoryHolder localCategoryHolder = mCategoriesList
                                .get(getPosition);

                        localCategoryHolder.setFavVisible(!mCategoriesList
                                .get(getPosition).isFavVisible());

                        holder.iv_cell_categoryFav
                                .setVisibility(setImageVisibility(localCategoryHolder
                                        .isFavVisible()));

                    }
                });

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.btn_cell_categoryAddCart.setTag(pos);
    holder.btn_cell_categoryAddFav.setTag(pos);

    holder.iv_cell_categoryCart
            .setVisibility(setImageVisibility(mCategoryHolder
                    .isCartVisible()));
    holder.iv_cell_categoryFav
            .setVisibility(setImageVisibility(mCategoryHolder
                    .isFavVisible()));

    // just for testing, the final images will be some bitmaps downloaded from a server
    holder.iv_cell_categoryContent
            .setBackgroundResource(mTestingImagesForCategories[pos]);

    return convertView;
}

private int setImageVisibility(boolean isVisible) {
    return (isVisible) ? View.VISIBLE : View.INVISIBLE;
}

static class ViewHolder {

    public ImageView iv_cell_categoryCart;
    public ImageView iv_cell_categoryFav;
    public ImageView iv_cell_categoryContent;
    public Button btn_cell_categoryAddFav;
    public Button btn_cell_categoryAddCart;

    public ViewHolder(View convertView) {

        iv_cell_categoryContent = (ImageView) convertView
                .findViewById(R.id.iv_cell_category_image);
        iv_cell_categoryCart = (ImageView) convertView
                .findViewById(R.id.iv_cell_category_cart);
        iv_cell_categoryFav = (ImageView) convertView
                .findViewById(R.id.iv_cell_category_fav);
        btn_cell_categoryAddFav = (Button) convertView
                .findViewById(R.id.btn_cell_category_add_fav);
        btn_cell_categoryAddCart = (Button) convertView
                .findViewById(R.id.btn_cell_category_add_cart);

    }

}

}

我只是不明白我的代码有什么问题?!

我有这个适配器,我对其进行了调试并且一切正常(更改了我放置在布局中的那些图像的布尔状态),但是:

1.

  • 它不会使图像(holder.iv_cell_categoryCart,holder.iv_cell_categoryFav)可见/不可见。问题可能是什么?(在每个 setOnClickListener 中使用 notifyDataSetChanged 解决了问题.. 是的,我知道.. 愚蠢的我)

2.

  • 这些静态图像(mTestingImagesForCategories -- R.drawable.pic1、R.drawable.pic2、R.drawable.pic3、R.drawable.pic4、R.drawable.pic5)从未显示。

此 GridView 的布局如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/iv_cell_category_image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/lay_cell_category_content"
    android:layout_below="@+id/lay_cell_category_header"
    android:background="@drawable/ic_launcher"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:scaleType="fitXY" />

<RelativeLayout
    android:id="@+id/lay_cell_category_header"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/activity_categories_gridview_cornerimage_height"
    android:layout_alignParentTop="true" >

    <ImageView
        android:id="@+id/iv_cell_category_fav"
        android:layout_width="@dimen/activity_categories_gridview_cornerimage_width"
        android:layout_height="fill_parent"
        android:layout_above="@+id/lay_cell_category_content"
        android:layout_alignParentLeft="true"
        android:background="@drawable/carti" />

    <ImageView
        android:id="@+id/iv_cell_category_cart"
        android:layout_width="@dimen/activity_categories_gridview_cornerimage_width"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:background="@drawable/star" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/lay_cell_category_content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_cell_category_add_cart"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Add Cart"
        android:textSize="12sp" />

    <Button
        android:id="@+id/btn_cell_category_add_fav"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Add Fav"
        android:textSize="12sp" />
</LinearLayout>

这种布局的想法是有一个图像的布局主要内容,有 2 个底部按钮,使布局的右上角和左上角的 2 个小图像可见/不可见。(如果用户按下 Add Fav,右上角的星形图像将可见。如果他再次按下它,图像将变得不可见 - 模拟添加/删除产品到收藏夹)。请任何帮助表示赞赏!

LE:我应该提一下,我正在以编程方式在 Fragment(在 ViewPager 中使用)内创建 GridView。

4

1 回答 1

0

看起来问题出在 mCategoriesList 上。在活动中设置适配器时它为空,之后我对其进行了初始化,导致它仅显示 gridview 单元布局的静态内容(2 个按钮和 2 个不可更改的图像)。

于 2013-04-17T07:41:27.717 回答