在 RecyclerView 项目按钮 OnClick 中使用共享首选项

Using Shared Preferences in a RecyclerView item Button OnClick

我的 product_list_item.xml 中有一个 ImageView,如下所示:

 <ImageView
        android:id="@+id/imgbtn_favorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/txt_pdt_desc"
        android:layout_alignParentRight="true"
        android:layout_marginRight="3dp"
        android:background="@null"
        android:clickable="true"
        android:focusable="true"
        android:onClick="starOnClick"
        android:contentDescription="@string/favorites" />

App Output: RecyclerView with Favourites ImageView

我的 OnItemClickOnItemLongClick 在我的 ProductListFragment class 中,如下所示:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Product product = (Product) parent.getItemAtPosition(position);
    Toast.makeText(activity, product.toString(), Toast.LENGTH_LONG).show();
}

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
        int position, long arg3) {
    ImageView button = (ImageView) view.findViewById(R.id.imgbtn_favorite);

    String tag = button.getTag().toString();
    if (tag.equalsIgnoreCase("grey")) {
        sharedPreference.addFavorite(activity, products.get(position));
        Toast.makeText(activity,
                activity.getResources().getString(R.string.add_favr),
                Toast.LENGTH_SHORT).show();

        button.setTag("red");
        button.setImageResource(R.drawable.heart_red);
    } else {
        sharedPreference.removeFavorite(activity, products.get(position));
        button.setTag("grey");
        button.setImageResource(R.drawable.heart_grey);
        Toast.makeText(activity,
                activity.getResources().getString(R.string.remove_favr),
                Toast.LENGTH_SHORT).show();
    }

    return true;
}

编辑 我的ProductListAdapterclass

public class ProductListAdapter extends ArrayAdapter<Product> {

private Context context;
List<Product> products;
SharedPreference sharedPreference;

public ProductListAdapter(Context context, List<Product> products) {
    super(context, R.layout.product_list_item, products);
    this.context = context;
    this.products = products;
    sharedPreference = new SharedPreference();
}

private class ViewHolder {
    TextView productNameTxt;
    TextView productDescTxt;
    TextView productPriceTxt;
    ImageView favoriteImg;
}

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

@Override
public Product getItem(int position) {
    return products.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.product_list_item, null);
        holder = new ViewHolder();
        holder.productNameTxt = (TextView) convertView
                .findViewById(R.id.txt_pdt_name);
        holder.productDescTxt = (TextView) convertView
                .findViewById(R.id.txt_pdt_desc);
        holder.productPriceTxt = (TextView) convertView
                .findViewById(R.id.txt_pdt_price);
        holder.favoriteImg = (ImageView) convertView
                .findViewById(R.id.imgbtn_favorite);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Product product = (Product) getItem(position);
    holder.productNameTxt.setText(product.getName());
    holder.productDescTxt.setText(product.getDescription());
    holder.productPriceTxt.setText(product.getPrice() + "");

    /*If a product exists in shared preferences then set heart_red drawable
     * and set a tag*/
    if (checkFavoriteItem(product)) {
        holder.favoriteImg.setImageResource(R.drawable.heart_red);
        holder.favoriteImg.setTag("red");
    } else {
        holder.favoriteImg.setImageResource(R.drawable.heart_grey);
        holder.favoriteImg.setTag("grey");
    }

    return convertView;
}

/*Checks whether a particular product exists in SharedPreferences*/
public boolean checkFavoriteItem(Product checkProduct) {
    boolean check = false;
    List<Product> favorites = sharedPreference.getFavorites(context);
    if (favorites != null) {
        for (Product product : favorites) {
            if (product.equals(checkProduct)) {
                check = true;
                break;
            }
        }
    }
    return check;
}

@Override
public void add(Product product) {
    super.add(product);
    products.add(product);
    notifyDataSetChanged();
}

@Override
public void remove(Product product) {
    super.remove(product);
    products.remove(product);
    notifyDataSetChanged();
}   
}

EDIT-2

我的 SharedPreferencesclass 有以下内容:

   public void addFavorite(Context context, Product product) {
    List<Product> favorites = getFavorites(context);
    if (favorites == null)
        favorites = new ArrayList<Product>();
    favorites.add(product);
    saveFavorites(context, favorites);
}

public void removeFavorite(Context context, Product product) {
    ArrayList<Product> favorites = getFavorites(context);
    if (favorites != null) {
        favorites.remove(product);
        saveFavorites(context, favorites);
    }

我完整的ProductListFragmentclass如下:

public class ProductListFragment extends Fragment implements
    OnItemClickListener, OnItemLongClickListener {

public static final String ARG_ITEM_ID = "product_list";

Activity activity;
ListView productListView;
List<Product> products;
ProductListAdapter productListAdapter;

SharedPreference sharedPreference;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = getActivity();
    sharedPreference = new SharedPreference();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_product_list, container,
            false);
    findViewsById(view);

    setProducts();

    productListAdapter = new ProductListAdapter(activity, products);
    productListView.setAdapter(productListAdapter);
    productListView.setOnItemClickListener(this);
    productListView.setOnItemLongClickListener(this);
    return view;
}

