Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

android - "Kotlin" How to save Position of ListView in NestedScrollView when Fragment changes?

I have a Fragment A (Read_Fragment) with a NestedScrollView and a ListView inside it. In MyListAdapter is an OnClickListener, which calls Fragment B. When coming from Fragment B to A, ListView is at beginning. How can I save the last position of ListView/NestedScrollView, so I can retrieve it when closing Fragment B.

Thanks!

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
    }
}
question from:https://stackoverflow.com/questions/65890584/kotlin-how-to-save-position-of-listview-in-nestedscrollview-when-fragment-chan

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...