使用动画在RecyclerView中展开/折叠CardView

时间:2017-11-29 17:22:12

标签: android android-recyclerview android-animation android-cardview

我尝试使用此动画folding-cell

我需要它在RecyclerView中作为Cardview,所有都运行惊人,但如果我尝试扩展/折叠一张卡,他扩展/崩溃更多它。我不知道是否随便和如何,但我不扩展/崩溃一次一张卡片。 这是我的适配器的代码:

public class AllCorsiAdapter extends RecyclerView.Adapter<AllCorsiAdapter.ScanViewHolder>{
    private List<MainActivity.Corso> corsiList;
    private Context context;
    // avatar
    private String emptyAvatar = "@drawable/contruction";
    private int imageRes;
    public AllCorsiAdapter(List<MainActivity.Corso> corsiList, Context context) {
        this.corsiList = corsiList;
        this.context = context;
     }
     @Override
     public ScanViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.single_card, null);
        ScanViewHolder scanViewHolder = new ScanViewHolder(view);
        return scanViewHolder;
    }
    @Override
    public void onBindViewHolder(final ScanViewHolder holder, int position) {
        MainActivity.Corso corso = corsiList.get(position);
        holder.tvNomeCorsoT.setText(corso.getNome());
        holder.tvNomeCorsoC.setText(corso.getNome());
        holder.tvInfo.setText(corso.getDay()+", " + corso.getHr());
        }
    }
    @Override
    public int getItemCount() {
        return corsiList.size();
    }
    public static class ScanViewHolder extends RecyclerView.ViewHolder {
        TextView tvNomeCorsoT;
        TextView tvNomeCorsoC;
        TextView tvInfo;
        ImageView ivCoverT;
        ImageView ivCoverC;
        CardView card;
        FoldingCell fc;

        public ScanViewHolder(final View itemView) {
            super(itemView);
            tvNomeCorsoT = (TextView) itemView.findViewById(R.id.tv_corsoname_title);
            tvNomeCorsoC = (TextView) itemView.findViewById(R.id.tv_corsoname_content);
            tvInfo = (TextView) itemView.findViewById(R.id.tv_infoCorso);
            card = (CardView) itemView.findViewById(R.id.cvSingleCorso);
            fc = (FoldingCell) itemView.findViewById(R.id.foldingCell);
            fc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    fc.toggle(false);
                }
            });
        }
    }
}

这是我的cardview .xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">

<android.support.v7.widget.CardView
    android:id="@+id/cvSingleCorso"
    android:layout_width="355dp"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="#edf7f9"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="7dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp">
    <com.ramotion.foldingcell.FoldingCell xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:folding-cell="http://schemas.android.com/apk/res-auto"
        android:id="@+id/foldingCell"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        folding-cell:animationDuration="1300"
        folding-cell:cameraHeight="20"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:foreground="?selectableItemBackground"
        android:clickable="true">

        <!-- CONTENT (UNFOLDED) LAYOUT (MUST BE AT LEAST 2x times BIGGER than content layout bellow)-->
        <include
            layout="@layout/cell_content_layout"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            tools:ignore="IncludeLayoutParam" />

        <!-- TITLE (FOLDED) LAYOUT (MUST BE AT LEAST 2x times SMALLER than content layout above) -->
        <include layout="@layout/cell_title_layout" />

    </com.ramotion.foldingcell.FoldingCell>

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

</RelativeLayout>

我认为问题出现在这部分代码中:

fc = (FoldingCell) itemView.findViewById(R.id.foldingCell);
fc.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        fc.toggle(false);
    }
});

在mainActivity.java中,我只是在for循环中使用它来创建卡片:

...
// cardView
corsiList.add(new Corso(nome,giorno,orario,cover));
...
RecyclerView recyclerViewScan = (RecyclerView) findViewById(R.id.rvAllCorsi);
recyclerViewScan.setLayoutManager(new LinearLayoutManager(MainActivity.this));
AllCorsiAdapter allCorsiAdapter = new AllCorsiAdapter(corsiList,MainActivity.this);
recyclerViewScan.setAdapter(allCorsiAdapter);
....

我已经尝试了所有的东西,并尝试了在线,但似乎没有人遇到过我的问题。

0 个答案:

没有答案