首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ExpandableListView OnChildClickListener不工作

ExpandableListView OnChildClickListener不工作
EN

Stack Overflow用户
提问于 2012-07-18 03:27:20
回答 3查看 14.4K关注 0票数 17

我的安卓应用程序上的可扩展ListView出现了问题

这是我的代码

代码语言:javascript
复制
package proyek.akhir;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;

public class list_tempat extends Activity {
private List<String> groupData;
private List<List<String>> childrenData;
static String asalfromList ;
protected static final Object[] String = null;
static String l1 ;
private void loadData() {
    groupData = new ArrayList<String>();
    groupData.add("Group 1");
    groupData.add("Group 2");
    groupData.add("Group 3");

    childrenData = new ArrayList<List<String>>();
    List<String> sub1 = new ArrayList<String>();
    sub1.add("G1 Item 1");
    sub1.add("G1 Item 2");
    childrenData.add(sub1);
    List<String> sub2 = new ArrayList<String>();
    sub2.add("G2 Item 1");
    sub2.add("G2 Item 2");
    sub2.add("G2 Item 3");
    sub2.add("G2 Item 4");
    childrenData.add(sub2);
    List<String> sub3 = new ArrayList<String>();
    sub3.add("G3 Item 1");
    sub3.add("G3 Item 2");
    sub3.add("G3 Item 3");
    sub3.add("G3 Item 4");
    sub3.add("G3 Item 5");
    childrenData.add(sub3);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.expandable_list_view);

    l1 = "l1";

    loadData();

    ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandable_list_view);
    expandableListView.setAdapter(new ExpandableAdapter());
    expandableListView.setOnGroupClickListener(new OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View clickedView, int groupPosition, long groupId) {
            return false;
        }
    });
    expandableListView.setOnChildClickListener(new OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandablelistview,
                View clickedView, int groupPosition, int childPosition, long childId) {
            Intent i = getIntent();

            //asalfromList =(String) ((TextView) view).getText();
            Intent intent = new Intent(list_tempat.this, ruteangkot.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("l1", String);
            intent.putExtra("textAsal",asalfromList );

            if (i.getStringExtra("tujuan") != null){
                intent.putExtra("textTujuan",list_tempat2.tujuanfromList );
                System.out.println("adaan");
            }else{
                System.out.println("eweh");
                intent.putExtra("textTujuan","");
            }
            menuutama.mu = "";
            list_tempat2.l2 = "";

            startActivity(intent);
            finish();
            System.out.println("wkwkwk");
            showMessage("hahasu" + ((TextView)clickedView).getText());
            return false;
        }
    });
    expandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {

        }
    });
    expandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {

        }
    });
}

private class ExpandableAdapter extends BaseExpandableListAdapter {

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childrenData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView text = null;
        if (convertView != null) {
            text = (TextView)convertView;
            text.setText(childrenData.get(groupPosition).get(childPosition));
        } else {
            text = createView(childrenData.get(groupPosition).get(childPosition));
        }
        return text;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childrenData.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupData.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groupData.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TextView text = null;
        if (convertView != null) {
            text = (TextView)convertView;
            text.setText(groupData.get(groupPosition));
        } else {
            text = createView(groupData.get(groupPosition));
        }
        return text;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private TextView createView(String content) {
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(  
                ViewGroup.LayoutParams.FILL_PARENT, 38);  
        TextView text = new TextView(list_tempat.this);  
        text.setLayoutParams(layoutParams);  
        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
        text.setPadding(40, 0, 0, 0);  
        text.setText(content);
        return text;
    }
}

private void showMessage(String message) {
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}

expandableListView.setOnChildClickListener不工作我想从子列表视图中捕获数据,有谁能帮我吗?谢谢

EN

回答 3

Stack Overflow用户

发布于 2012-09-05 22:38:53

您必须激活您的孩子是可选择的!为此,在类ExpandableListAdapter的(被覆盖的) isChildSelectable方法中返回true。

代码语言:javascript
复制
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
票数 62
EN

Stack Overflow用户

发布于 2013-12-27 06:58:50

如果您使用的是OnChildClickListener和/或OnGroupClickListener,那么组的行和子行中的每个子视图都不能是可聚焦的。例如,如果在子项中有复选框,则将该复选框设置为不可聚焦:

代码语言:javascript
复制
checkBox.setFocusable(false);

此外,如果您将组/子convertView设置为clickable,则会阻止单击到达OnChildClickListenerOnGroupClickListener。如果是这样,请在您的expListViewAdapter中转到getGroupView,并设置:

代码语言:javascript
复制
convertView.setClickable(false);

然后在您的expListViewAdapter中转到getChildView,并设置:

代码语言:javascript
复制
convertView.setClickable(false);

在此之后,OnGroupClickListenerOnChildClickListener都应该可以工作-如果您首先设置侦听器(使用expandableListView.setOnGroupClickListener(...)expandableListView.setOnChildClickListener(...))

票数 29
EN

Stack Overflow用户

发布于 2015-05-25 13:39:43

您必须更改返回值为true而不是false。使子视图可选。

使用以下命令:

代码语言:javascript
复制
  @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

而不是:

代码语言:javascript
复制
@Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11529472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档