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



在我最新的安卓应用程序中,我正在使用加载器从互联网加载数据。我有一个回收器视图,当我单击某个项目时,就会启动新的活动。但是每次我向后导航时,加载程序都会重新加载数据,因此会更新回收器视图,从而导致跳转到列表顶部。

我怎样才能阻止这种行为?

主活动

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

列表片段

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

编辑:添加了回收器视图代码

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

有同样的问题...似乎加载程序在onResume中调用onLoadFinished.要修复只需在onLoadFinished中调用destroyLoader

更多细节在这里:为什么在片段恢复后再次调用 onLoadDone?

覆盖交付结果并缓存了以前加载器的数据,并存储现在开始加载检查存储的变量是否包含数据,如果不强制加载,否则使用缓存的数据,这样它将防止数据重新加载我是安卓的新手,顺便说一句

相关内容

  • 没有找到相关文章