android 嵌套 RecyclerView 问题:View.GONE 不会立即 resize/redraw 嵌套 RecyclerView

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

我有一个应用可以受益于嵌套在 recyclerView 中的 recyclerView。

我遇到的问题是,当我在 recyclerView 的外部移除 textView 时,内部的 recyclerView 不会自行重绘。

我的应用程序允许用户阅读页面(具有多个段落的 RecyclerView(嵌套 RecyclerView) 用户可以单击章节标题来显示一些添加的 definitions/context,如果他单击定义,它就会消失。

问题是当定义消失时,嵌套的 RecyclerView 不会 redraw/re-size 本身没有来回滑动页面,这可以解决吗?

我不是 android 专家所以我可能做错了什么 :S

我的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>

我的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);
    }
}

我的 recyclerView 适配器非常基础:

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);
        }
    }
}

删除视图后,您是否在适配器上手动调用了 notifyDataSetChanged?