android嵌套RecyclerView问题:View.GONE不会立即调整/重新绘制嵌套RecyclerView的大小

时间:2019-12-12 12:46:28

标签: android android-recyclerview nested draw visibility

我有一个应用程序,它将受益于嵌套在recyclerView中的recyclerView。

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

我的应用允许用户阅读页面(带有多个段落的RecyclerView(嵌套的RecyclerView) 用户可以单击章节标题以显示一些添加的定义/上下文,如果单击该定义,它将消失。

问题是当定义消失时,如果没有来回滑动页面,嵌套的RecyclerView不会重新绘制/调整大小,这可以解决吗?

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

Application started

View Added

View removed - Nested RecyclerView Not resized/re-drawed

Swiping page back and forth redraw the nested RecyclerView properly

1 个答案:

答案 0 :(得分:1)

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