如何使用RecyclerView在片段中“ findViewById”?

时间:2019-04-05 17:07:26

标签: android kotlin android-recyclerview findviewbyid

我在RecyclerView内部有一个ConstraintLayout,它生活在一个片段中。我想更改ConstraintLayout的尺寸(基于屏幕分辨率),但似乎找不到在代码中获取ConstraintLayout的正确方法。

我尝试过:

view !!。findViewById(R.id.postConstraint)

activity !!。findViewById(R.id.postConstraint)

两者均返回null。

我的片段

package no.alacho.pgr202_positivity

import android.graphics.Color
import android.graphics.Point
import android.os.Bundle
import android.util.Log
import android.view.Display
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.fragment_main.*
import kotlinx.android.synthetic.main.single_post_item.*

class Posts : Fragment() {

  private lateinit var layoutManager: GridLayoutManager
  private lateinit var adapter: PostAdapter
  private lateinit var display: Display
  private lateinit var size: Point

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    display = activity!!.windowManager.defaultDisplay
    size = Point()
    display.getSize(size)
  }

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_main, container, false)
  }

  override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    layoutManager = GridLayoutManager(activity, 2)
    postRecycler.layoutManager = GridLayoutManager(activity, 2)
    adapter = PostAdapter()
    postRecycler.adapter = adapter

    //view!!.findViewById<Button>(R.id.hurraButton).text =  "Something"

    view!!.findViewById<ConstraintLayout>(R.id.postConstraint).minWidth = 100
    activity!!.findViewById<ConstraintLayout>(R.id.postConstraint).minWidth = 100
  }
}

还有我的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_height="wrap_content"
        android:minHeight="150dp"
        android:minWidth="150dp"
        android:layout_width="wrap_content"
        android:backgroundTint="#FF0000"
        android:background="#FF0000"
        android:id="@+id/postConstraint">

    <Button android:layout_width="wrap_content"
            android:id="@+id/hurraButton"
            android:layout_height="wrap_content"
            android:text="Hallo"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

2 个答案:

答案 0 :(得分:1)

尝试在onCreateView中将其初始化。

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    var rootView = inflater.inflate(R.layout.fragment_main, container, false)
    rootView.findViewById(R.id.YourRecyclerViewId)
    return rootView
 }

使变量成为全局变量以在oder函数中使用它。我希望这有帮助。

答案 1 :(得分:0)

您应该重写RecyclerView的适配器和ViewHolder,尝试执行以下操作:

class Posts : Fragment() {

    private lateinit var adapter: MyAdapter

    ...

    class MyAdapter() : RecyclerView.Adapter<MyViewHolder>() {


        ...

        override fun onBindViewHolder (viewHolder: MyViewHolder, pos: Int) {
            viewHolder.bind()
        }

    }

    class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {

        private val constraintLayout = itemView.findViewById<ConstraintLayout>(R.id.postConstraint)

        fun bind() {
            constraintLayout.minWidth = 100  //You should use LayoutParams to modify View bounds
        }
    }
}

如果您在实现自定义适配器和ViewHolder时遇到问题,请发表评论,我将予以实现。