Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在RecyclerView中扩展和折叠卡片?_Java_Android_Android Recyclerview_Android Cardview - Fatal编程技术网

Java 如何在RecyclerView中扩展和折叠卡片?

Java 如何在RecyclerView中扩展和折叠卡片?,java,android,android-recyclerview,android-cardview,Java,Android,Android Recyclerview,Android Cardview,我在互联网上浏览了20-30个链接,但没有一个是有用的,因为每个代码都有一定的限制我在RecyclerView中的RelativeLayout中有卡片,我想单击Details按钮展开这些卡片。CardView中只有文本视图和按钮,而不是列表。请查看我的代码,并帮助我如何实现这一点 activity\u phrase\u search.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android

我在互联网上浏览了20-30个链接,但没有一个是有用的,因为每个代码都有一定的限制我在RecyclerView中的RelativeLayout中有卡片,我想单击Details按钮展开这些卡片。CardView中只有文本视图和按钮,而不是列表。请查看我的代码,并帮助我如何实现这一点

activity\u phrase\u 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\u phrase\u 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>
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();
    }
}

RecycleServiceAdapterPhraseList.java:

<?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>
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();
    }
}
package in.hkcl.pdfsearcher;
导入android.content.Context;
导入android.content.Intent;
导入android.net.Uri;
导入android.support.transition.TransitionManager;
导入android.support.v7.widget.CardView;
导入android.support.v7.widget.RecyclerView;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.animation.animation;
导入android.widget.Button;
导入android.widget.ImageButton;
导入android.widget.LinearLayout;
导入android.widget.RelativeLayout;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.ArrayList;
/**
*由Android于2017年11月17日创建。
*/
公共类RecycleServiceAdapterPhraseList扩展了RecyclerView.Adapter{
私人法庭法官;
公共布尔值isExpanded=false;
公共类短语列表持有者扩展了RecyclerView.ViewHolder{
TextView文档名称,展开的_文本;
按钮细节;
图像按钮链接;
线性布局隐藏式布局;
/*可扩展的相对视图*/
公共短语列表持有者(查看项目视图){
超级(项目视图);
documentName=(TextView)itemView.findViewById(R.id.documentName);
detailsBtn=(按钮)itemviewbyd(R.id.details\u btn);
linkBtn=(ImageButton)itemView.findViewById(R.id.link\u 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)*/
}
}
公共RecycleServiceAdapterPhraseList(ArrayList mPhraseList){
this.mPhraseList=mPhraseList;
}
@凌驾
public RecycleServiceAdapterPhraseList.PhraseListHolder onCreateViewHolder(ViewGroup父级,int-viewType){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.component\u短语\u列表,parent,false);
短语列表持有者短语列表持有者=新短语列表持有者(视图);
返回短语列表持有者;
}
@凌驾
BindViewHolder上的公共无效(最终RecycleServiceAdapterPhraseList.PhraseListHolder,最终整型位置){
字符串documentNameWithoutUrl=mPhraseList.get(position).documentName;
holder.documentName.setText(documentNameWithoutUrl.substring(documentNameWithoutUrl.lastIndexOf('/')+1,documentNameWithoutUrl.length());
holder.linkBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意向i=新意向(意向.行动\视图);
i、 setData(Uri.parse(mPhraseList.get(position.getDocumentName());
v、 getContext().startActivity(i);
}
});
holder.detailsBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
/*Intent j=新的Intent(v.getContext(),occurrencedetailsactivity.class);
j、 putParcelableArrayListExtra(“发生”,mPhraseList.get(position.getoccurrencedetails());
v、 getContext().startActivity(j)*/
holder.expanded_text.setVisibility(View.VISIBLE);
holder.expanded_text.setEnabled(真);
holder.expanded_text.setText(mPhraseList.get(position.getNoOfocusences());
}
});
}
@凌驾
public int getItemCount(){
返回mPhraseList.size();
}
}

stackoverflow上的早期链接不符合我的代码,因此在实现它们时我面临很多问题。请帮助我如何做到这一点?

我对您的布局和适配器做了一些更改

//这是你的隐藏布局

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/expanded_text"
            />
    </LinearLayout>

告诉我它是否解决了您的问题

在我看来,卡的展开和折叠可以通过使用idexpanded_text设置文本视图高度的动画来完成,而不仅仅是切换可见性

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

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中的详细信息文本的展开/折叠中添加软动画效果。如果有任何问题,请告诉我


谢谢。:)

您的代码运行正常,您的详细信息中已经有扩展代码。只需检查“mPhraseList.get(position).getNoOfOccurences()”的值就可以了。对于empty,我认为它会为您提供空字符串。

如果您希望像这样精确,则会有所帮助,因为它不起作用。点击按钮,一些屏幕会打开几秒钟,然后消失。嗨,古尼特。请将id为“hidden_layout”的线性布局放置在“outerBoundaryCard”CardView视图组中,以便反映更改。嗨,西里尔:问题仍然是这样的:(@Guneet Kaur正如你代码中提到的,你试图在点击按钮时显示文本视图,但隐藏的布局不是cardview的一部分。如果是这样,你想扩展你的cardview,然后将你的线性布局放在cardview中。正如你所说,但我所能看到的是一个空白屏幕1-2秒,然后是放置按钮的屏幕。谢谢你的帮助亲爱的:)是的。问题在于“mPhraseList.get(position.getNoO”