我对android编程很陌生,并尝试实现一个基本的可扩展列表视图,显示一个课程列表,单击时应该显示一些额外的细节。我觉得这个问题非常基本,因为我在任何视图中都没有包含任何花哨的东西,但是我仍然无法在网上找到解决方案,大多数其他问题都更加具体。
第一部分工作正常(显示课程列表),问题是列表不可点击,这意味着它不会展开。
我现在确定如下:
1. getChildView 永远不会被调用(调试器永远不会在断点处停止)
2. 有孩子的数据,因为它与用于 groupheading 的数据相同,工作正常。
我还尝试设置一个从未调用过的 onGroupClickListener,因此我再次将其删除。
我的代码:
组标题布局:
<TextView
android:id="@+id/CourseName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="CourseName"
android:layout_weight="7"
android:layout_marginRight="25dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/CourseDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_weight="3"
android:text="CourseDate" />
<CheckBox
android:id="@+id/checkBox"
style="?android:attr/starStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
child_row 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button1"
android:text="Button" />
<TextView
android:id="@+id/phases"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:layout_below="@+id/button1"
android:text="phases:" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/phases"
android:gravity="center"
android:text="information:" />
</RelativeLayout>
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/sportName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="Sport Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="@dimen/profile_title" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:numStars="5"
android:stepSize="1"
android:layout_centerInParent="true" />
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/sportName" >
</ExpandableListView>
</RelativeLayout>
适配器java:
package ch.unibe.unisportbern.views.details;
import java.util.ArrayList;
import com.example.unisportbern.R;
import ch.unibe.unisportbern.support.Course;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class SportsAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Course> courseList;
public SportsAdapter(Context context, ArrayList<Course> courseList) {
this.context = context;
this.courseList = courseList;
}
@Override
public Object getChild(int index, int stub) {
return courseList.get(index);
}
@Override
public long getChildId(int index, int stub) {
return stub;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_row, null);
}
TextView phases = (TextView) convertView.findViewById(R.id.phases);
TextView info = (TextView) convertView.findViewById(R.id.info);
phases.setText("phases:\n" + courseList.get(groupPosition).getPhases());
info.setText(courseList.get(groupPosition).getInformation());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return courseList.get(groupPosition);
}
@Override
public int getGroupCount() {
return courseList.size();
}
@Override
public long getGroupId(int groupPosition) {
return courseList.get(groupPosition).getId();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_heading, null);
}
TextView courseName = (TextView) convertView.findViewById(R.id.CourseName);
TextView courseDate = (TextView) convertView.findViewById(R.id.CourseDate);
courseName.setText(courseList.get(groupPosition).getName());
courseDate.setText(courseList.get(groupPosition).getDay() + courseList.get(groupPosition).getTime());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
活动爪哇:
package ch.unibe.unisportbern.views.details;
import java.util.ArrayList;
import com.example.unisportbern.R;
import ch.unibe.unisportbern.support.Course;
import ch.unibe.unisportbern.support.DBMethodes;
import ch.unibe.unisportbern.support.Sport;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ExpandableListView;
public class DActivity extends Activity{
public final static String NAME = "SportName";
public final static String ID = "SportID";
private Sport sport;
private ArrayList<Course> courses;
private SportsAdapter sportsadapter;
private ExpandableListView myList;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSport();
getCourses();
setContentView(R.layout.details_layout);
myList = (ExpandableListView) findViewById(R.id.expandableListView);
sportsadapter = new SportsAdapter(this, courses);
myList.setAdapter(sportsadapter);
}
private void getCourses() {
DBMethodes dbMethodes = new DBMethodes(this);
try {
courses = dbMethodes.getAllCourses(sport);
} catch (Exception e) {
}
}
private void getSport() {
Intent intent = this.getIntent();
int id = intent.getIntExtra(ID, 0);
String name = intent.getStringExtra(NAME);
this.sport = new Sport(id, name);
}
}
提前非常感谢!
编辑:我偶然发现了它。groupheader 内的复选框是可聚焦的,从列表中窃取所有点击。要解决此问题,只需将列表中复选框/按钮的可聚焦属性设置为 false。