【问题标题】:How to keep scrolling position in ScrollView after rotating the screen in fragment with LiveDate object loaded在加载 LiveDate 对象的片段中旋转屏幕后如何在 ScrollView 中保持滚动位置
【发布时间】:2019-08-01 19:36:21
【问题描述】:

我有一个片段,用于显示根据给定 ID 从房间数据库加载的歌曲的歌词。

我想在旋转屏幕后保留滚动位置。现在旋转后,歌曲再次从 db 加载,无论旋转前的滚动位置如何,视图都位于最顶部。

我以为我可以在onSaveInstanceState 中保存滚动位置@ 987654322@ 中的一些捆绑@ 使用mSongDisplayScrollView.scrollTo(x, y) 上的命令

片段代码:


public class SongDisplayFragment extends Fragment {

    private Song mSongToDisplay;

    private ScrollView mSongDisplayScrollView;
    private TextView mSongTitleTextView;
    private RecyclerView mSongLyricsRecyclerView;

    private GuitarSongbookViewModel mGuitarSongbookViewModel;

    public static final String SONG_ID_KEY = "SONG_ID_KEY";


    public SongDisplayFragment() {

    }

    public static SongDisplayFragment newInstance(Long songId) {
        SongDisplayFragment songDisplayFragment = new SongDisplayFragment();
        Bundle arguments = new Bundle();
        arguments.putLong(SONG_ID_KEY, 
        songDisplayFragment.setArguments(arguments);
        return songDisplayFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(false);
    }

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

        mSongDisplayScrollView = view.findViewById(R.id.song_display_scroll_view);
        mSongLyricsRecyclerView = view.findViewById(R.id.lyrics_rv_);
        mSongTitleTextView = view.findViewById(R.id.

        mGuitarSongbookViewModel = ViewModelProviders.of(this).get(GuitarSongbookViewModel.class);

        final SongDisplayAdapter songDisplayAdapter = new SongDisplayAdapter(getContext());

        Long songId = null;
        if (getArguments().containsKey(SONG_ID_KEY)) {
            songId = getArguments().getLong(SONG_ID_KEY);
        }

        if (songId != null) {
            final Long finalSongId = songId;


            mGuitarSongbookViewModel.getSongById(songId).observe(this, new Observer<Song>() {
                @Override
                public void onChanged(@Nullable final Song song) {
                    mSongToDisplay = song;
                    mSongTitleTextView.setText(mSongToDisplay.getMTitle());
                    songDisplayAdapter.setSong(song);

                }
            });


        }


        mSongLyricsRecyclerView.setAdapter(songDisplayAdapter);
        mSongLyricsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        return view;
    }

}

片段 XML:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.SongDisplayFragment">

    <ScrollView
        android:id="@+id/song_display_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/autoscroll_bar"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        tools:layout_editor_absoluteX="138dp">

        <LinearLayout

            android:id="@+id/son_display_linear_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/displayed_song_title_txt_"
                style="@style/TitleOfDisplayedSong"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:text="@string/title_placeholder" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/lyrics_rv_"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="12dp"
                android:nestedScrollingEnabled="false" />
        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

片段在 textView 和 RecyclerView 中显示歌曲的倾斜和歌词,适配器类代码是:

package com.example.guitarsongbook.adapters;

import android.content.Context;
import android.graphics.Rect;
import android.text.Html;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guitarsongbook.R;
import com.example.guitarsongbook.daos.SongChordJoinDao;
import com.example.guitarsongbook.model.Chord;
import com.example.guitarsongbook.model.Song;

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


public class SongDisplayAdapter extends RecyclerView.Adapter<SongDisplayAdapter.SongViewHolder> {

    private Context context;
    private final LayoutInflater mInflater;

    private Song mSong;
    private ArrayList<String> mLyrics;

    public SongDisplayAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public SongViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.song_lyrics_rv_item, parent, false);
        return new SongDisplayAdapter.SongViewHolder(itemView);
    }

    public void setSong(Song song){
        mSong = song;
        mLyrics = mSong.getMLyrics();
        notifyDataSetChanged();
    }



    @Override
    public void onBindViewHolder(@NonNull SongViewHolder holder, int position) {
        holder.bindTo(position);
    }

    @Override
    public int getItemCount() {
        if (mLyrics != null)
            return mLyrics.size();
        else return 0;
    }

    public class SongViewHolder extends RecyclerView.ViewHolder {

        private final TextView mLyricsLineTextView;


        public SongViewHolder(@NonNull View itemView) {
            super(itemView);
            mLyricsLineTextView = itemView.findViewById(R.id.song_lyric_line_txt_);
        }

        public void bindTo(int position) {
            if (mSong != null) {
                mLyricsLineTextView.setText(Html.fromHtml(mLyrics.get(position)));



            } else {
                mLyricsLineTextView.setText(context.getString(R.string.no_song_label));
            }
        }
    }


}

