gpt4 book ai didi

android - 从 ExpandableListView (Android) 在 ChildView 中添加标题

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:38 25 4
gpt4 key购买 nike

我正在尝试通过 Internet 为我的答案找到解决方案,但找不到合适的解决方案。我看了这个话题:Android ExpandableListView Child Headers但它对我不起作用。

我现在拥有的(屏幕):

我的代码是:

private void fillData() {
ExpandableListView lv;
lv = (ExpandableListView) getActivity().findViewById(R.id.grades_view);
lv.setItemsCanFocus(false);
lv.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);

View view = View.inflate(getActivity(), R.layout.list_header, null);
lv.addHeaderView(view, null, false);

dbh = new DatabaseHelper(getActivity());
MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(
getActivity(), dbh);
mAdapter.synchronize();
lv.setAdapter(mAdapter);
registerForContextMenu(lv);
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

private List<Subject> mSubjects = new ArrayList<Subject>();
private List<List<Course>> mCourse = new ArrayList<List<Course>>();
private DatabaseHelper dbh;
private LayoutInflater inflater;

public MyExpandableListAdapter(Context context, DatabaseHelper dbh) {
this.dbh = dbh;
this.inflater = LayoutInflater.from(context);
}

public void synchronize() {

mSubjects = dbh.getSubjects();

for (Subject s : mSubjects) {
mCourse.add(dbh.getCourses(s));
}
}

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

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

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

Course c = (Course) getChild(groupPosition, childPosition);

if (convertView == null)
convertView = inflater.inflate(R.layout.list_item_child,
parent, false);

TextView txt = (TextView) convertView.findViewById(R.id.code);
txt.setText(c.getCode());
TextView txt1 = (TextView) convertView.findViewById(R.id.ects);
txt1.setText(String.valueOf(c.getEcts()));
TextView txt2 = (TextView) convertView.findViewById(R.id.grade);

if (c.getGrade() == -1) {
txt2.setText("-");
} else {
txt2.setText(String.valueOf(c.getGrade()));
}

return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return mCourse.get(groupPosition).size();
}

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

@Override
public int getGroupCount() {
return mSubjects.size();
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

Subject s = (Subject) getGroup(groupPosition);

if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, parent,
false);

TextView txt1 = (TextView) convertView.findViewById(R.id.name);
txt1.setText(String.valueOf(s.getName()));
TextView txt2 = (TextView) convertView.findViewById(R.id.semester);
txt2.setText(String.valueOf(s.getSemester()));

return convertView;

}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

}

正如我们所见,为了放置我使用的一级标题(Name 和 Sem):

View view = View.inflate(getActivity(), R.layout.list_header, null);
lv.addHeaderView(view, null, false);

但我不知道如何对 child 做同样的事情(带有代码、ECTS 和成绩的标题)。

提前谢谢你。

最佳答案

1.-在您的组的 xml 中包含标题,例如:

<?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="wrap_content"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
//Your elementsof group
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/headerList"
android:orientation="horizontal" >
//the elements of your header (ID,counter, description, etc..)
</LinearLayout>

2.-现在,在您的方法 getGroupView() 中使用该 ID...

LinearLayout headerList = (LinearLayout) view.findViewById(R.id.headerList);
if(isExpanded)
headerList.setVisibility(View.VISIBLE);
else
headerList.setVisibility(View.GONE);

一年后,我希望它对你有用:)

关于android - 从 ExpandableListView (Android) 在 ChildView 中添加标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17012071/

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