我对Android编程很新,并试图实现一个基本的可扩展列表视图,显示一个课程列表,在点击时应该显示一些额外的细节。我觉得问题是非常基本的,因为我没有在任何视图中包含任何奇特的内容,但我仍然无法在线找到解决方案,大多数其他问题都更加具体。
第一部分工作正常(显示课程列表),问题是列表不可点击,这意味着它不会扩展。
我现在所确定的是以下内容:
1.永远不会调用getChildView(调试器永远不会在断点处停止)
2.有孩子们的数据,因为它与用于组头的工作相同的数据相同。
我还尝试设置一个从未调用的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;
}
}
活动java:
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。
答案 0 :(得分:43)
我偶然发现了它。 groupheader中的复选框是可聚焦的,从列表中窃取所有点击。要解决此问题,只需使用以下任一项将列表中复选框/按钮的focusable属性设置为false:
Java :中的
checkbox.setFocusable(false);
或,在 xml :
中android:focusable="false"
答案 1 :(得分:1)
我遇到了同样的问题,但我将以下行添加到我的父视图
android:descendantFocusability="blocksDescendants"
它对我有用。