RecyclerView 项目 XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/song_lyric_line_txt_"
        style="@style/LyricOfDisplayedSong"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="24dp"
        app:layout_constraintEnd_toStartOf="@+id/song_chord_line_txt_"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="placeholder text" />


</androidx.constraintlayout.widget.ConstraintLayout>

我认为问题在于,在旋转屏幕后,必须由观察者的方法onChange() 再次加载屏幕歌曲,因此有一小段时间滚动视图没有任何内容可显示。我想找到一些解决方案,如何在再次加载歌曲后返回到旧位置。

【问题讨论】:

    标签: java android android-room android-scrollview android-livedata


    【解决方案1】:

    第一步是通过 Fragment 的 onSaveInstanceState 方法持久化对象的值。

    我的更多解释来自每个代码块中的 cmets,因此您可以更好地了解它是如何工作的。

    要持久化的值:mSongToDisplaymScrollViewPosition

    //new variable introduced
    int mScrollViewPosition = 0;
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //If screen orientation changes, android redraws all views 
        //and non persistent data would be lost. 
        //Nevertheless, before this happens android informs the app via this method
        //So to prevent state loss we save the data to a temporary storage
         outState.putParcelable("song_data", mSongToDisplay);
        //save ScrollView current position
         outState.putInt("position_data", mSongDisplayScrollView.getScrollY());
        //call super to commit your changes 
        super.onSaveInstanceState(outState);
    }
    

    接下来是方向改变完成后恢复数据

    在您的fragments onCreateView 中添加以下块,并确保在初始化您的适配器之后出现if statement block

     ...
     //...add adapter initialization block here before checking for saved data 
    
     //Check for saved data
     if (savedInstanceState != null) {
      // Retrieve the data you saved
      mSongToDisplay = savedInstanceState.getParcelable("song_data");
      mScrollViewPosition = savedInstanceState.getInt("position_data");
    
      //Re-initialize and reload adapter record
      mSongToDisplay = song;
      mSongTitleTextView.setText(mSongToDisplay.getMTitle());
      songDisplayAdapter.setSong(song); 
     } 
     else {
     //No data to retrieve
     //initialize data model here. 
     }
    
       //Assign the adapter to recyclerView
       mSongLyricsRecyclerView.setAdapter(songDisplayAdapter);
       mSongLyricsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
      try{
       //Finally we set the scrollview position at this point just to make sure
       //recyclerview has its data loaded and full length of scrollview is restored
       mSongDisplayScrollView.setScrollY(mScrollViewPosition);}
      catch(Exception ex){
         //position isn't right
       }
      return view;
    

    此时所需的数据已被持久化,并且在每个 screenOrientationChange 上,应用程序会将先前保存的歌曲对象加载到适配器中,以防止对 db or api 的错误调用,并且还会使用保存的整数值mScrollViewPosition

    【讨论】:

    • 感谢您的回答。它正在工作,但只有一点点。我的意思是,现在在旋转屏幕后,歌曲很快被重新加载,但视图只滚动到嵌套 recyclerView 的最开始。因此,无论我在旋转后滚动歌曲歌词多远,它都会在开头显示。它必须以某种方式与 scrollView 内有 recyclerView 相关,但我不想使用带有 scrollView 的 recyclerView 滚动。
    • 我在onCreateView() if (savedInstanceState!=null){ final int scrollY = savedInstanceState.getInt("position_data"); mSongDisplayScrollView.postDelayed(new Runnable() { @Override public void run() { mSongDisplayScrollView.scrollTo(0, scrollY); } },700); } 中使用这个sn-p 让它工作了,但缺点是它不平滑,因为有时它必须跳到要求的位置。
    • 查看 XY 坐标并存储,旋转后检索将不起作用,因为无法保证自动换行在纵向和横向模式下会以相同的方式发生。根据您的屏幕比例等,某些项目的 Y 坐标在纵向和横向可能是两倍或三倍。
    猜你喜欢
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多