如何解决xml布局中RecyclerView上消失的小部件?

时间:2019-04-19 12:59:22

标签: java android xml android-layout android-recyclerview

我有一个 RecyclerView ,其中包含具有单选按钮的项目... 我想在2秒后刷新我的RecyclerView项目数据,并显示一个加载小部件,并在用户单击RadioButtons的同时使RecylerView滚动到第一项。

除了加载和RecylerView小部件可见性 ...

之外,一切都已正确完成

加载小部件和RecyclerView可见性不变。

这是我的xml

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recy_billing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/materialCardView7"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

        <com.airbnb.lottie.LottieAnimationView
            android:id="@+id/loading_order"
            android:layout_width="@dimen/loading"
            android:layout_height="@dimen/loading"
            android:layout_gravity="center"
            android:visibility="invisible"
            app:layout_constraintBottom_toTopOf="@id/materialCardView7"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:lottie_autoPlay="true"
            app:lottie_fileName="loading.json"
            app:lottie_loop="true" />

这是我的代码。

public void updateAddressSelectionRecy() {

    try {

        mRecyBilling.setVisibility(View.GONE);
        mSelectAddressLoading.setVisibility(View.VISIBLE);
        mRecyBilling.getAdapter().notifyDataSetChanged();
        mRecyBilling.scrollToPosition(0);

        Thread.sleep(SLEEP_TIME);
        mRecyBilling.setVisibility(View.VISIBLE);
        mSelectAddressLoading.setVisibility(View.GONE);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:0)

mRecyBilling.getAdapter().notifyDataSetChanged();区块之前再次呼叫catch()

即:

public void updateAddressSelectionRecy() {

    try {

        mRecyBilling.setVisibility(View.GONE);
        mSelectAddressLoading.setVisibility(View.VISIBLE);
        mRecyBilling.getAdapter().notifyDataSetChanged();
        mRecyBilling.scrollToPosition(0);

        Thread.sleep(SLEEP_TIME);
        mRecyBilling.setVisibility(View.VISIBLE);
        mSelectAddressLoading.setVisibility(View.GONE);
        mRecyBilling.getAdapter().notifyDataSetChanged();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}