因此,我的应用程序中出现了这种奇怪的事情。

考虑到这一点,我有一个AppCompatActivity,其中有ViewPager,其中包含两个片段。我在两个片段中都添加了LottieAnimationView

FragmentA中,我使用Retrofit从服务器获取数据,并且在加载时隐藏了LottieAnimationView。在FragmentB(仅用于检查问题)中,我只是使用Handler在3秒后隐藏LottieAnimationView

现在,每当我最小化该应用程序,然后再次将其打开时,我只会在LottieAnimationView中看到setVisibility(View.GONE)。在FragmentA中,再次最小化并打开应用程序后(按预期工作),我在FragmentB时看不到视图。

这是我在setVisibility(View.GONE)中看到的图像。

java - 最小化 Activity 然后将其还原后, View 再次可见-LMLPHP

FragmentA处于暂停状态。

这是我的LottieAnimationView的代码。

public class FragmentA extends Fragment {

    private AdapterEventStore mAdapter;
    private List<Item> mStore;
    private final String hostName = "https://xxx.xxx.xxx";
    private View view;
    private Context context;
    private LottieAnimationView lottieAnimationView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_a, container, false);
        context = view.getContext();
        lottieAnimationView = view.findViewById(R.id.ais_lav_loading);
        lottieAnimationView.setVisibility(View.VISIBLE);
        initRecyclerView();
        return view;
    }


    private void initRecyclerView() {
        RecyclerView store = view.findViewById(R.id.ais_rv_event_store);
        store.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mStore = new ArrayList<>();
        mAdapter = new AdapterEventStore(context, mStore);
        store.setAdapter(mAdapter);
        fetchDataFromServer();
    }

    private void fetchDataFromServer() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);

        final Retrofit retrofit = new Retrofit.Builder()
                .client(httpClient.build())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(hostName)
                .build();
        APIGetItem itemShop = retrofit.create(APIGetItem.class);

        Call<ModelEventExclusive> call = itemShop.getEventStore(hostName);
        call.enqueue(new Callback<ModelEventExclusive>() {
            @Override
            public void onResponse(Call<ModelEventExclusive> call, Response<ModelEventExclusive> response) {
                Log.e("Response: ", response.body().getItems().get(0).getItemName());
                mEventStore.addAll(response.body().getItems());
                mAdapter.notifyDataSetChanged();
                hideLottieAnimation();
            }

            @Override
            public void onFailure(Call<ModelEventExclusive> call, Throwable t) {
                hideLottieAnimation();
                Toast.makeText(context, "Error:"+t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                Log.e("Error occurred: ", t.getMessage());
            }
        });
    }

    private void hideLottieAnimation(){
        lottieAnimationView.cancelAnimation();
        lottieAnimationView.setVisibility(View.GONE);
    }
}


FragmentA的布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/ais_rv_event_store"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/ais_lav_loading"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center"
        app:lottie_autoPlay="true"
        app:lottie_loop="true"
        android:visibility="gone"
        app:lottie_repeatMode="reverse"
        app:lottie_rawRes="@raw/loading" />
</FrameLayout>


请注意,首先将FragmentA的可见性设置为LottieAnimationView
上面的代码可以100%读取项目,请不要指出语法/变量名中的任何错误,因为我在尝试隐藏实际的变量/类名时做得不好。

这是GONE的代码

public class FragmentB extends Fragment {
    private LottieAnimationView anim_loading;
    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_b, container, false);
        anim_loading = view.findViewById(R.id.lav_loading);
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                anim_loading.cancelAnimation();
                anim_loading.setVisibility(View.GONE);
            }
        }, 3000);
        return view;
    }
}


你能告诉我我在做什么错吗?为什么它在一个片段中能按预期工作,而在另一个片段中却不起作用?它与我如何将其隐藏在Retrofit回调中有什么关系吗?

最佳答案

问题是当您获取背景背景时,会调用onResume,因此您需要添加行以将视图隐藏在onResume中,而不是onCreateView

查看this以获得更多详细信息

10-06 06:46