如何启用和禁用卡项目中的视图?

时间:2016-02-24 08:13:12

标签: android android-recyclerview android-cardview

我正在使用cardview来设计这个布局。在此处,当我在任何项目中点击 3点图标时,相应的卡片项目会显示透明黑色图层。但是当我点击 ITEM-1 Vennila 时,我需要做什么,它需要显示透明黑色布局,然后当我点击 ITEM -2时点 ITEM-1透明必须消失,必须显示ITEM-2透明。

enter image description here

我正在使用RecyclerView。请帮我解决。

这是我的适配器代码:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

    List<Restaurant_Beam> restaurant_prducts;
    Config config;
    Bitmap bmp;
    boolean success = false;
    ArrayList<String> CATEGORYNAME = new ArrayList<>();
    ArrayList<String> ITEMNAME = new ArrayList<>();
    ArrayList<String> ITEM_PRICE = new ArrayList<>();
    ArrayList<String> LIKE_COUNT = new ArrayList<>();
    int clickCount = 0;

    private ImageLoader imageLoader;
    private Context context;

    public CardAdapter(List<Restaurant_Beam> superHeroes, Context context) {
        super();

        this.restaurant_prducts = superHeroes;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        Restaurant_Beam restaurant_beam = restaurant_prducts.get(position);
        Typeface tf = Typeface.createFromAsset(context.getAssets(), "roboto.medium.ttf");
        Typeface tf1 = Typeface.createFromAsset(context.getAssets(), "roboto.regular.ttf");


        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(config.IMAGE_URL + restaurant_beam.getImageUrl() + ".jpg", ImageLoader.getImageListener(holder.productImage, R.drawable.image_1, R.drawable.image_1));  //android.R.drawable.ic_dialog_alert

        String IMAGE_URL = config.IMAGE_URL + restaurant_beam.getImageUrl() + ".jpg";
        String ITEM_NAME = restaurant_beam.getStr_productName();
        String CATEGORY_NAME = restaurant_beam.getStr_categoryName();
        String PRICE = String.valueOf(restaurant_beam.getStrPrice());
        int LIKECOUNT = (restaurant_beam.getLikeCount());
        String ITEM_ID = restaurant_beam.getProduct_id();
        Log.e("Like Count  ", String.valueOf(LIKECOUNT));

        holder.productImage.setImageUrl(IMAGE_URL, imageLoader);
        holder.txt_itemName.setText(ITEM_NAME);
        holder.txt_itemName.setTypeface(tf);
        holder.txt_categoryName.setText(CATEGORY_NAME);
        holder.txt_itemName.setTypeface(tf1);
        holder.txt_Price.setText(PRICE);
        holder.txtFav.setText(String.valueOf(LIKECOUNT));
        holder.txtItemID.setText(ITEM_ID);

        holder.linearLayout.setVisibility(View.INVISIBLE);


        ITEMNAME.add(ITEM_NAME);
        CATEGORYNAME.add(CATEGORY_NAME);
        ITEM_PRICE.add(PRICE);
        LIKE_COUNT.add(String.valueOf(LIKECOUNT));


        Log.e("Card Adapter", "Product Name" + String.valueOf(ITEMNAME) + "  Category Name" + CATEGORYNAME + " Price" + ITEM_PRICE + "Restaurant Product" + restaurant_prducts);

        holder.productImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(view.getContext(), ProductActivity.class);
                i.putExtra("itemName", holder.txt_itemName.getText().toString());
                i.putExtra("categoryName", holder.txt_categoryName.getText().toString());
                i.putExtra("Price", Integer.parseInt(holder.txt_Price.getText().toString()));
                i.putExtra("like", Integer.parseInt(holder.txtFav.getText().toString()));
                i.putExtra("itemID", holder.txtItemID.getText().toString());

                view.getContext().startActivity(i);
            }
        });

        holder.iv_settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickCount++;

                if (v.isEnabled() == true) {
                    holder.linearLayout.setVisibility(View.VISIBLE);
                    holder.txt_categoryName.setVisibility(View.INVISIBLE);
                    holder.txt_itemName.setVisibility(View.INVISIBLE);
                    holder.iv_settings.setVisibility(View.INVISIBLE);
                }

            }
        });

        holder.viewDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Clicked ViewDetail", Toast.LENGTH_SHORT).show();
            }
        });
        holder.addToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Clicked AddToCart", Toast.LENGTH_SHORT).show();
            }
        });
        holder.checkOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Clicked CheckOut", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.e("Restaurant Size", String.valueOf(restaurant_prducts
                .size()));
        return restaurant_prducts.size();
    }

    public void setFilter(String query) {
        Log.e("Adapter Query", query);

        if (ITEMNAME.contains(query)) {
            Log.e("Inside of SetFilter", " Res");
            Log.e("Adapter", String.valueOf(ITEMNAME));
        }

        Log.e("Adapter Query", String.valueOf(ITEMNAME) + " " + CATEGORYNAME + " " + ITEM_PRICE + " ");
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public NetworkImageView productImage;
        public TextView txt_categoryName, txtFav, txtItemID;
        public TextView txt_itemName, txt_Price;
        public CardView cardlist_item;
        public ImageView iv_settings;
        public LinearLayout linearLayout;
        public Button viewDetail, addToCart, checkOut;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_settings = (ImageView) itemView.findViewById(R.id.iv_settings);
            cardlist_item = (CardView) itemView.findViewById(R.id.cardlist_item);
            txt_itemName = (TextView) itemView.findViewById(R.id.txt_itemName);
            txt_Price = (TextView) itemView.findViewById(R.id.txt_Price);
            txt_categoryName = (TextView) itemView.findViewById(R.id.txt_categoryName);
            productImage = (NetworkImageView) itemView.findViewById(R.id.productImage);
            txtFav = (TextView) itemView.findViewById(R.id.txtFav);
            txtItemID = (TextView) itemView.findViewById(R.id.txtItemID);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linearTransparent);
            viewDetail = (Button) itemView.findViewById(R.id.butViewDetail);
            addToCart = (Button) itemView.findViewById(R.id.but_addTCart);
            checkOut = (Button) itemView.findViewById(R.id.butCheckOut);
        }
    }
}

