Search code examples
androidandroid-recyclerviewnesteddrawvisibility

android nested RecyclerView issue: View.GONE doesn't instantly resize/redraw nested RecyclerView


I have an app that would benefit from a recyclerView nested in a recyclerView.

The issue I have though is that when I remove a textView outside on the recyclerView, the inner recyclerView doesn't redraw itself.

My app allows the user to read pages (RecyclerView with multiple paragraphs (nested RecyclerView) The user can click the chapter title to get some added definitions/context displayed and if he clicks the definition it goes away.

The issue is when the definition goes away the nested RecyclerView doesn't redraw/re-size itself without a page swipe back and forth, could this be resolved?

I'm not android expert so I'm probably doing something wrong :S

My activity_main.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="match_parent">

    <LinearLayout
        android:id="@+id/pageContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/definitionContainer">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/page"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="15dp"/>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/definitionContainer"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:visibility="gone"
        android:onClick="hideDefinition">
        <View android:id="@+id/devider" android:layout_marginTop="1dp" android:layout_marginBottom="1dp" android:background="@color/colorAccent" android:layout_width="match_parent" android:layout_height="2px" android:layout_alignParentTop="true"/>
        <ScrollView
            android:id="@+id/definitionScrollView"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:layout_below="@+id/devider"
            android:background="@color/colorPrimaryDark"
            android:onClick="hideDefinition">
            <TextView
                android:id="@+id/definitionTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:text="Lorem ipsum dolor sit amet, ..."
                android:onClick="hideDefinition" />
        </ScrollView>
    </RelativeLayout>
</RelativeLayout>

My MainActivity.java

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.page) RecyclerView page;
    @BindView(R.id.definitionContainer) RelativeLayout definitionContainer;

    private ChapterContainerAdapter chapterAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        //if (page == null) page = findViewById(R.id.page);
        page.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        page.setItemAnimator(new DefaultItemAnimator());
        chapterAdapter = new ChapterContainerAdapter(this);
        page.setAdapter(chapterAdapter);

        SnapHelper snapHelper = new LinearSnapHelper();
        snapHelper.attachToRecyclerView(page);
    }

    public void showDefinition (View v) {
        definitionContainer.setVisibility(View.VISIBLE);
    }
    public void hideDefinition (View v) {
        definitionContainer.setVisibility(View.GONE);
    }
}

My recyclerView Adapter is very basic:

public class ChapterContainerAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private Context ctx;
    static final String pageData[] = {"Page 1", "Page 2", "Page 3", "Page 4", "Page 5"};

    public ChapterContainerAdapter(Context ctxIn) { ctx = ctxIn; }
    @Override public void onBindViewHolder(BaseViewHolder holder, int position) { holder.onBind(position); }

    @Override public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChapterContainerAdapter.ChapterAdapterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_page, parent, false));
    }

    @Override public int getItemCount() { return pageData.length; }

    public class ChapterAdapterViewHolder extends BaseViewHolder {
        @BindView(R.id.chapterTitle)
        TextView chapterTitle;

        @BindView(R.id.chapterContentRecyclerView)
        RecyclerView chapterContentRecyclerView;
        ParagraphAdapter paragraphAdapter;

        public ChapterAdapterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        protected void clear() { chapterTitle.setText(""); }

        public void onBind(int position) {
            super.onBind(position);
            initiateVerseRecyclerView(position);
        }

        private void initiateVerseRecyclerView (int position) {
            chapterTitle.setText(pageData[position]);
            chapterContentRecyclerView.setLayoutManager(new LinearLayoutManager(ctx, RecyclerView.VERTICAL, false));
            chapterContentRecyclerView.setItemAnimator(new DefaultItemAnimator());
            paragraphAdapter = new ParagraphAdapter(ctx, position);
            chapterContentRecyclerView.setAdapter(paragraphAdapter);
        }
    }
}

Application started

View Added

View removed - Nested RecyclerView Not resized/re-drawed

Swiping page back and forth redraw the nested RecyclerView properly


Solution

  • Did you to call notifyDataSetChange manually on adapter after removing view?