android-fragments 调用了OnCreateView,但片段不可见

vdzxcuhz  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(164)

我使用日志来查看是否调用了onCreateView和onViewCreated。在那一边一切看起来都很好。但是,由于某种原因,它没有显示片段的布局。除了main_actvity布局之外,什么都看不到。P.S.请不要介意缩进,这是我在这里的第一个问题,所以我对问题编辑不是很熟悉。
主要活动:

class MainActivity: AppCompatActivity() {
private lateinit var viewBinding: ActivityMainBinding

private var imageCapture: ImageCapture? = null

private lateinit var cameraExecutor: ExecutorService
private lateinit var db: FirebaseFirestore

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Log.d("TAG", "onCreate: ")

    viewBinding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(viewBinding.root)
    Log.d("TAG", "onCreate: ")

    // Request camera permissions
    if (allPermissionsGranted()) {
        Log.d(TAG, "onCreate: ")

        startCamera()
    } else {
        Log.d(TAG, "onCreate: ")
        ActivityCompat.requestPermissions(
            this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS
        )
    }

    // Set up the listeners for take photo and video capture buttons
    viewBinding.imageCaptureButton.setOnClickListener { takePhoto() }

    cameraExecutor = Executors.newSingleThreadExecutor()

    val historyBtn = viewBinding.historyBtn

    historyBtn.setOnClickListener {
        if (savedInstanceState == null) {
            supportFragmentManager.commit {
                Log.d("bla", "onCreate: sdfsdfsd")

                setReorderingAllowed(true)
                add<HistoryFragment>(R.id.root_layout)
            }
        }
    }
}

历史片段:

class HistoryFragment : Fragment() {

private lateinit var db: FirebaseFirestore

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

}

companion object{
    fun newInstance(): HistoryFragment{
        return HistoryFragment()
    }
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    db = Firebase.firestore

    val docRef = db.collection("Products")
    Log.d("frTag", "onCreateView: sdasda")

    docRef.get()
        .addOnSuccessListener { documents ->
            for (document in documents){
                val objects = documents.toObjects(ProductModel::class.java)

                val productArrayList: ArrayList<ProductModel> = objects as ArrayList<ProductModel>
               
            }
        }

}
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    Log.d("kalla", "onCreateView: called")
    return inflater.inflate(R.layout.fragment_history, container, false)
}

}

主要活动xml:

<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"
tools:context=".MainActivity">

<fragment
    class="com.sanjarbek.mlkitfunproject.HistoryFragment"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.sanjarbek.mlkitfunproject.HistoryFragment" />

<androidx.camera.view.PreviewView
    android:id="@+id/viewFinder"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<Button
    android:id="@+id/image_capture_button"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_marginBottom="50dp"
    android:layout_marginEnd="50dp"
    android:elevation="2dp"
    android:text="take photo"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintEnd_toStartOf="@id/vertical_centerline" />

<Button
    android:id="@+id/video_capture_button"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_marginBottom="50dp"
    android:layout_marginStart="50dp"
    android:elevation="2dp"
    android:text="start capture"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@id/vertical_centerline" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/vertical_centerline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent=".50" />

<Button
    android:id="@+id/history_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:text="History"
    android:layout_margin="10dp"/>

 </androidx.constraintlayout.widget.ConstraintLayout>

历史记录片段xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HistoryFragment">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="adfdasdfafa"/>
qhhrdooz

qhhrdooz1#

嘿,不要担心格式,它比很多人发布的要好得多!我可以看到一些可能导致这种情况的问题(虽然现在不能测试)。
首先,您的主Activity布局中的HistoryFragment位于填充布局的PreviewView之后:

<fragment
    class="com.sanjarbek.mlkitfunproject.HistoryFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<androidx.camera.view.PreviewView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

因为PreviewView是在 later 声明的,所以在这里它被绘制在Fragment * 之上(除非您提供了elevation或更改了它的Z顺序)。我不知道该视图是否呈现透明,除非您对它做了什么,但如果不这样,它会遮住您的Fragment。
另一个问题是添加了一个HistoryFragment,作为布局中已有的HistoryFragment的子级(在XML中声明):

// activity_main.xml
<fragment
    class="com.sanjarbek.mlkitfunproject.HistoryFragment"
    android:id="@+id/root_layout"
    ...
/>

// MainActivity
supportFragmentManager.commit {
    add<HistoryFragment>(R.id.root_layout)
}

您在布局中嵌入了一个HistoryFragment-然后,由于root_layout是该Fragment的id,因此您在其中 * 嵌入了add * 另一个 * HistoryFragment * 示例。运行您的应用,打开 * 布局检查器 *,查看一下层次结构。
通常情况下,您不会像这样将Fragment嵌入XML中,这样做不如使用FragmentTransaction添加和删除Fragment灵活。而且,看起来您无论如何都要在代码中添加Fragment。您应该将root_layout作为某种 * 容器 * 来代替-FrameLayout是一种经典的方法,但现在建议您使用FragmentContainerView。您可以根据自己的喜好调整大小和位置,并将片段添加到其中。
我不知道这两种方法是否能解决你的片段不可见的问题,但无论如何都需要做。记住 * 布局检查器 * 会帮助你看到正在发生的事情,它对解决布局问题非常有用

相关问题