在recyclerview上更改方向期间再次从Internet加载数据

时间:2019-07-01 15:45:02

标签: android

有RecyclerView,并且在方向更改期间,我希望数据不会再次从Internet加载。你是怎样做的?

这是代码

    package picodiploma.dicoding.database.picodiploma.dicoding.database.tv;


import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import java.util.ArrayList;
import java.util.List;

import picodiploma.dicoding.database.ApiClient;
import picodiploma.dicoding.database.ApiInterface;
import picodiploma.dicoding.database.R;
import retrofit2.Call;
import retrofit2.Callback;

import static android.view.View.VISIBLE;


/**
 * A simple {@link Fragment} subclass.
 */
public class TvShow extends Fragment  {

    private static RecyclerView recyclerView;
    private static final String API_KEY = "2e08750083b7e21e96e915011d3f8e2d";
    private static final String TAG = TvShow.class.getSimpleName();
    private static ProgressBar progressBar;
    private static List<ResultsItem> resultsTv = new ArrayList<>();
    private static TvShowRecyclerAdapter recyclerAdapter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public TvShow() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.fragment_tv_show, container, false);

        recyclerView = view.findViewById(R.id.recycler_tv);
        progressBar = view.findViewById(R.id.progress_bar);

        progressBar.setVisibility(VISIBLE);


        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);

        getData();

        if (savedInstanceState == null) {
            getData();
        }
        else  {
            resultsTv = savedInstanceState.getParcelableArrayList("tv");
            recyclerAdapter.refill(resultsTv);
        }


        return view;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putParcelableArrayList("tv", (ArrayList<? extends Parcelable>) resultsTv);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_change_settings){
            Intent mIntent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
            startActivity(mIntent);
        }
        return super.onOptionsItemSelected(item);
    }

    public void getData() {
        ApiInterface apiInterface = ApiClient.getList().create(ApiInterface.class);

        Call<Response> responseCall = apiInterface.getTvList(API_KEY);
        responseCall.enqueue(new Callback<Response>() {
            @Override
            public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
                resultsTv = response.body().getResults();
                recyclerAdapter = new TvShowRecyclerAdapter(resultsTv, getContext());
                recyclerView.setAdapter(recyclerAdapter);
                recyclerView.setVisibility(VISIBLE);
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(Call<Response> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
                Log.d(TAG, t.toString());
            }
        });
    }


}

现在,在重新定向之后,可以再次从Internet更改RecyclerView加载数据。我想让它使用Fragment上以前保存的数据。顺便说一句,Fragment也连接到MainActivity上的FragmentPagerAdapter。谢谢您的回答。

2 个答案:

答案 0 :(得分:1)

处理这类情况的推荐方法-处理方向更改-无需从网络中获取数据,是使用Google的Android体系结构组件中的ViewModel。

ViewModel可能如下所示

public class YourViewModel extends ViewModel {

private MutableLiveData<List<ResultsItem>> resultsItem;

public LiveData<List< ResultsItem >> getData() {
    if (resultsItem == null) {
        resultsItem = new MutableLiveData<List<resultsItem>>();
        loadData();
    }
    return resultsItem;
}

private void loadData() {
    // Request your data from network
}
}

然后在您的Fragment

YourViewModel model = ViewModelProviders.of(this).get(YourViewModel.class);
    model.getData().observe(this, resultsItem -> {
        // here you can update UI - passing the list of data
    });

答案 1 :(得分:0)

Possible duplicate of this question。当设备方向改变时,android会创建特定活动或片段的新实例。因此数据将丢失。但是您可以覆盖活动/片段的onSaveInstanceState方法,然后将数据写入实现{在parcelable or serializable interface类和ResultItem中的{1}}中,您需要检查数据是否可用,否则需要从服务器加载数据。