-4

嗨,我有一个关于.My CardViews 的问题CardViewsRecyclerView基本上有 2 个部分,即基卡和扩展卡。我OnClick只是将setVisibility每个段的属性设置为GONEor 或VISIBLE。现在我OnClickListenerOnBindViewHolder方法是。我读过最好把它放在ViewHolder课堂上,但是,我现在不确定如何达到相同的结果。我的主要问题最好用下面的图片来表示:

这是图像应显示的默认方式或“base_view”

基础卡片视图

当您单击图像时,卡片将显示与该卡片相关的信息,即“expanded_view”。

点击时展开 CardView

当我更改一张卡片的布局时,如何防止其他 CardViews(中间和右侧卡片)发生变化(从正方形变为长矩形)?示例如下:

在此处输入图像描述

这是我的 CardView XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/tools"
android:layout_gravity="center"
app:cardCornerRadius="8dp"
android:layout_margin="1dp"
android:id="@+id/cv_army">


<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:orientation="vertical"
    android:id="@+id/base_card">

    <ImageView
        android:layout_width="90dp"
        android:layout_height="120dp"
        android:layout_margin="2dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/summon_cm"
        android:scaleType="fitCenter"
        android:id="@+id/iv_card"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/iv_card"
        android:layout_marginBottom="10dp"
        android:layout_centerHorizontal="true"
        android:textColor="#000"
        android:text="Test"
        android:textAllCaps="false"
        android:textSize="10dp"
        android:textAlignment="center"
        android:background="#ffffff"
        android:backgroundTint="@color/trans_success_stroke_color"
        android:id="@+id/tv_card"/>

</RelativeLayout>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="100dp"
    android:layout_height="150dp"
    android:layout_gravity="center"
    android:background="@color/colorPrimaryDark"
    android:id="@+id/expanded_card">

    <TextView
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:id="@+id/card_popup_name"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:layout_centerHorizontal="true"
        android:text="Card Name"/>


    <ImageView
        android:layout_width="90dp"
        android:layout_height="80dp"
        android:src="@drawable/bg_summon"
        android:scaleType="fitCenter"
        android:id="@+id/card_popup_img"
        android:layout_below="@id/card_popup_name"
        android:layout_marginTop="5dp"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="35dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/card_popup_img"
        android:layout_marginTop="10dp"
        android:layout_alignStart="@id/card_popup_img"
        android:id="@+id/card_popup_hp"
        android:text="HP"
        android:textAlignment="viewEnd"
        android:layout_gravity="end"/>

    <TextView
        android:text="123"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/card_popup_hp"
        android:layout_alignStart="@id/card_popup_img"
        android:layout_marginStart="45dp"
        android:id="@+id/tv_hp" />

    <Button
        android:text="Use"
        android:textSize="10dp"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="5dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/btn_use" />
</RelativeLayout>

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

我也把我的 onBindView 放在这里,以防万一:

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

    holder.base_card.setVisibility(View.VISIBLE);
    holder.expanded_card.setVisibility(View.GONE);
    final int p = position;
    String s = card_list.get(p).card_name;
    final String sx = s.substring(1); //The 1st position [index 0] stores the rarity of the card, so remove that from the string to get the name
    holder.tv_card.setText(sx);
    holder.iv_card.setImageResource(card_list.get(p).card_img);

    //RV_Animator.animate(holder);

    holder.iv_card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        holder.expanded_card.setVisibility(View.VISIBLE);
        holder.base_card.setVisibility(View.GONE);

            for (int ix =0; ix<getItemCount();ix++){
                if (ix!=p){
                    notifyItemChanged(ix);
                }
            }

        }
    });

    holder.expanded_card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View vy) {
            holder.expanded_card.setVisibility(View.GONE);
            holder.base_card.setVisibility(View.VISIBLE);
        }
    });

    holder.btn_use.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View vx) {
            new SweetAlertDialog(context, SweetAlertDialog.SUCCESS_TYPE)
                    .setTitleText("Success!")
                    .setContentText("asd")
                    .show();
        }
    });
}
4

2 回答 2

1

为此,您必须存储您单击的项目的位置,例如int mClickedPosition

然后在onBindViewHolder()方法中,您必须检查当前位置是否与 相同mClickedPosition,只需将扩展视图设置为VISIBLE,否则对于其他位置,将其设置为GONE

在你的 onBindViewHolder 里面,你必须检查这样的事情

if (mClickedPosition == position) {
        holder.base_card.setVisibility(View.GONE);
        holder.expanded_card.setVisibility(View.VISIBLE);
    } else {
        holder.base_card.setVisibility(View.VISIBLE);
        holder.expanded_card.setVisibility(View.GONE);
    }

根据要求更新了整个示例适配器代码:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View content_view = inflater.inflate(R.layout.list_item,parent,false);
        MyViewHolder holder = new MyViewHolder(content_view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.base_card.setVisibility(View.VISIBLE);
        holder.expanded_card.setVisibility(View.GONE);
        final int p = position;
        String s = "hasif";//card_list.get(p).card_name;
       // final String sx = s.substring(1); //The 1st position [index 0] stores the rarity of the card, so remove that from the string to get the name
        holder.tv_card.setText(s);
        //holder.iv_card.setImageResource(card_list.get(p).card_img);

        //RV_Animator.animate(holder);

        holder.iv_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                holder.expanded_card.setVisibility(View.VISIBLE);
                holder.base_card.setVisibility(View.GONE);

                for (int ix =0; ix<getItemCount();ix++){
                    if (ix!=p){
                        notifyItemChanged(ix);
                    }
                }

            }
        });

        holder.expanded_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vy) {
                holder.expanded_card.setVisibility(View.GONE);
                holder.base_card.setVisibility(View.VISIBLE);
            }
        });

        holder.btn_use.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vx) {
                //Toast.makeText()
            }
        });
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_card;
        private TextView tv_card;
        private RelativeLayout expanded_card;
        private RelativeLayout base_card;
        private Button btn_use;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv_card = (ImageView) itemView.findViewById(R.id.iv_card);
            tv_card = (TextView) itemView.findViewById(R.id.tv_card);
            expanded_card = (RelativeLayout) itemView.findViewById(R.id.expanded_card);
            base_card = (RelativeLayout) itemView.findViewById(R.id.base_card);
            btn_use = (Button) itemView.findViewById(R.id.btn_use);

        }
    }
}
于 2017-01-13T03:09:05.837 回答
0
@Override
public int getItemViewType(int position) {
     //More to come
}

在您的 RecyclerAdapter 中覆盖此方法并传递适当的 int 类型 ViewType

 @Override

公共 RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

  RecyclerView.ViewHolder viewHolder;
  LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

  switch (viewType) {
      case USER:
          View v1 = inflater.inflate(R.layout.layout_viewholder1, viewGroup, false);
          viewHolder = new ViewHolder1(v1);
          break;
      case IMAGE:
          View v2 = inflater.inflate(R.layout.layout_viewholder2, viewGroup, false);
          viewHolder = new ViewHolder2(v2);
          break;
      default:
          View v = inflater.inflate(android.R.layout.simple_list_item_1, viewGroup, false);
          viewHolder = new RecyclerViewSimpleTextViewHolder(v);
          break;
  }
  return viewHolder;

}

根据viewType对对应的view进行inflate

参考链接:https ://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView

于 2017-01-13T04:36:57.117 回答