您对recyclerview卡翻转有何想法?

时间:2019-02-15 06:49:06

标签: android android-recyclerview flip cardview

我有一个带有Cardview项目的Recyclerview,我只想在我的Recyclerview上显示一个项目,它是我卡的正面。当我单击卡片正面时,它是翻转卡片视图并显示背面卡片。在此布局中,我也有一个按钮来显示下一张卡片。我的代码在下面,但是问题是当我单击第一张卡片时,它会翻转并显示回来,但是第四张卡片也翻转了,我该如何解决?

主要活动

public class ReviewActivity extends AppCompatActivity {

Toolbar toolbar;

private ReviewAdapterRecyclerView adapter;
private LinearLayoutManager mLayoutManager;
private RecyclerView recyclerView;
private ArrayList<Card> cards;
// Database Helper
MyDatabaseHelper db;

public CardView f,b;
public LinearLayout dialog;
private boolean isShowingBack = false;
public ImageButton nxt;
public int cPos = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_review);

    setupToolbar();

    assert getSupportActionBar() != null;
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    db = new MyDatabaseHelper(getApplicationContext());

    cards = db.getAllCards();

    recyclerView = (RecyclerView) findViewById(R.id.recycler_review);
    recyclerView.setHasFixedSize(true);
    //mLayoutManager = new LinearLayoutManager(this);
    mLayoutManager = new SnappingLinearLayoutManager(getApplicationContext());
    //recyclerView.setLayoutManager(new SnappingLinearLayoutManager(this,VERTICAL,false));
    recyclerView.setLayoutManager(new SnappingLinearLayoutManager(getApplicationContext()));
    recyclerView.setLayoutManager(mLayoutManager);

    adapter = new ReviewAdapterRecyclerView(cards, this);

    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    dialog = (LinearLayout) findViewById(R.id.review_dialog);

    recyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(getApplicationContext(),recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (findViewById(R.id.card_front).getVisibility()==GONE) {
                        b = (CardView) view.findViewById(R.id.card_front);
                        f = (CardView) view.findViewById(R.id.card_back);
                        dialog.setVisibility(View.GONE);
                    } else {
                        f = (CardView) view.findViewById(R.id.card_front);
                        b = (CardView) view.findViewById(R.id.card_back);
                        dialog.setVisibility(View.VISIBLE);
                   }
                    f.animate().rotationX(-90).setDuration(300).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            f.setVisibility(View.GONE);
                            b.setRotationX(90);
                            b.setVisibility(View.VISIBLE);
                            b.animate().rotationX(0).setDuration(300).setListener(null);
                        }
                    });
                    isShowingBack=!isShowingBack;
                }

                @Override
                public void onLongItemClick(View view, int position) {
                    // do whatever
                }
            })
    );
    nxt = (ImageButton) findViewById(R.id.showNextCard);
    nxt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //myRecyclerView.getLayoutManager().scrollToPosition(cPos++);
            if(cPos==cards.size()){
                Toast.makeText(ReviewActivity.this, "finish", Toast.LENGTH_SHORT).show();
                finish();
            }
            isShowingBack = !isShowingBack;
            recyclerView.getLayoutManager().scrollToPosition(cPos++);
            dialog.setVisibility(View.GONE);
        }
    });
}

Recyclerview适配器

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

private ArrayList<Card> cardList;
private Context ctx;

public ReviewAdapterRecyclerView(ArrayList<Card> cards, Context context) {
    this.cardList = cards;
    this.ctx = context;
}

class MyViewHolder extends RecyclerView.ViewHolder {
    public CardView fr,bk;
    public TextView txtFront, txtBack, txtFrontHeader, txtBackHeader;
    public View view;

    public MyViewHolder(final View itemView) {
        super(itemView);
        view=itemView;
        txtFrontHeader = (TextView) itemView.findViewById(R.id.front_header_txt);
        txtBackHeader = (TextView) itemView.findViewById(R.id.back_header_txt);
        txtFront = (TextView) itemView.findViewById(R.id.display_card_front_text);
        txtBack = (TextView) itemView.findViewById(R.id.display_card_back_text);
        fr=(CardView) itemView.findViewById(R.id.card_front);
        bk=(CardView) itemView.findViewById(R.id.card_back);
    }
}

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

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

    final Card card = cardList.get(position);
    String s = ctx.getString(R.string.box);
    holder.txtFrontHeader.setText(card.getGroupName() + " " + card.getId());
    holder.txtBackHeader.setText(s + " " + boxString(card.getBoxNo()));
    holder.txtFront.setText(card.getFront());
    holder.txtBack.setText(card.getRear());
}

private String boxString(int x) {
    String s = "";
    switch (x) {
        case 1:
            s = ctx.getString(R.string.first);
            break;
        case 2:
            s = ctx.getString(R.string.second);
            break;
        case 3:
            s = ctx.getString(R.string.third);
            break;
        case 4:
            s = ctx.getString(R.string.fourth);
            break;
        case 5:
            s = ctx.getString(R.string.fifth);
            break;

    }
    return s;
}

@Override
public int getItemCount() {
    return cardList.size();
}
}

以及回收站项目的布局

cards_review_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <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/card_front"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_margin="20dp"
        android:descendantFocusability="blocksDescendants"
        card_view:cardCornerRadius="10dp"
        android:clickable="true"
        card_view:cardElevation="0dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        <LinearLayout
            style="@style/CardFrontHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/front_header_txt"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                style="@style/CardFrontHeaderText"
                android:text="FrontHeader" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/display_card_front_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/CardTextReview"
                android:text="Front" />
        </LinearLayout>
        </LinearLayout>

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

    <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/card_back"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_margin="20dp"
        android:descendantFocusability="blocksDescendants"
        android:visibility="gone"
        card_view:cardCornerRadius="10dp"
        card_view:cardElevation="0dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                style="@style/CardBackHeader"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/back_header_txt"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    style="@style/CardBackHeaderText"
                    android:text="BackHeader" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/display_card_back_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    style="@style/CardTextReview"
                    android:text="Back" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

</RelativeLayout>

0 个答案:

没有答案