Android expandablelistview and onchildclick do not work properly
•
Android
I've been reading a lot of threads with the same problem as me, but I can't figure out what happened. I have an extensible list view, but I can't make onchildclicklistener work. Ongroupclicklistener works normally, but I really don't need it. I've seen many examples, and they seem to be like me. This is the code:
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;
}
});
Children's view:
<?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>
Group layout:
<?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 layout:
<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>
Adapter code:
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;
}
}
thank you very much.
resolvent:
I have the same problem. My expandablelistview children have two imageviews and one textview. Setting the attribute textview: focusable to "false" in the layout file solves this problem. Strangely, the same code (without focusable attribute) works well in API8, but when executed on the api16 simulator, the children do not respond to click
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码