Android RecyclerView回弹到项目

huangapple go评论56阅读模式
英文:

Android Recyclerview snaps back to item

问题

以下是您请求的翻译部分:

"如果我滚动查看我的RecyclerView,我不能同时查看项目的末尾和开头...每次我松开手后,它总是会回到项目的中心...我想要自由滚动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="match_parent"
    android:background="#ffffff"
    tools:context=".activities.ViewNewsSubDiv">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/title_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ffffff"
                    android:fontFamily="@font/cairo"
                    android:text="Schüler"
                    android:textAlignment="viewEnd"
                    android:textColor="#000000"
                    android:textSize="24sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ffffff"
                    android:fontFamily="@font/cairo"
                    android:text="Blick"
                    android:textAlignment="gravity"
                    android:textColor="#000000"
                    android:textSize="24sp" />
            </LinearLayout>

            <ImageButton
                android:id="@+id/title_bar_back"
                android:layout_width="85dp"
                android:layout_height="52dp"
                android:background="#ffffffff"
                android:scaleType="fitCenter"
                android:src="@drawable/back"
                app:tint="#000000" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/subview_progress_bar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00000000"
                android:clickable="true"
                android:focusable="true"
                android:gravity="center"
                android:orientation="vertical">

                <ProgressBar
                    style="?android:attr/progressBarStyle"
                    android:layout_width="225dp"
                    android:layout_height="225dp"
                    android:layout_gravity="center"
                    android:indeterminateTint="#7C7C7C" />

                <TextView
                    android:id="@+id/subview_progress_bar_textview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0/100%"
                    android:textColor="#7C7C7C"
                    android:textSize="15pt" />

            </LinearLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/pdfContent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="none"
                android:nestedScrollingEnabled="true" />

        </FrameLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

-- Kotlin 代码

package com.example.schuelerblick.activities

import ...

class ViewNewsSubDiv : AppCompatActivity() {

    private val viewModel by viewModels<PDFReaderViewModel>()

    private val pdfContent: RecyclerView by lazy {
        findViewById(R.id.pdfContent)
    }

    private val progressBar: LinearLayout by lazy {
        findViewById(R.id.subview_progress_bar)
    }

    private val progressBarTextView: TextView by lazy {
        findViewById(R.id.subview_progress_bar_textview)
    }

    private lateinit var adapter: PDFAdapter

    private val totalPDFBitmapList = mutableListOf<Bitmap>()

    private var currentIndex = 0

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

        val backButton = findViewById<ImageButton>(R.id.title_bar_back)

        val pdfContent = findViewById<RecyclerView>(R.id.pdfContent)

        fun setValuesToViews() {
            viewModel.pdf(this, intent.getStringExtra("pdf").toString())
            initFlow()
            initList()
        }

        setValuesToViews()

        backButton.setOnClickListener {
            val intent = Intent(this, StartActivity::class.java)
            startActivity(intent)
        }
    }

    private fun initFlow() {
        // ...
    }

    private fun initList() {
        // ...
    }

    private fun reload() {
        // ...
    }

    private fun loadMore() {
        // ...
    }

    private fun fetchData(offset: Int, limit: Int): List<Bitmap> {
        // ...
    }

    private fun getScreenWidth(context: Context): Int {
        // ...
    }

    private fun showProgress() {
        // ...
    }

    private fun hideProgress() {
        // ...
    }
}

请注意,代码中的 HTML 实体已经被还原为正常的字符。如果您有任何其他翻译请求或需要进一步的帮助,请随时告诉我。

英文:

If I scroll through my recyclerview I can't look at the end of an item and the start at the same time.. it always snaps back to an item to be in the center if I let go... I want to scroll freely through the recyclerview without being send back to some item... how can I stop that?

