“Kotlin”如何在 Fragment 更改时在 NestedScrollView 中保存 ListView 的位置?

问题描述

我有一个 Fragment A (Read_Fragment),里面有一个 nestedScrollView 和一个 ListView。 Mylistadapter 中是一个 OnClickListener,它调用 Fragment B。当从 Fragment B 到 A 时,ListView 在开始。如何保存 ListView/nestedScrollView 的最后位置,以便在关闭 Fragment B 时检索它。

谢谢!

fragment_read.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/clReadFirst"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_black">

        <TextView
            android:id="@+id/tvReadTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="İtiraf oku"
            android:textColor="@color/beige"
            android:fontFamily="@font/akrobat_black"
            android:textSize="50sp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.core.widget.nestedScrollView
            android:id="@+id/nsvRead"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="50dp"
            android:fillViewport="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvReadTitle"
            app:layout_constraintVertical_bias="0">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/clRead"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
        <ListView
            android:id="@+id/lvRead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintVertical_bias="0"
            android:nestedScrollingEnabled="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.nestedScrollView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

ReadFragment.kt:

import android.nfc.Tag
import android.os.Bundle
import android.os.Parcelable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.example.itirafet.databinding.FragmentReadBinding
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase

private var _binding : FragmentReadBinding? = null
private val binding get() = _binding!!

class ReadFragment: Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
    ): View? {
        _binding = DataBindingUtil.inflate(inflater,R.layout.fragment_read,container,false)

        val db = Firebase.firestore
        val titleArray: ArrayList<String> = ArrayList()
        val textArray: ArrayList<String> = ArrayList()
        val timeArray: ArrayList<String> = ArrayList()

        db.collection("itiraflar")
            .orderBy("time",Query.Direction.DESCENDING)
            .get()
            .addOnSuccessListener {
                for (document in it){
                    titleArray.add(document.data["title"] as String)
                    textArray.add(document.data["text"] as String)
                    timeArray.add(document.data["time"] as String)
                }
                val adapter = Mylistadapter(requireActivity(),titleArray,textArray,timeArray)
                binding.lvRead.adapter = adapter
            }
            .addOnFailureListener {
                Log.i(TAG,"Error getting documents: ",it)
            }
        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

Mylistadapter.kt:

import android.app.Activity
import android.content.res.Resources
import android.util.displayMetrics
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.findFragment
import androidx.navigation.findNavController
import com.google.android.gms.dynamic.SupportFragmentWrapper

class Mylistadapter(private val context: Activity,private val title: ArrayList<String>,private val text: ArrayList<String>,private val time: ArrayList<String>) :
    ArrayAdapter<String>(context,R.layout.item_itiraf,title){

    override fun getView(position: Int,convertView: View?,parent: ViewGroup): View {
        val inflater = context.layoutInflater
        val rowView = inflater.inflate(R.layout.item_itiraf,null,true)

        val titleText = rowView.findViewById<TextView>(R.id.tvItiraf)
        val textText = rowView.findViewById<TextView>(R.id.tvReadTextExp)
        val timeText = rowView.findViewById<TextView>(R.id.tvReadDate)

        titleText.text = title[position]
        textText.text = text[position]
        timeText.text = "Eklenme tarihi: " + time[position]

        val tvReadAll = rowView.findViewById<TextView>(R.id.tvReadAll)

        tvReadAll.setonClickListener {
            it.findNavController().navigate(ReadFragmentDirections.actionReadFragmentToItirafFullFragment(title[position],text[position]))
        }

        textText.setonClickListener {
            it.findNavController().navigate(ReadFragmentDirections.actionReadFragmentToItirafFullFragment(title[position],text[position]))
        }

        return rowView
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...