gpt4 book ai didi

Android ExpandableListView 和 onChildClick 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:21 24 4
gpt4 key购买 nike

我已经阅读了很多与我遇到的问题相同的线程,但我无法弄清楚发生了什么。我有一个可展开的 ListView ,但我无法使 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;
}
});

subview :

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


}

非常感谢。

最佳答案

我遇到了同样的问题。我的 ExpandableListView 的 child 有两个 ImageView 和一个 TextView。在布局文件中将属性 TextView:focusable 设置为“false”解决了这个问题。奇怪的是,同样的代码(没有设置 focusable 属性)在 API8 中运行良好,但在 API16 模拟器上执行时,childs 没有响应点击。

关于Android ExpandableListView 和 onChildClick 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19555364/

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