-- XML layout

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:background=&quot;#ffffff&quot;
    tools:context=&quot;.activities.ViewNewsSubDiv&quot;&gt;

    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:orientation=&quot;vertical&quot;&gt;

        &lt;FrameLayout
            android:id=&quot;@+id/title_bar&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;&gt;

            &lt;LinearLayout
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_gravity=&quot;center&quot;
                android:orientation=&quot;horizontal&quot;&gt;

                &lt;TextView
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:layout_weight=&quot;1&quot;
                    android:background=&quot;#ffffff&quot;
                    android:fontFamily=&quot;@font/cairo&quot;
                    android:text=&quot;Sch&#252;ler&quot;
                    android:textAlignment=&quot;viewEnd&quot;
                    android:textColor=&quot;#000000&quot;
                    android:textSize=&quot;24sp&quot;
                    android:textStyle=&quot;bold&quot; /&gt;

                &lt;TextView
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:layout_weight=&quot;1&quot;
                    android:background=&quot;#ffffff&quot;
                    android:fontFamily=&quot;@font/cairo&quot;
                    android:text=&quot;Blick&quot;
                    android:textAlignment=&quot;gravity&quot;
                    android:textColor=&quot;#000000&quot;
                    android:textSize=&quot;24sp&quot; /&gt;
            &lt;/LinearLayout&gt;

            &lt;ImageButton
                android:id=&quot;@+id/title_bar_back&quot;
                android:layout_width=&quot;85dp&quot;
                android:layout_height=&quot;52dp&quot;
                android:background=&quot;#ffffffff&quot;
                android:scaleType=&quot;fitCenter&quot;
                android:src=&quot;@drawable/back&quot;
                app:tint=&quot;#000000&quot; /&gt;

        &lt;/FrameLayout&gt;

        &lt;FrameLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;&gt;

            &lt;LinearLayout
                android:id=&quot;@+id/subview_progress_bar&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:background=&quot;#00000000&quot;
                android:clickable=&quot;true&quot;
                android:focusable=&quot;true&quot;
                android:gravity=&quot;center&quot;
                android:orientation=&quot;vertical&quot;&gt;

                &lt;ProgressBar
                    style=&quot;?android:attr/progressBarStyle&quot;
                    android:layout_width=&quot;225dp&quot;
                    android:layout_height=&quot;225dp&quot;
                    android:layout_gravity=&quot;center&quot;
                    android:indeterminateTint=&quot;#7C7C7C&quot; /&gt;

                &lt;TextView
                    android:id=&quot;@+id/subview_progress_bar_textview&quot;
                    android:layout_width=&quot;wrap_content&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:text=&quot;0/100%&quot;
                    android:textColor=&quot;#7C7C7C&quot;
                    android:textSize=&quot;15pt&quot; /&gt;

            &lt;/LinearLayout&gt;

            &lt;androidx.recyclerview.widget.RecyclerView
                android:id=&quot;@+id/pdfContent&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:scrollbars=&quot;none&quot;
                android:nestedScrollingEnabled=&quot;true&quot;/&gt;

        &lt;/FrameLayout&gt;

    &lt;/LinearLayout&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

-- kotlin code

package com.example.schuelerblick.activities

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.widget.ImageButton
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.schuelerblick.R
import com.example.schuelerblick.StartActivity
import android.view.View
import android.view.WindowManager
import android.widget.AbsListView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenCreated
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.example.schuelerblick.adapter.PDFAdapter
import com.example.schuelerblick.viewmodel.PDFReaderViewModel
import com.example.schuelerblick.viewmodel.PDFReaderViewModelState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Suppress(&quot;DEPRECATION&quot;)
class ViewNewsSubDiv : AppCompatActivity() {

    private val viewModel by viewModels&lt;PDFReaderViewModel&gt;()

    private val pdfContent: RecyclerView by lazy {
        findViewById(R.id.pdfContent)
    }



    private val progressBar: LinearLayout by lazy {
        findViewById(R.id.subview_progress_bar)
    }

    private val progressBarTextView: TextView by lazy {
        findViewById(R.id.subview_progress_bar_textview)
    }

    private lateinit var adapter: PDFAdapter

    private val totalPDFBitmapList = mutableListOf&lt;Bitmap&gt;()

