使用scrollToPosition保留RecyclerView中的滚动位置不起作用

问题描述 投票:1回答:1

我正在尝试使用onPause和onResume方法来保留RecyclerView的滚动位置,当一个intent用于启动另一个活动时(在我的情况下是一个Web浏览器)

当我调试onResume方法时,“lastFirstVisiblePosition”是一个有效的整数,但是屏幕仍然一直滚动到RecyclerView的第一个元素。

感谢任何帮助,谢谢你提前。

编辑:我正在使用viewpager,我有多个回收站视图/片段

继承我的片段:

public class CultureFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<News>>, RecyclerViewAdapter.OnItemClicked {
    private String baseUrlString = "https://content.guardianapis.com/search?show-fields=headline,thumbnail,wordcount,trailText";
    private String LOG_TAG = CultureFragment.class.getName();
    public LoaderManager loaderManager;
    private RecyclerView recyclerView;
    private RecyclerViewAdapter recyclerViewAdapter;
    private TextView mEmptyStateTextView;
    private View LoadingIndicator;
    private RecyclerView.LayoutManager layoutManager;
    private int lastFirstVisiblePosition;

    View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        setRetainInstance(true);

        //initialise rootview
        rootView = inflater.inflate(R.layout.word_recycler_view, container, false);

        //set up empty text view
        mEmptyStateTextView = rootView.findViewById(R.id.empty_view);
        mEmptyStateTextView.setVisibility(View.GONE);

        //Set up loading indicator
        LoadingIndicator = rootView.findViewById(R.id.loading_indicator);


        // initialising the recycler view and setting the adapter for adding items.
        recyclerView = rootView.findViewById(R.id.recycler_view);
        recyclerView.setAdapter(recyclerViewAdapter);


        //set the layout manager for item positioning in the list
        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);


        //Setting up loadermanager if there is connection
        if (NewsActivity.isConnected) {
            loaderManager = getLoaderManager();
            loaderManager.initLoader(1, null, this);
        } else {
            recyclerView.setVisibility(View.GONE);
            LoadingIndicator.setVisibility(View.GONE);
            mEmptyStateTextView.setVisibility(View.VISIBLE);
            mEmptyStateTextView.setText(R.string.no_connection);
        }


        return rootView;
    }


    @Override
    public Loader<List<News>> onCreateLoader(int id, Bundle args) {

        Uri baseUri = Uri.parse(baseUrlString);
        Uri.Builder uriBuilder = baseUri.buildUpon();

        //Create the URL
        uriBuilder.appendQueryParameter("section", "culture");
        uriBuilder.appendQueryParameter("page-size", "20");
        uriBuilder.appendQueryParameter("api-key", "test");
        uriBuilder.appendQueryParameter("format", "json");
        uriBuilder.appendQueryParameter("orderBy", "newest");

        Log.e(LOG_TAG, " the url  " + uriBuilder.toString());

        return new NewsLoader(getContext(), uriBuilder.toString());
    }

    @Override
    public void onLoadFinished(android.support.v4.content.Loader<List<News>> loader, List<News> data) {
        //populate the recyclerview
        //dismiss the loading indicator
        LoadingIndicator.setVisibility(View.GONE);

        if (data != null) {
            recyclerViewAdapter = new RecyclerViewAdapter(getContext(), data);
            recyclerView.setAdapter(recyclerViewAdapter);


            //set the on click listener if the adapter is not NULL (here)
            recyclerViewAdapter.setOnClick(this);

        } else {

            // Remove recycler view, and set the no text textview
            recyclerView.setVisibility(View.GONE);
            mEmptyStateTextView.setVisibility(View.VISIBLE);
            mEmptyStateTextView.setText(R.string.no_stories);

        }

    }

    @Override
    public void onLoaderReset(android.support.v4.content.Loader<List<News>> loader) {

    }

    /*Custom on item click method, implemented in the recycler view Adapter*/
    @Override
    public void onItemClick(int position) {
        News news = recyclerViewAdapter.getItem(position);

        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(news.getmWebUrl()));
        startActivity(i);

    }

    @Override
    public void onPause() {
        super.onPause();
        lastFirstVisiblePosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (lastFirstVisiblePosition > 0) {
            ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPosition(lastFirstVisiblePosition);

        }
    }
}
android android-recyclerview onresume onpause
1个回答
-1
投票

使用LinearLayoutManager和2种方法:

 linearLayoutManagerInstance.onSaveInstanceState()
 linearLayoutManagerInstance.onRestoreInstanceState()
© www.soinside.com 2019 - 2024. All rights reserved.