gpt4 book ai didi

java - 传递给ArrayAdapter和RecyclerView适配器的 View 和ViewGroup是什么?

转载 作者:行者123 更新时间:2023-12-01 20:19:57 26 4
gpt4 key购买 nike

我对最初传递给方法的内容感到非常困惑。

ArrayAdapter getview方法中,传递的ViewViewGroup是什么

 public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
....

}

List_item xml 是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">


<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:fontFamily="serif"

android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />

....
</LinearLayout>

这里传入RecyclerView适配器的ViewGroup是什么。

@Override
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;

View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
NumberViewHolder viewHolder = new NumberViewHolder(view);

return viewHolder;
}

Number_list_item 是

<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="wrap_content"
android:padding="16dp">

<TextView
android:id="@+id/tv_item_number"
style="@style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:fontFamily="monospace"
android:textSize="42sp"
tools:text="#42" />
</FrameLayout>

除了膨胀布局之外,下面的行还做了什么。

View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately

最佳答案

    View view = inflater.inflate(layoutIdForListItem, viewGroup, 
shouldAttachToParentImmediately);

当你想在 UI 上显示任何 View 时,它必须有一定的高度和宽度。在 inflate 方法中,

  • 第一个参数是 View 的资源 ID,它应该膨胀。对于Adapter来说,应该是要被回收的item view。

  • 第二个参数是一个ViewGroup,通常是父 View 。父 View 必须是 ViewGroup,因为 ViewGroup 包含 subview ,反之亦然。在 ListView 的情况下,有时未自动采用的膨胀 View 的大小(宽度)取决于其父级。

For an example - If a ListView have a width match_parent then there is a possibility width of your inflated view less than its parent if you skipped this parameter.

  • 第三个参数用于决定是否要将膨胀 View 添加到父ViewGroup(第二个参数)中。在ListView的情况下,如果你设置为true,那么你将得到RunTimeException(View已经有一个父级),这个参数应该是true如果您想在父 View 中添加新 View 。

For an example - If I want to add View in my UI only after sudden action, then I will inflate a 'View' and add in a ViewGroup.

关于java - 传递给ArrayAdapter和RecyclerView适配器的 View 和ViewGroup是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44998340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com