gpt4 book ai didi

android - 适配器不调用 getChildView

转载 作者:太空狗 更新时间:2023-10-29 14:17:23 26 4
gpt4 key购买 nike

我不得不承认我是 Android 的新手,我的 ExpandableListView 给我带来了很多麻烦。我认为是适配器工作不正常。我正在使用一个扩展 BaseExpandableListAdapter 的类。

从日志中我知道了这么多:我交给适配器构造函数的列表已正确填写。 getGroupCount 返回 1(是的,只有 1 个组),getChildrenCount 返回 18(正如预期的那样)。方法 getGroupView 被调用 (Log.d...) 但 getChildView 不是 - 不是在此方法的最开始记录时而不是调试时(未达到断点)。没有错误消息 - 只是 ExpandableListView 静静地没有展开。

知道哪里出了问题吗?我需要一个 ViewHolder 还是它没有正确地膨胀组?我完全迷路了...

我的部分代码:

package de.cimitery.android.cimitery;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ExpandableAdapter extends BaseExpandableListAdapter {

private List<GroupCat> catList;
private Context ctx;
NewGraveActivity app;

public ExpandableAdapter(List<GroupCat> catList, Context ctx, NewGraveActivity app) {
this.catList = catList;
this.ctx = ctx;
this.app = app;
}

@Override
public Object getGroup(int groupPosition) {
return catList.get(groupPosition);
}

@Override
public int getGroupCount() {
Log.d("ExAdapter getGroupCount", "" + catList.size());
return catList.size();
}

@Override
public long getGroupId(int groupPosition) {
return catList.get(groupPosition).hashCode();
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Log.d("ExAdapter getGroupView", "Start");
View v = convertView;

if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.group_layout, parent, false);
}

TextView groupName = (TextView) v.findViewById(R.id.groupName);

GroupCat cat = catList.get(groupPosition);

groupName.setText(cat.getGroupName());

return v;
}


@Override
public Object getChild(int groupPosition, int childPosition) {
return catList.get(groupPosition).getItemList().get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return catList.get(groupPosition).getItemList().get(childPosition).hashCode();
}

@Override
public View getChildView(final int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Log.d("ExAdapter getChildView", "Start");
View v = convertView;

if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.item_layout, parent, false);
}

CheckBox itemCheck = (CheckBox) v.findViewById(R.id.itemCheck);
TextView itemName = (TextView) v.findViewById(R.id.itemName);

if (app.selected.containsKey((groupPosition))==true)
itemCheck.setChecked(true);
else
itemCheck.setChecked(false);


Category child = catList.get(groupPosition).getItemList().get(childPosition);
Log.d("ExAdapter getChildView", child.getName());

itemName.setText(child.getName());

itemCheck.setOnCheckedChangeListener(new CheckListener(childPosition, app));

return v;
}

@Override
public int getChildrenCount(int groupPosition) {
int size = catList.get(groupPosition).getItemList().size();
System.out.println("number of children for group ["+groupPosition+"] is ["+size+"]");
return size;
}

}

组布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/groupName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dip" />

<TextView
android:id="@+id/groupDescr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="8dip" />

</LinearLayout>

项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<CheckBox
android:id="@+id/itemCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/itemName"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

用 exp 布局。 ListView :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

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

<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp" >
</ExpandableListView>


<Button
android:id="@+id/buttonNewGrave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonNewGrave" />


</LinearLayout>

</ScrollView>

最佳答案

第一件事:您不应该在 ScrollView 中使用任何滚动组件,例如可扩展列表,这就是为什么 Listview inside ScrollView is not scrolling on Android .

第二件事:ExpandableListView 的高度应该是 match_parent。在您的情况下,您在线性布局中采用了高度为 wrap_content 的 Expandable ListView,将其更改为 match_parent。

我遇到了同样的问题,我按照同样的方法解决了这个问题。

关于android - 适配器不调用 getChildView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20662647/

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