ndroid的ExpandableListView和onChil

ndroid的ExpandableListView和onChil

本文介绍了Android的ExpandableListView和onChildClick不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读了很多,我也有同样问题的线程,但我无法弄清楚发生了什么。我有一个可扩展的列表视图,但我不能让onChildClickListener工作。该onGroupClickListener好的工作,但我不真的需要它。我看了很多例子,他们都似乎是为我的。这里是code:

I've been reading a lot of threads with the same problem that I have, but I can't figure out what's happening. I have a expandable list view, but I can't make the onChildClickListener to work. The onGroupClickListener is working ok, but I dont really need it. I looked at a lot of examples and they all seem to be as mine. Here 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;
                      }
                  });

子视图:

<?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布局:

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>

适配器code:

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


}

非常感谢你。

推荐答案

我有同样的问题。我ExpandableListView的童车有两个ImageViews和一个TextView。设置该属性的TextView:可聚焦在布局文件假解决了问题。
奇怪的是,在同一code(无设置聚焦的财产)的罚款曾在API8但是当API16模拟器执行,孩子的没有点击响应。

I had the same problem. My ExpandableListView's childs had two ImageViews and a TextView. Setting the property TextView:focusable to "false" in the layout file solved the issue.Strangely, the same code (without setting the focusable property) worked fine in API8 but when executed on API16 emulator, childs didn't respond to click.

这篇关于Android的ExpandableListView和onChildClick不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:32