gpt4 book ai didi

java - 如何在RecyclerView中展开和折叠卡片?

转载 作者:行者123 更新时间:2023-12-02 11:58:48 33 4
gpt4 key购买 nike

我在互联网上浏览了 20-30 个链接,但没有一个有帮助,因为每个代码都有一定的局限性。 我在RecyclerView的RelativeLayout中有卡片,我想在单击“详细信息”按钮时展开它。 CardViews 里面只有 TextView 和按钮,而不是列表。请查看我的代码并帮助我如何实现这一目标。

activity_phrase_search.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_phrase_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="in.hkcl.pdfsearcher.PhraseSearchActivity"
android:background="#EEEEEE">

<include android:id="@+id/toolbar"
layout="@layout/toolbar_layout"></include>

<EditText
android:id="@+id/inputSearchPhrase"
android:layout_below="@id/toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:maxLines="1"
android:padding="5dp"
android:textColor="#004D40"
android:inputType="text"
android:ellipsize="end"
android:scrollHorizontally="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="@drawable/search_edittext"
android:ems="10"
android:hint="Search your phrase here ..."
android:drawableLeft="@drawable/search"
android:drawableRight="@drawable/deletetext"
android:drawablePadding="12dp"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclrviewPhrase"
android:layout_marginTop="8dp"
android:layout_below="@id/inputSearchPhrase"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:behavior_overlapTop="14dp" />

component_phrase_list.xml:

<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#9E9E9E"
android:clickable="true"
android:focusable="true"
android:paddingBottom="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
>
<android.support.v7.widget.CardView
android:id="@+id/outerBoundaryCard"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center"
android:elevation="4dp"
android:background="?android:attr/selectableItemBackground">
<RelativeLayout
android:id="@+id/visible_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp">
<TextView
android:id="@+id/documentName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:textStyle="bold"/>

<Button
android:id="@+id/details_btn"
android:layout_below="@id/documentName"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:text="Details"
android:textColor="#FFFFFF"
android:background="#FBCC38"
android:textStyle="bold"/>

<ImageButton
android:id="@+id/link_btn"
android:layout_below="@id/documentName"
android:src="@drawable/link"
android:layout_toLeftOf="@id/details_btn"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_width="50dp"
android:layout_height="40dp"
android:background="#FFFFFF"/>

</RelativeLayout>

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

<LinearLayout
android:id="@+id/hidden_layout"
android:layout_below="@id/outerBoundaryCard"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/expanded_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>

<View
android:id="@+id/hr"
android:layout_below="@id/hidden_layout"
android:layout_marginTop="5dp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#FFFFFF"></View>

</RelativeLayout>

RecyclerViewAdapterPhraseList.java:

package in.hkcl.pdfsearcher;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.transition.TransitionManager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
* Created by Android on 11/17/2017.
*/

public class RecyclerViewAdapterPhraseList extends RecyclerView.Adapter<RecyclerViewAdapterPhraseList.PhraseListHolder> {

private ArrayList<PhraseModel.PhraseList> mPhraseList;
public boolean isExpanded=false;


public class PhraseListHolder extends RecyclerView.ViewHolder {
TextView documentName,expanded_text;
Button detailsBtn;
ImageButton linkBtn;
LinearLayout hidden_layout;

/*RelativeLayout expandable_view;*/

public PhraseListHolder(View itemView) {
super(itemView);
documentName=(TextView)itemView.findViewById(R.id.documentName);
detailsBtn=(Button)itemView.findViewById(R.id.details_btn);
linkBtn=(ImageButton)itemView.findViewById(R.id.link_btn);
expanded_text=(TextView)itemView.findViewById(R.id.expanded_text);
hidden_layout=(LinearLayout)itemView.findViewById(R.id.hidden_layout);
/*expandable_view=(RelativeLayout)itemView.findViewById(R.id.expandable_view);*/
}
}

public RecyclerViewAdapterPhraseList(ArrayList<PhraseModel.PhraseList> mPhraseList) {
this.mPhraseList = mPhraseList;
}

@Override
public RecyclerViewAdapterPhraseList.PhraseListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.component_phrase_list,parent,false);
PhraseListHolder phraseListHolder=new PhraseListHolder(view);
return phraseListHolder;
}

@Override
public void onBindViewHolder(final RecyclerViewAdapterPhraseList.PhraseListHolder holder, final int position) {
String documentNameWithoutUrl=mPhraseList.get(position).documentName;

holder.documentName.setText(documentNameWithoutUrl.substring(documentNameWithoutUrl.lastIndexOf('/')+1, documentNameWithoutUrl.length()));
holder.linkBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(mPhraseList.get(position).getDocumentName()));
v.getContext().startActivity(i);

}
});

holder.detailsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*Intent j = new Intent (v.getContext(), OccurenceDetailsActivity.class);
j.putParcelableArrayListExtra("occurences",mPhraseList.get(position).getOccurenceDetails());
v.getContext().startActivity(j);*/
holder.expanded_text.setVisibility(View.VISIBLE);
holder.expanded_text.setEnabled(true);
holder.expanded_text.setText(mPhraseList.get(position).getNoOfOccurences());

}
});
}

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

stackoverflow 上的早期链接并不符合我的代码,因此我在实现它们时面临很多问题。请帮助我该怎么做?

最佳答案

在我看来,卡片的展开和折叠可以通过使用 id expanded_text 对 TextView 的高度进行动画处理来完成,而不仅仅是切换可见性。

展开/折叠动画的实现可以通过以下方式完成:

holder.detailsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*Intent j = new Intent (v.getContext(), OccurenceDetailsActivity.class);
j.putParcelableArrayListExtra("occurences",mPhraseList.get(position).getOccurenceDetails());
v.getContext().startActivity(j);*/


holder.expanded_text.setText(mPhraseList.get(position).getNoOfOccurences());

// Object Animator to animate the expanded_text TextView
ObjectAnimator scaleHeightAnim;

// Checking if the TextView is already visible, if "yes",
//animating and collapsing the TextView, else expanding the TextView.
if (expanded_text.getVisibility() == View.VISIBLE) {

AnimatorSet animatorSet = new AnimatorSet();

scaleHeightAnim = ObjectAnimator.ofFloat(expanded_text, "height", expanded_text.getHeight(), 0);

animatorSet.play(scaleHeightAnim);
animatorSet.setDuration(1000);
animatorSet.start();

animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
expanded_text.setVisibility(View.GONE);
}
});

} else {

AnimatorSet animatorSet = new AnimatorSet();

expanded_text.setVisibility(View.VISIBLE);
scaleHeightAnim = ObjectAnimator.ofFloat(expanded_text, "height", 0, expanded_text.getHeight());

animatorSet.play(scaleHeightAnim);
animatorSet.setDuration(1000);
animatorSet.start();
}

}
});

我希望这篇技巧可以帮助您为 CardView 中的详细信息文本的展开/折叠添加软动画效果。如果有任何疑虑,请告诉我。

谢谢。 :)

关于java - 如何在RecyclerView中展开和折叠卡片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47407361/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com