簡體   English   中英

在加載了LiveDate對象的片段中旋轉屏幕后,如何在ScrollView中保持滾動位置

[英]How to keep scrolling position in ScrollView after rotating the screen in fragment with LiveDate object loaded

我有一個片段,用於顯示根據給定ID從房間數據庫加載的歌曲的歌詞。

旋轉屏幕后,我想保留滾動位置。 現在,旋轉之后,歌曲再次從db加載,並且視圖位於最頂部,無論旋轉之前的滾動位置如何。

我以為我可以在onSaveInstanceState中將滾動位置保存在onCreateView()某些包中,使用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>

我認為問題在於,旋轉屏幕歌曲之后,必須由觀察者的methon onChange()重新加載,因此有一小段時間,scrollView無需顯示任何內容。 我想找到一些解決方案,在再次加載歌曲后如何返回到舊位置。

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

我的更多解釋將來自每個代碼塊內的注釋,因此您可以更好地了解其全部工作原理。

保留的值: 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滾動到期望的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM