JDoeKnought
JDoeKnought

Reputation: 85

RecyclerView with expandable Views

I have already found this post that contains an answer how to do it. But there are some things that don't work and I can't figure out why.

@Override
        public void onBindViewHolder(final ShipViewer.anAdapter.NumberViewHolder holder, final int position) {

            final int[] mExpandedPosition = {-1};
            final boolean isExpanded = position== mExpandedPosition[0];
            holder.details.setVisibility(isExpanded?View.VISIBLE:View.GONE);
            holder.itemView.setActivated(isExpanded);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mExpandedPosition[0] = isExpanded ? -1:position;
                    TransitionManager.beginDelayedTransition(recyclerView);
                    notifyDataSetChanged();
                }
            });
            holder.bind(position);
        }

First of, I can't resolve details. Details is a gone ConstraintLayout inside of my Viewholder:

<android.support.constraint.ConstraintLayout
        android:id="@+id/details"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="0dp"
        android:visibility="gone"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bigtextname">

        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </android.support.constraint.ConstraintLayout>

My second problem is that I can't resolve recyclerView. I have already tried inserting my adapter but that also doesn't work.

Lastly, is it fine that so many things are final here? This doesn't seem right, but if I don't do it it tells me that they need to be final to be accessed from an inner class.

My recyclerView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ShipRec"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

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

Initialization of RecyclerView:

View b = inflater.inflate(R.layout.shipviewerrecview, container, false);

                    if(!shipsCursor.moveToPosition(0)) {return b;}

                    shipropView = (RecyclerView) b.findViewById(R.id.ShipRec);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
                    shipropView.setLayoutManager(layoutManager);
                    shipropView.setHasFixedSize(true);
shippropAdapter = new ShipViewer.anAdapter(numberofField, Names, Type, Size, MaxSize, Amount);


                    ShipsDbHelper.close();
                    return b;

Upvotes: 1

Views: 175

Answers (1)

kostyabakay
kostyabakay

Reputation: 1689

Probably expandable-recycler-view library from bignerdranch will useful for you.

Upvotes: 1

Related Questions