简体   繁体   English

保存和恢复 FirebaseUI RecyclerView 滚动状态

[英]Saving and Restoring FirebaseUI RecyclerView scroll state

I am new to developing, much of coding in general, and am attempting to learn by building an app in Kotlin.我是开发新手,大部分编码都是一般的,并且正在尝试通过在 Kotlin 中构建应用程序来学习。 I have some basic functions running but I am having trouble trying to get the RecyclerView to keep it's scroll position when returning from other activities.我有一些基本功能正在运行,但是在从其他活动返回时,我无法尝试让 RecyclerView 保持其滚动位置。 I have searched and attempted different methods but, probably due to my lack of experience, I cannot get them to work.我已经搜索并尝试了不同的方法,但可能由于我缺乏经验,我无法让它们起作用。 I have one method that looks promising but I don't know how or where to place the supplied code.我有一种看起来很有希望的方法,但我不知道如何或在哪里放置提供的代码。

The method I'm attempting to apply我尝试应用的方法

class MainActivity : AppCompatActivity() {
    private val db = FirebaseFirestore.getInstance()
    private val notebookRef = db.collection("Notebook")
    private lateinit var adapter: NoteAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val buttonAddNote =
            findViewById<FloatingActionButton>(R.id.button_add_note)
        buttonAddNote.setOnClickListener {
            startActivity(
                Intent(
                    this@MainActivity,
                    NewNoteActivity::class.java
                )
            )
        }
        setUpRecyclerView()
    }

    private fun setUpRecyclerView() {
        val query: Query = notebookRef.orderBy("priority", Query.Direction.DESCENDING)
        val options = FirestoreRecyclerOptions.Builder<Note>()
            .setQuery(query, Note::class.java)
            .build()
        adapter = NoteAdapter(options)
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        recyclerView.setHasFixedSize(true)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
    }

    override fun onStart() {
        super.onStart()
        adapter.startListening()
    }

    override fun onStop() {
        super.onStop()
        adapter.stopListening()
    }
}
class Note {
    var title: String? = null
        private set
    var priority = 0
        private set

    constructor() { //empty constructor needed
    }

    constructor(title: String?, priority: Int) {
        this.title = title
        this.priority = priority
    }
}
class NoteAdapter(options: FirestoreRecyclerOptions<Note>) :
    FirestoreRecyclerAdapter<Note, NoteAdapter.NoteHolder>(options) {

    override fun onBindViewHolder(
        holder: NoteHolder,
        position: Int,
        model: Note
    ) {
        holder.textViewTitle.text = model.title
        holder.textViewPriority.text = model.priority.toString()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): NoteHolder {
        val v: View = LayoutInflater.from(parent.context).inflate(
            R.layout.note_item,
            parent, false
        )
        return NoteHolder(v)
    }

    inner class NoteHolder(itemView: View) :
        ViewHolder(itemView) {
        var textViewTitle: TextView = itemView.findViewById(R.id.text_view_title)
        var textViewPriority: TextView = itemView.findViewById(R.id.text_view_priority)
    }
}

Also the RecyclerView appears to reload after returning from an activity, which I'm hoping the above method will also fix.从活动返回后,RecyclerView 似乎也重新加载,我希望上述方法也能解决这个问题。 I've tried "singleTop" but that doesn't seem to work.我试过“singleTop”,但这似乎不起作用。

AndroidManifest.xml: AndroidManifest.xml:

    <activity
            android:name=".MainActivity"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NewNoteActivity"
            android:parentActivityName=".MainActivity" />

Help on any code cleanup is also greatly appreciated.对任何代码清理的帮助也非常感谢。

I do not read your huge code implementation, but I can say some important things about RecyclerView.我没有阅读你的大量代码实现,但我可以说一些关于 RecyclerView 的重要事情。

The RecyclerView is composed by 5 actors: RecyclerView 由 5 个参与者组成:

  • RecyclerView in your layout布局中的 RecyclerView
  • ViewHolder (Each View that you needs to be repeated) ViewHolder(你需要重复的每个View)
  • Collection data采集数据
  • LayoutManager (3 popular implementations: Linear - Grid - Staggered) LayoutManager(3 个流行的实现:线性 - 网格 - 交错)
  • Adapter (Set each single data from collection with each viewHolder)适配器(使用每个 viewHolder 设置集合中的每个数据)

You only must initialize 1 time: RecyclerView LayoutManager Adapter您只需初始化 1 次:RecyclerView LayoutManager Adapter

So, in onCreate of your Activity/Fragment you init Recycler, LayoutManager and Adapter.因此,在您的 Activity/Fragment 的 onCreate 中,您初始化 Recycler、LayoutManager 和 Adapter。

After this point, you only must worry about to pass update Collection to the Adapter.此后,您只需担心将更新集合传递给适配器。 The reconstruction of View, last position visible, recycler operation... Is RecyclerView internal implementation, do not worry about that behavior. View的重构,最后一个位置可见,recycler操作……是RecyclerView内部实现,不用担心那个行为。

How you save in a reconstruction scenario your collection data?您如何在重建场景中保存您的收集数据? In saveInstanceState?在 saveInstanceState 中?

If you not have this implementation, when the Activity/Fragment is reconstructed you obtain a new Collection data, this collection you passed to RecyclerView, and this guy think this is the same data or it is new?如果你没有这个实现,当Activity/Fragment被重构时你得到一个新的Collection数据,这个collection你传递给RecyclerView,这家伙认为这是相同的数据还是新的? If is new data I need to show the first position, if not I need show last position visible.如果是新数据,我需要显示第一个位置,如果不是,我需要显示最后一个位置可见。

So, you are saving your Collection data?那么,您正在保存您的收藏数据吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM