Java ChildClickListener上的ExpandableListView不';行不通
适配器Java ChildClickListener上的ExpandableListView不';行不通,java,android,expandablelistview,Java,Android,Expandablelistview,适配器 public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; private ExpandableListView mExpandableListView; private List <Grupe> mGroupCollection; private int[] groupStatus;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ExpandableListView mExpandableListView;
private List <Grupe> mGroupCollection;
private int[] groupStatus;
public ExpandableListAdapter() {}
public ExpandableListAdapter(Context pContext,
ExpandableListView pExpandableListView,
List <Grupe> pGroupCollection) {
mContext = pContext;
mGroupCollection = pGroupCollection;
mExpandableListView = pExpandableListView;
groupStatus = new int[mGroupCollection.size()];
setListEvent();
}
private void setListEvent() {
mExpandableListView
.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int arg0) {
groupStatus[arg0] = 1;
}
});
mExpandableListView
.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int arg0) {
groupStatus[arg0] = 0;
}
});
}
@Override
public String getChild(int arg0, int arg1) {
return mGroupCollection.get(arg0).getChildren().get(arg0).getPrimaoc_Poruke();
}
@Override
public long getChildId(int arg0, int arg1) {
return 0;
}
@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {
ChildHolder childHolder;
if (arg3 == null) {
arg3 = LayoutInflater.from(mContext).inflate(
R.layout.list_group_item, null);
childHolder = new ChildHolder();
childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
arg3.setTag(childHolder);
} else {
childHolder = (ChildHolder) arg3.getTag();
}
childHolder.title.setText(mGroupCollection.get(arg0).getChildren().get(arg1).getPrimaoc_Poruke());
return arg3;
}
@Override
public int getChildrenCount(int arg0) {
return mGroupCollection.get(arg0).getChildren().size();
}
@Override
public Object getGroup(int arg0) {
return mGroupCollection.get(arg0);
}
@Override
public int getGroupCount() {
return mGroupCollection.size();
}
@Override
public long getGroupId(int arg0) {
return arg0;
}
@Override
public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
GroupHolder groupHolder;
if (arg2 == null) {
arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group, null);
groupHolder = new GroupHolder();
groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
arg2.setTag(groupHolder);
} else {
groupHolder = (GroupHolder) arg2.getTag();
}
if (groupStatus[arg0] == 0) {
groupHolder.img.setImageResource(R.drawable.group_down);
} else {
groupHolder.img.setImageResource(R.drawable.group_up);
}
groupHolder.title.setText(mGroupCollection.get(arg0).getTip());
return arg2;
}
class GroupHolder {
ImageView img;
TextView title;
}
class ChildHolder {
TextView title;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私有上下文;
私有可扩展列表视图mExpandableListView;
私人名单收集;
私有int[]组状态;
公共ExpandableListAdapter(){}
公共ExpandableListAdapter(上下文pContext,
ExpandableListView PEExpandableListView,
列表(集合){
mContext=pContext;
mGroupCollection=pGroupCollection;
mExpandableListView=pExpandableListView;
groupStatus=newint[mGroupCollection.size()];
setListEvent();
}
私有void setListEvent(){
mExpandableListView
.setOnGroupExpandListener(新OnGroupExpandListener(){
@凌驾
public void onGroupExpand(int arg0){
groupStatus[arg0]=1;
}
});
mExpandableListView
.setOnGroupCollapseListener(新OnGroupCollapseListener(){
@凌驾
公共无效组折叠(int arg0){
groupStatus[arg0]=0;
}
});
}
@凌驾
公共字符串getChild(int arg0,int arg1){
返回mGroupCollection.get(arg0.getChildren().get(arg0.getPrimaoc_Poruke();
}
@凌驾
公共长getChildId(int arg0,int arg1){
返回0;
}
@凌驾
公共视图getChildView(int arg0、int arg1、布尔值arg2、视图arg3、,
视图组(arg4){
儿童持有人儿童持有人;
如果(arg3==null){
arg3=从(mContext)充气(
R.layout.list\u group\u item,空);
childHolder=新的childHolder();
childHolder.title=(TextView)arg3.findViewById(R.id.item_title);
arg3.设置标签(儿童持有人);
}否则{
childHolder=(childHolder)arg3.getTag();
}
childHolder.title.setText(mGroupCollection.get(arg0.getChildren().get(arg1.getPrimaoc_Poruke());
返回arg3;
}
@凌驾
公共整数getChildrenCount(整数arg0){
返回mGroupCollection.get(arg0.getChildren().size();
}
@凌驾
公共对象getGroup(int arg0){
返回mGroupCollection.get(arg0);
}
@凌驾
public int getGroupCount(){
返回mGroupCollection.size();
}
@凌驾
公共长getGroupId(int arg0){
返回arg0;
}
@凌驾
公共视图getGroupView(整数arg0、布尔值arg1、视图arg2、视图组arg3){
股东;股东;
如果(arg2==null){
arg2=LayoutInflater.from(mContext).充气(R.layout.list\u组,空);
groupHolder=新的groupHolder();
groupHolder.img=(ImageView)arg2.findViewById(R.id.tag\u img);
groupHolder.title=(TextView)arg2.findViewById(R.id.group\u title);
arg2.setTag(groupHolder);
}否则{
groupHolder=(groupHolder)arg2.getTag();
}
if(组状态[arg0]==0){
groupHolder.img.setImageResource(R.drawable.group_down);
}否则{
groupHolder.img.setImageResource(R.drawable.groupup);
}
groupHolder.title.setText(mGroupCollection.get(arg0.getTip());
返回arg2;
}
类股东{
图像视图img;
文本视图标题;
}
班级儿童持有者{
文本视图标题;
}
@凌驾
公共布尔表ID(){
返回true;
}
@凌驾
公共布尔值isChildSelectable(int arg0,int arg1){
返回true;
}
}
main活动
public class Glavna extends Activity implements OnClickListener, OnChildClickListener {
private List <Grupe> mGroupCollection;
private ExpandableListView mExpandableListView;
private ExpandableListAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavna);
prepareResource();
initPage();
}
private void initPage() {
mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
adapter = new ExpandableListAdapter(this,mExpandableListView, mGroupCollection);
mExpandableListView.setAdapter(adapter);
mExpandableListView.setOnChildClickListener(this);
}
private void prepareResource() {
mGroupCollection = new ArrayList <Grupe> ();
ge.setTip("Online korisnici");
mGroupCollection.add(ge);
AktivniChat gi = new AktivniChat(1, "receiver 1", new Korisnik(1, "sender1"));
mGroupCollection.get(0).setAktivniChat(gi);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
return true;
}
}
public类Glavna扩展活动实现OnClickListener、OnChildClickListener{
私人名单收集;
私有可扩展列表视图mExpandableListView;
私有ExpandableListAdapter=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavna);
prepareResource();
initPage();
}
私有void initPage(){
mExpandableListView=(ExpandableListView)findViewById(R.id.expandableListView1);
adapter=新的ExpandableListAdapter(this,mExpandableListView,mGroupCollection);
setAdapter(适配器);
mExpandableListView.setOnChildClickListener(这个);
}
私有void prepareResource(){
mGroupCollection=newarraylist();
ge.setTip(“在线korisnici”);
mGroupCollection.add(通用电气);
AktivniChat gi=新AktivniChat(1,“接收者1”,新Korisnik(1,“发送者1”);
mGroupCollection.get(0.setAktivniChat(gi);
}
@凌驾
公共布尔onChildClick(ExpandableListView父视图、视图v、int-groupPosition、int-childPosition、长id){
返回true;
}
}
在所有元素上设置android:focusable=“false”
<?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:baselineAligned="false"
android:paddingTop="5dip"
android:focusable="false">
<LinearLayout
android:id="@+id/groupItem"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFF385"
android:clickable="false"
android:focusable="false">
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF84"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="sample"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
这对我来说也很有效。。。布局中的所有项(子项)必须设置为focusable=“false”和clickable=“false”。但是,如果在子布局中有按钮,这将不起作用。不要忘记启用子可选属性
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
为我工作,有人知道为什么吗?这是虫子吗?据我所知这不是虫子。childView中的视图只捕获onClick()事件,因此该事件永远不会到达childView。(这就是那个从未被叫来过的孩子们的听众)