为了账号安全,请及时绑定邮箱和手机立即绑定

活动最小化然后恢复后视图再次可见

活动最小化然后恢复后视图再次可见

慕妹3242003 2023-08-04 09:59:38
我的应用程序中出现了这个奇怪的事情。我有一个AppCompatActivity,ViewPager里面有两个片段。我已添加LottieAnimationView到这两个片段中。在FragmentA我使用从服务器获取数据Retrofit时,它隐藏了LottieAnimationView. 在FragmentB(仅用于检查问题所在)中,我只是用来Handler隐藏LottieAnimationView3 秒后的内容。现在,每当我最小化应用程序,然后再次打开它时,我都会看到LottieAnimationView我setVisibility(View.GONE)只在FragmentA. 在 中,当我最小化并再次打开应用程序(按预期工作)后,FragmentB我看不到视图。setVisibility(View.GONE)这是我在 中看到的图像FragmentA。处于LottieAnimationView暂停状态。这是我的代码FragmentA。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();    }
查看完整描述

2 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

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


查看完整回答
反对 回复 2023-08-04
?
波斯汪

TA贡献1811条经验 获得超4个赞

从 onCrete 中删除这一行并将其添加到 fetchDataFromServer() 方法中,如下所示。


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

    }

  @Override

    public void onResume() {

        super.onResume();

        lottieAnimationView.setVisibility(View.GONE);

    }

}



查看完整回答
反对 回复 2023-08-04
  • 2 回答
  • 0 关注
  • 115 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信