xml代码:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardlist_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/curved_shape"
    android:orientation="vertical"
    card_view:cardElevation="7dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:background="#FFF"
        android:orientation="horizontal">

        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/productImage"
            android:layout_width="150dp"
            android:layout_height="130dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:background="@drawable/round_shape"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/product_2" />

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_alignLeft="@+id/productImage"
            android:layout_alignStart="@+id/productImage"
            android:layout_below="@+id/productImage"
            android:background="@drawable/curved_shape"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingLeft="3dp"
            android:paddingRight="4dp"
            android:weightSum="1">

            <TextView
                android:id="@+id/listitem_name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_gravity="center"
                android:layout_weight="0.1"
                android:textStyle="bold"
                android:text="@string/Rs"
                android:textSize="12dp" />

            <TextView
                android:id="@+id/txt_Price"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_weight="0.25"
                android:text="120"
                android:textSize="12dp" />

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.07"
                android:src="@drawable/ic_favorite" />

            <TextView
                android:id="@+id/txtFav"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.20"
                android:text=" 153"
                android:textSize="10dp" />

            <TextView
                android:id="@+id/txtItemID"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.00"
                android:textSize="0dp" />

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.07"
                android:gravity="center"
                android:src="@drawable/ic_schedule" />

            <TextView
                android:id="@+id/txtTime"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:gravity="center"
                android:text="30 min"
                android:textSize="10dp" />
        </LinearLayout>


        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="150dp"
            android:layout_height="130dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:background="@drawable/transparent_layout" />

        <TextView
            android:id="@+id/txt_itemName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/txt_categoryName"
            android:layout_alignLeft="@+id/productImage"
            android:layout_alignStart="@+id/productImage"
            android:layout_marginLeft="8dp"
            android:text="Veg Burger"
            android:textColor="@color/white"
            android:textSize="16dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txt_categoryName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/linearLayout"
            android:layout_alignLeft="@+id/productImage"
            android:layout_alignStart="@+id/productImage"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="2dp"
            android:text="Category Name"
            android:textColor="@color/white"
            android:textSize="11dp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/iv_settings"
            android:layout_width="30dp"
            android:layout_height="35dp"
            android:layout_alignBottom="@+id/txt_categoryName"

            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/txt_itemName"
            android:foregroundGravity="right"
            android:src="@drawable/ic_settings3" />

        <LinearLayout
            android:id="@+id/linearTransparent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageView4"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@color/transparent_black"
            android:orientation="vertical"
            android:visibility="visible">

            <Button
                android:id="@+id/butViewDetail"
                android:layout_width="120dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="8dp"
                android:background="@color/white"
                android:text="View Detail" />

            <Button
                android:id="@+id/but_addTCart"
                android:layout_width="120dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="8dp"
                android:background="@color/green"
                android:text="ADD TO CART"
                android:textColor="@color/white" />

            <Button
                android:id="@+id/butCheckOut"
                android:layout_width="120dp"
                android:layout_height="30dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="8dp"
                android:background="@color/orange"
                android:text="CHECK  OUT"
                android:textColor="@color/white" />
        </LinearLayout>
    </RelativeLayout>

</android.support.v7.widget.CardView>

3 个答案:

答案 0 :(得分:2)

在适配器中创建一个名为clickedPos的int全局变量,如int clickedPos = -1;bindViewHolder写 -

if (clickedPos == position) {
    holder.linearLayout.setVisibility(View.VISIBLE);
    holder.txt_categoryName.setVisibility(View.INVISIBLE);
    holder.txt_itemName.setVisibility(View.INVISIBLE);
    holder.iv_settings.setVisibility(View.INVISIBLE);
} else {
    holder.linearLayout.setVisibility(View.INVISIBLE);
    holder.txt_categoryName.setVisibility(View.VISIBLE);
    holder.txt_itemName.setVisibility(View.VISIBLE);
    holder.iv_settings.setVisibility(View.VISIBLE);
}

holder.iv_settings.setOnClickListener(new View.OnClickListener()
{
        @Override
        public void onClick(View v)
        {
           clickedPos = position;
           notifyDataSetChanged();
        }
    });

您必须这样做,因为recyclerview会重复使用视图。

答案 1 :(得分:1)

Hy我认为您应该使用clickListener并将黑色布局叠加在cardview中,默认情况下,黑色布局的可见性已经消失,此处是步骤:

  • 用户点击3点
  • recycleview迭代它的项目并在所有cardviews中将黑色背景可见性设置为false,除了单击的一个(您可以添加条件检查)
  • 当用户点击另一个的卡片视图时,该步骤会重复,因此一个。

这是我的观点,但可能是更好的解决方案。 抱歉我的英语不好:)工作顺利,祝你好运

答案 2 :(得分:1)

try this 
if (pos == position) 
{
    holder.linearLayout.setVisibility(View.VISIBLE);
    holder.txt_categoryName.setVisibility(View.INVISIBLE);
    holder.txt_itemName.setVisibility(View.INVISIBLE);
    holder.iv_settings.setVisibility(View.INVISIBLE);
}

holder.iv_settings.setOnClickListener(new View.OnClickListener()
{
        @Override
        public void onClick(View v)
        {
           pos = position;
           notifyDataSetChanged();
        }
    });