簡體   English   中英

XML 文件是否也有其上下文或只有活動在 android 開發中具有

[英]Does XML files also have its context or does only activity have in android development

我正在學習使用 Kotlin 進行 Android 開發,並且正在學習需要制作適配器的自定義 ListView。 但是當它完成時,我在代碼中發現了一些東西,我很困惑它是什么以及為什么在那里使用它。 在使用 LayoutInfalter.from() 時,在 from() 方法中,我必須傳遞上下文,但是要傳遞的上下文作為我傳遞的整個代碼已經傳遞了不同的上下文。

package com.example.myapplication

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import androidx.core.content.ContextCompat

class ItemAdapter(val items : Array<Item>) : BaseAdapter() {
    override fun getCount(): Int {
        return items.size
    }

    override fun getItem(position: Int): Item {
        return items[position]
    }

    override fun getItemId(position: Int): Long {
        return items[position].ItemName.hashCode().toLong()
    }
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val conView = LayoutInflater.from(parent?.context).inflate(R.layout.list_item, parent, false)// in from() why parent?.context is passed.
        val ItemNames : TextView = conView.findViewById(R.id.item_name)
        val ItemPrice : TextView = conView.findViewById(R.id.item_price)
        ItemNames.text = getItem(position).ItemName
        ItemPrice.text = "₹ ${getItem(position).ItemPrice.toString()}"
        val item = getItem(position)
        if(item.isFav){
            conView.setBackgroundColor(ContextCompat.getColor(conView.context, R.color.teal_200)) // in this line why we pass conView.context, why not something else as we usually do by passing 'this'.
        }else{
            conView.setBackgroundColor(ContextCompat.getColor(conView.context, R.color.white)) // in this line why we pass conView.context, why not something else as we usually do by passing 'this'.
        }
        return conView
    }
}

在代碼中我們傳遞了兩個不同的上下文,

  1. 父級?.上下文
  2. conView.context 為什么我們需要兩個上下文以及如何確定要傳遞誰的上下文?

我已經在我的代碼中標記了我無法將上下文理解為注釋的行。

Context 可以包含有關應用程序當前狀態的各種信息。 上下文中對視圖及其相關類很重要的部分是資源。

還要記住,Activity 是它自己的上下文。 它是 Context 的子類。 因此,當您在 Activity 類中編寫代碼時,可以將this作為 Context 參數傳遞。 在其他類中,如果這些類不是 Context 的子類,則不能將this用作上下文。 (幾乎您將使用的唯一作為 Context 子類的類是 Activity、Service 和 Application。)

XML 文件沒有上下文,但是當您在 Activity 中調用setContentView時,Activity 會將自身作為其正在膨脹的每個視圖的上下文參數傳遞,因此布局中的所有視圖都引用與它們自己相同的 Activity 實例context

Activity 的資源將具有當前為 String 資源選擇的語言,以及當前屏幕方向的正確布局等。這就是 Views 使用 Context 的目的。

在您的適配器內部,可以使用parent.context 子視圖應始終使用與其父視圖相同的上下文。 順便說一句,父級的上下文實際上是用於在setContentView中擴展視圖布局的 Activity。

使用conview.context也很好,因為這將是在之前調用同一函數時傳遞的同一個 Context 實例。

您應該更改函數簽名,以便parent不可為空,因為它永遠不能為空。 這使它更容易使用。 你不需要?. 空安全調用,因此您的代碼將更清晰。

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

LayoutInflater 用於將布局 XML 文件實例化為其對應的 View 對象。 您不能直接使用視圖。 通過傳遞內容,它將返回 LayoutInflater 對象,您可以從中訪問子視圖,並使用 dot(.) 運算符訪問視圖的屬性。

parent?.context用於將整個 xml 根視圖作為對象。 同時, conView.context用於提供對特定“conView”屬性成員的訪問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM