【发布时间】:2020-07-23 04:32:46
【问题描述】:
我正在开发可扩展的 ListView 并使用了本教程。
https://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
但我正在寻找的是我想要一个带有水平滚动视图的子视图。我的子视图内部有一个图像和一个文本视图。它垂直工作,但我希望它水平工作。我看过其他一些教程,但没有一个有效。谁能告诉我如何让它水平工作?
child_view:
<?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:background="@color/textFieldBackgroundColor"
android:orientation="vertical">
<ImageView
android:id="@+id/imageViewChild"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/tvChildName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
android:fontFamily="@font/uber_move_text_regular"
android:gravity="center"
android:textAlignment="center"
android:textColor="@color/alternateTextColor"
android:textSize="19sp" />
</LinearLayout>
适配器视图:
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.categories_list_child, null);
}
TextView txtListChild = convertView
.findViewById(R.id.tvChildName);
ImageView imageViewChild = convertView.findViewById(R.id.imageViewChild);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.categories_list_header, null);
}
TextView lblListHeader = convertView
.findViewById(R.id.tvHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
ImageView headerImage = convertView.findViewById(R.id.imageViewCatHeader);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
【问题讨论】:
-
谢谢,但我找到了一个解决方案,我将在答案中分享。
标签: android expandablelistview