private void setProducts() {

    Product product1 = new Product(1, "Dell XPS", "Dell XPS Laptop", 60000);
    Product product2 = new Product(2, "HP Pavilion G6-2014TX",
            "HP Pavilion G6-2014TX Laptop", 50000);
    Product product3 = new Product(3, "ProBook HP 4540",
            "ProBook HP 4540 Laptop", 45000);
    Product product4 = new Product(4, "HP Envy 4-1025TX",
            "HP Envy 4-1025TX Laptop", 46000);
    Product product5 = new Product(5, "Dell Inspiron",
            "Dell Inspiron Laptop", 48000);
    Product product6 = new Product(6, "Dell Vostro", "Dell Vostro Laptop",
            50000);
    Product product7 = new Product(7, "IdeaPad Z Series",
            "Lenovo IdeaPad Z Series Laptop", 40000);
    Product product8 = new Product(8, "ThinkPad X Series",
            "Lenovo ThinkPad X Series Laptop", 38000);
    Product product9 = new Product(9, "VAIO S Series",
            "Sony VAIO S Series Laptop", 39000);
    Product product10 = new Product(10, "Series 5",
            "Samsung Series 5 Laptop", 50000);

    products = new ArrayList<Product>();
    products.add(product1);
    products.add(product2);
    products.add(product3);
    products.add(product4);
    products.add(product5);
    products.add(product6);
    products.add(product7);
    products.add(product8);
    products.add(product9);
    products.add(product10);
}

private void findViewsById(View view) {
    productListView = (ListView) view.findViewById(R.id.list_product);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Product product = (Product) parent.getItemAtPosition(position);
    Toast.makeText(activity, product.toString(), Toast.LENGTH_LONG).show();
}

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
        int position, long arg3) {
    ImageView button = (ImageView) view.findViewById(R.id.imgbtn_favorite);

    String tag = button.getTag().toString();
    if (tag.equalsIgnoreCase("grey")) {
        sharedPreference.addFavorite(activity, products.get(position));
        Toast.makeText(activity,
                activity.getResources().getString(R.string.add_favr),
                Toast.LENGTH_SHORT).show();

        button.setTag("red");
        button.setImageResource(R.drawable.heart_red);
    } else {
        //sharedPreference.removeFavorite(activity, products.get(position));
        button.setTag("grey");
        button.setImageResource(R.drawable.heart_grey);
        Toast.makeText(activity,
                activity.getResources().getString(R.string.remove_favr),
                Toast.LENGTH_SHORT).show();
    }

    return true;
}

目前,当用户在 RecyclerView 中长按(OnItemLongClick)一个项目时,这些项目被标记为收藏夹..

我在 MainActivity class 中实现了 starOnClick,如下所示:

public void starOnClick (View view){
    // Toast.makeText(getApplicationContext(),"Star Clicked",Toast.LENGTH_SHORT).show();
}

但我收到错误,因为我不知道如何获取项目的位置并在此处保存共享首选项....

如何在我的 ImageView onClick 上实现 imgbtnFavourite 以将项目标记为收藏夹

您需要在不在 MainActivity 的适配器上实现点击:

holder.favoriteImg.setOnClickListener(this)

并实施 onClick(View v),将产品添加到共享首选项并更改图像。

首先,我认为使用 SharedPreferences 存储此 "isFavorite" 信息是次优解决方案。这是一个磁盘操作,会在高度优化的 RecyclerView 元素中影响性能。而是将 属性 添加到您的 Product class 以维护产品是否为 "Favorite" 的信息。在我的例子中,我将使用 getIsFavorite()setIsFavorite(boolean).

您需要通过设置 setOnClickListener() 来处理 Adapter class 中的 onClick。请注意,您也可以只为 Item 添加 onClick 而不是 ImageView ...这将为用户提供更大的点击目标。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.product_list_item, null);
        holder = new ViewHolder();
        holder.productNameTxt = (TextView) convertView
                .findViewById(R.id.txt_pdt_name);
        holder.productDescTxt = (TextView) convertView
                .findViewById(R.id.txt_pdt_desc);
        holder.productPriceTxt = (TextView) convertView
                .findViewById(R.id.txt_pdt_price);
        holder.favoriteImg = (ImageView) convertView
                .findViewById(R.id.imgbtn_favorite);

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

    holder.favoriteImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Product prod = products.get(position);
            //toggle the value
            boolean isFavorite = !prod.getIsFavorite();
            prod.setIsFavorite(isFavorite);

            if (isFavorite) {
                holder.favoriteImg.setImageResource(R.drawable.heart_red);
                holder.favoriteImg.setTag("red");
            } else {
                holder.favoriteImg.setImageResource(R.drawable.heart_grey);
                holder.favoriteImg.setTag("grey");
            }
        }
    }); 

    Product product = (Product) getItem(position);
    holder.productNameTxt.setText(product.getName());
    holder.productDescTxt.setText(product.getDescription());
    holder.productPriceTxt.setText(product.getPrice() + "");

    boolean isItemFavorite = product.getIsFavorite();

    if (isItemFavorite) {
        holder.favoriteImg.setImageResource(R.drawable.heart_red);
        holder.favoriteImg.setTag("red");
    } else {
        holder.favoriteImg.setImageResource(R.drawable.heart_grey);
        holder.favoriteImg.setTag("grey");
    }

    return convertView;
}

现在您可以摆脱 checkFavoriteItem() 方法,因为您不再使用 SharedPreferences

所以现在您不需要 ImageView

中的这些属性
    android:clickable="true"
    android:focusable="true"
    android:onClick="starOnClick"

删除它们,使其看起来像这样:

<ImageView
        android:id="@+id/imgbtn_favorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/txt_pdt_desc"
        android:layout_alignParentRight="true"
        android:layout_marginRight="3dp"
        android:background="@null"
        android:contentDescription="@string/favorites" />




编辑:

要将 isFavorite 属性 添加到您的 Product class 只需添加一个新的 class 变量:

private boolean isFavorite = false;

然后添加 setter:

public void setIsFavorite(boolean favorite) { this.isFavorite = favorite; } 

和getter:

public boolean getIsFavorite(){ return this.isFavorite; }