ExpandableTextView in CardView to Open and Close w

2020-03-07 06:27发布

I have a CardView which has ExpandableTextView as Child of CardView when i was clicked on CardView the Expandable Text View expand and also in next click it was closed but the Main Problem is that When I click to Expandable Text View it was expanded and also some of other CardView's Expandable TextView in the RecycleView also expanded without clicked on that CardView. i was searched for the reason but not able to get any proper solution and i am new in development of Material's concept Layout So Please help me in My Following code. Thanks in Advance.

The following is my code for RecycleView Adapter.

public class TestRecyclerViewAdapter extends RecyclerView.Adapter<TestRecyclerViewAdapter.PersonViewHolder> {


FloatingActionMenu menubutton;
Context con;
Handler handler;
public ExpandableTextView previous_text_view = null;

public static class PersonViewHolder extends RecyclerView.ViewHolder {

    CardView cv;
    TextView personName;
    TextView personAge;
    ImageView personPhoto;
    ExpandableTextView expandableTextView;
    CustomObject object;
    // FloatingActionButton button;


    PersonViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cv);
        expandableTextView = (ExpandableTextView) itemView.findViewById(R.id.expandingTextView);
        // button = (FloatingActionButton)itemView.findViewById(R.id.action_c);

    }

    public void bindexpand(CustomObject cob){
        object = cob;
        //expandableTextView.isExpanded(object.isExpanded());
    }
}

List<CustomObject> contents;

static final int TYPE_HEADER = 0;
static final int TYPE_CELL = 1;

public TestRecyclerViewAdapter(List<CustomObject> contents, FloatingActionMenu button, Context context) {
    this.contents = contents;
    menubutton = button;
    con = context;
}

@Override
public int getItemViewType(int position) {
    switch (position) {
        case 0:
            return TYPE_HEADER;
        default:
            return TYPE_CELL;
    }
}

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

@Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = null;

    switch (viewType) {
        case TYPE_HEADER: {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item_card_big, parent, false);
            return new PersonViewHolder(view) {
            };
        }
        case TYPE_CELL: {

            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item_card_small, parent, false);
            final PersonViewHolder pvh = new PersonViewHolder(view);
            return pvh;
        }
    }
    return null;
}


@Override
public void onBindViewHolder(final PersonViewHolder holder, final int position) {
    switch (getItemViewType(position)) {
        case TYPE_HEADER:
            break;
        case TYPE_CELL:
            // holder.button.setColorNormalResId(R.color.blue);
            final int pos = position;
            holder.cv.setTag(new Integer(pos));
            final CustomObject c = contents.get(pos);
            holder.cv.setOnClickListener(new View.OnClickListener() {
                @TargetApi(Build.VERSION_CODES.HONEYCOMB)
                @Override
                public void onClick(View v) {
                    holder.expandableTextView.toggleExpansion();
                    CardView cvdata = (CardView)v;
                    int clickedPos = ((Integer)cvdata.getTag()).intValue();
                    if (holder.expandableTextView.isExpanded()) {
                            for (int i=1;i<contents.size();i++){
                                if(clickedPos != i && contents.get(i).isExpanded() == true){
                                    c.setIsExpanded(false);
                                    notifyItemChanged(i);
                                }
                            }
                        menubutton.showMenuButton(true);
                        contents.get(clickedPos).setIsExpanded(true);
                    } else {
                        menubutton.hideMenuButton(true);
                    }


                }
            });
            break;
    }
}
}

Now, The following is the code for My Custom Object class:

public class CustomObject {

String data;
boolean isExpanded;

public CustomObject(){

}

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}


public boolean isExpanded() {
    return isExpanded;
}

public void setIsExpanded(boolean isExpanded) {
    this.isExpanded = isExpanded;
}


}

Last One, the code for cardview's xml layout.

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



        <android.support.v7.widget.CardView
            android:id="@+id/cv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="2dp"
            app:cardElevation="2dp"
            android:layout_marginBottom="@dimen/cardMarginVertical"
            android:layout_marginLeft="@dimen/cardMarginHorizontal"
            android:layout_marginRight="@dimen/cardMarginHorizontal"
            android:layout_marginTop="@dimen/cardMarginVertical"
            app:cardPreventCornerOverlap="false"
            app:contentPadding="0dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <com.github.florent37.materialviewpager.sample.ExpandableTextView
                android:id="@+id/expandingTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:layout_marginLeft="10dp"
                android:maxLines="3"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:text="@string/longText" />
            </LinearLayout>
        </android.support.v7.widget.CardView>



</FrameLayout>

0条回答
登录 后发表回答