Android停止加载程序在返回导航后重新加载

时间:2016-06-02 09:19:51

标签: android listview navigation android-recyclerview loader

在我最新的Android应用程序中,我正在使用Loaders从互联网上加载数据。 我有一个RecyclerView,当我点击一个项目时,会启动一个新的Activity。但每次我向后导航时,Loader都会重新加载数据,因此会更新RecyclerView,从而跳转到列表的顶部。

我该如何阻止这种行为?

MainActivity

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

    if(savedInstanceState == null) {
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        pagerAdapter = new PagerAdapter(getApplicationContext(), getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(3);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }
}

ListFragment

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

    getLoaderManager().initLoader(0, null, this);
}

@Override
public Loader onCreateLoader(int id, Bundle args) {
    return new DataLoader(getActivity());
}

@Override
public void onLoadFinished(Loader<Object> loader, Object data) {
    adapter.data = data;
    adapter.notifyDataSetChanged();
}

启动新活动的代码

Intent intent = new Intent(view.getContext(), DetailActivity.class);
intent.putExtra(DetailFragment.ARG_DATA, data);
view.getContext().startActivity(intent);

返回导航

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}

编辑:添加了RecyclerView代码

public class ListItemRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public List<Data> data;

    public ListItemRecyclerViewAdapter(List<Data> data) {
        data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ListItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
         ((ListItemViewHolder) holder).setData(data.get(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}

2 个答案:

答案 0 :(得分:0)

遇到同样的问题......似乎Loader在onLoadFinished中调用了onResume。要解决此问题,请在destroyLoader

中致电onLoadFinished

此处有更多详情: Why is onLoadFinished called again after fragment resumed?

答案 1 :(得分:0)

覆盖deliverResult并缓存先前加载器的数据并存储现在onStartLoading检查存储的变量是否包含数据如果不强制加载否则使用缓存的数据这种方式将阻止数据重新加载 顺便说一句,我是 android 新手