您好我对CardViews
中的RecyclerView
有疑问。我的CardViews基本上有2个段,即基卡和扩展卡。我的OnClick
只是将每个细分的setVisibility
属性设置为GONE
或VISIBLE
。现在我的OnClickListener
位于OnBindViewHolder
方法中。我已经读过将它放在ViewHolder
类中更好,但是,我不知道如何在此刻实现相同的结果。我的主要问题最好用以下图片表示:
这是图像显示的默认方式或“base_view”
当您点击图片时,该卡片将显示与该卡片相关的信息,即“expanded_view”。
当我更改一张卡的布局时,如何阻止其他CardView(中间和右侧卡)更改(从正方形到长矩形)? 示例如下:
这是我的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();
}
});
}
答案 0 :(得分:1)
为此,您必须存储您点击的项目的位置,例如int mClickedPosition
然后在onBindViewHolder()
方法中,你必须检查当前位置是否与mClickedPosition
相同,只需将expandview设为 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);
}
}
}
答案 1 :(得分:0)
@Override
public int getItemViewType(int position) {
//More to come
}
在RecyclerAdapter中重写此方法并传递适当的int类型ViewType
@Override
public 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
充气相应的视图参考链接:https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView