我一直在阅读很多与我有同样问题的线程,但我无法弄清楚发生了什么。我有一个可扩展的列表视图,但我无法使onChildClickListener工作。 onGroupClickListener工作正常,但我真的不需要它。我看了很多例子,他们似乎都像我一样。这是代码:
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ListView;
import android.widget.Toast;
import org.ektorp.CouchDbConnector;
import org.ektorp.DbAccessException;
import org.ektorp.android.util.EktorpAsyncTask;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.widget.TextView;
public class MainActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EktorpAsyncTask getUsersTask = new EktorpAsyncTask() {
@Override
protected void doInBackground() {
//here i connect to de BD and fill the Group and Children arrays
}
@Override
protected void onSuccess() {
UserArrayExpandableAdapter adapter = new UserArrayExpandableAdapter(getApplicationContext(),parentsChilds.getParents(), parentsChilds.getChilds());
ExpandableListView expandableList = (ExpandableListView) findViewById(R.id.userview);
expandableList.setAdapter(adapter);
expandableList.setClickable(true);
expandableList.setOnGroupClickListener(new OnGroupClickListener(){
@Override
public boolean onGroupClick(ExpandableListView arg0, View arg1,
int groupPosition, long arg3) {
return false;
}
});
expandableList.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
{
Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show();
return false;
}
});
儿童观点:
<?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="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:clickable="true"
android:focusable="true">
<TextView
android:id="@+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/DarkSeaGreen"
android:paddingLeft="8dp"
android:paddingBottom="4dp"
android:textColor="@color/White"
android:textSize="18sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip"
android:clickable="true"
android:focusable="true">
<TextView
android:id="@+id/subject"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>
小组布局:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="5dp" >
<TextView
android:id="@+id/monthText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="26sp"
android:background="@color/SkyBlue"
android:textColor="@color/White"
android:textStyle="bold" />
</LinearLayout>
MainActivity布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoginActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/userview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</RelativeLayout>
适配器代码:
public class UserArrayExpandableAdapter extends BaseExpandableListAdapter {
private final Context context;
private final ArrayList<ArrayList<EventVO>> childs;
private ArrayList<String> parents;
public UserArrayExpandableAdapter(Context context, ArrayList<String> parents,ArrayList<ArrayList<EventVO>> childs) {
this.context = context;
this.childs = childs;
this.parents = parents;
}
@Override
public Object getChild(int arg0, int arg1) {
return childs.get(arg0).get(arg1);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
List<EventVO> child = (ArrayList<EventVO>) childs.get(groupPosition);
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.user_view, parent, false);
}
TextView nameView = (TextView) convertView.findViewById(R.id.name);
TextView dateView = (TextView) convertView.findViewById(R.id.day);
TextView subjectView = (TextView) convertView.findViewById(R.id.subject);
nameView.setText(child.get(childPosition).getName());
String day = child.get(childPosition).getDate();
day = DateTransformer.getDay(day);
dateView.setText(day);
subjectView.setText(child.get(childPosition).getSubject());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return childs.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return parents.get(groupPosition);
}
@Override
public int getGroupCount() {
return parents.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView==null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.parent_view, parent, false);
}
TextView monthView = (TextView) convertView.findViewById(R.id.monthText);
monthView.setText(parents.get(groupPosition));
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
非常感谢。
答案 0 :(得分:3)
我遇到了同样的问题。我的ExpandableListView的孩子有两个ImageView和一个TextView。设置属性TextView:focusable to&#34; false&#34;在布局文件中解决了这个问题。 奇怪的是,相同的代码(没有设置focusable属性)在API8中运行良好,但是当在API16模拟器上执行时,孩子们没有响应click。
答案 1 :(得分:0)
我有同样的问题。就我而言,在我设置的 ExpandableListViewAdapter 类的 getGroupView 方法中
convertView.setClickable(false);
然后,转到 ExpandableListViewAdapter 中的 getChildView 并设置
convertView.setClickable(false);
对我有用。