gpt4 book ai didi

java - Android ExpandableListView 不展开/不可点击

转载 作者:搜寻专家 更新时间:2023-10-30 20:55:16 26 4
gpt4 key购买 nike

我是 android 编程的新手,并尝试实现一个基本的可扩展 ListView ,显示类(class)列表,单击时应显示一些其他详细信息。我觉得这个问题非常基本,因为我在任何 View 中都没有包含任何花哨的东西,但我仍然无法在网上找到解决方案,大多数其他问题都更加具体。

第一部分工作正常(显示类(class)列表),问题是列表不可点击,这意味着它不会展开。

我现在确定的是:
1. getChildView 永远不会被调用(调试器永远不会在断点处停止)
2. 有 child 的数据,因为它与用于 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>

子行布局:

<?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>

Activity 布局:

<?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;
}

}

Activity 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 中的复选框是可聚焦的,从列表中窃取了所有点击。要解决此问题,只需将列表中复选框/按钮的 focusable 属性设置为 false。

最佳答案

偶然发现的。 groupheader 中的复选框是可聚焦的,从列表中窃取了所有点击。要解决此问题,只需将列表中复选框/按钮的 focusable 属性设置为 false 即可:

Java 中:

checkbox.setFocusable(false);

或者,在 xml 中:

android:focusable="false"

关于java - Android ExpandableListView 不展开/不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19732816/

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