    private var currentIndex = 0

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

        val backButton = findViewById&lt;ImageButton&gt;(R.id.title_bar_back)

        val pdfContent = findViewById&lt;RecyclerView&gt;(R.id.pdfContent)

        fun setValuesToViews() {

            viewModel.pdf(this, intent.getStringExtra(&quot;pdf&quot;).toString())

            initFlow()
            initList()

        }

        setValuesToViews()

        backButton.setOnClickListener {
            val intent = Intent(this, StartActivity::class.java)
            startActivity(intent)

        }
    }

    private fun initFlow() {
        lifecycleScope.launch(Dispatchers.Main) {
            whenCreated {
                viewModel.pdfReaderViewModelState.collect {
                    when (it) {
                        is PDFReaderViewModelState.OnPDFFile -&gt; {
                            Log.d(&quot;PDFReader.OnPDFFile&quot;, &quot;pdf file ${it.file.length()}&quot;)
                            hideProgress()

                            viewModel.bitmaps(it.file, getScreenWidth(this@ViewNewsSubDiv))
                        }

                        is PDFReaderViewModelState.OnBitmaps -&gt; {

                            hideProgress()

                            totalPDFBitmapList.clear()
                            totalPDFBitmapList.addAll(it.list)

                            reload()

                        }

                        is PDFReaderViewModelState.Error -&gt; {
                            Toast.makeText(this@ViewNewsSubDiv, &quot;error - ${it.message}&quot;, Toast.LENGTH_SHORT).show()
                            hideProgress()
                        }

                        is PDFReaderViewModelState.Empty -&gt; {
                            Toast.makeText(this@ViewNewsSubDiv, &quot;error - empty&quot;, Toast.LENGTH_SHORT).show()
                            hideProgress()
                        }
                        is PDFReaderViewModelState.Loading -&gt; {
                            showProgress()
                        }
                        is PDFReaderViewModelState.Progress -&gt; {
                            progressBarTextView.post{
                                progressBarTextView.text = if ( it.progress != 0) {
                                    &quot;${it.progress}/100%&quot;
                                }
                                else {
                                    &quot;&quot;
                                }
                            }
                        }
                        is PDFReaderViewModelState.None -&gt; Unit

                    }
                }
            }
        }
    }

    private fun initList() {

        pdfContent.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)

        adapter = PDFAdapter {
            loadMore()
        }

        pdfContent.adapter = adapter

        pdfContent.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)

                if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {

                    currentIndex = (recyclerView.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()

                    // current page
                }
            }
        })

        PagerSnapHelper().attachToRecyclerView(pdfContent)

    }

    private fun reload() {
        pdfContent.post{
            adapter.reload(fetchData(0, 1))

        }
    }

    private fun loadMore() {
        pdfContent.post{
            adapter.loadMore(fetchData(adapter.itemCount, 3))
        }
    }

    private fun fetchData(offset: Int, limit: Int): List&lt;Bitmap&gt; {
        val list = mutableListOf&lt;Bitmap&gt;()

        for (i in offset until offset + limit) {
            if ( i &gt; totalPDFBitmapList.size - 1){
                break
            }
            list.add(totalPDFBitmapList[i])
        }

        return list
    }

    private fun getScreenWidth(context: Context): Int {
        val outMetrics = DisplayMetrics()

        if (Build.VERSION.SDK_INT &gt;= Build. VERSION_CODES.R) {
            val display = context.display
            display?.getRealMetrics(outMetrics)
        }
        else {
            val display = (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
            display.getMetrics(outMetrics)
        }

        return outMetrics.widthPixels
    }

    private fun showProgress() {
        progressBar.visibility = View.VISIBLE
    }

    private fun hideProgress() {
        progressBar.visibility = View.GONE
    }
}

答案1

得分: 0

works now!

item snapping was enabled...

英文:

works now!

item snapping was enabled...

huangapple
  • 本文由 发表于 2023年5月22日 22:12:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定