2012-11-05 87 views
3

我使用的是可擴展的listview根據南,北,東,中顯示印度的狀態。狀態列表顯示像那樣,但是當應用onChildclickListener到expandableListView時沒有響應。我想點擊狀態印度開放了一項新活動。ExpandabeListView不支持ChildClickListener在android

  1. 膨脹列表視圖

    <ExpandableListView 
    android:id="@+id/indiaParts" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:groupIndicator="@null" 
    android:listSelector="@null" 
    android:scrollbars="none" 
    /> 
    
  2. list_group.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/group_header_bg" 
    android:gravity="center_vertical" 
    > 
    
    <ImageView 
        android:id="@+id/tag_img" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentTop="true" 
        android:layout_marginRight="10dip" 
        android:src="@drawable/group_down"  
        /> 
    
    <TextView 
        android:id="@+id/group_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_marginLeft="10dip" 
        android:text="Sample" 
        android:textColor="#FFFFFF" 
        android:textSize="18dip" 
        android:textStyle="bold" 
        /> 
    

  3. list_group_i tem.xml

    <?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="wrap_content" 
        android:background="@drawable/group_item_normal" 
        android:id="@+id/groupItem" 
        android:clickable="true" 
        > 
    
        <TextView 
         android:id="@+id/item_title" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="15dip" 
         android:text="sample" 
         android:textColor="#000000" 
         android:textSize="20sp" 
         android:layout_gravity="center" 
         android:gravity="center" 
         /> 
    
        </LinearLayout> 
    
  4. 適配器類

    public class ExpandableListAdapter extends BaseExpandableListAdapter { 
    
    private Context mContext; 
    private ExpandableListView mExpandableListView; 
    private ArrayList <GroupEntity> mGroupCollection; 
    private int[] groupStatus; 
    
    public ExpandableListAdapter(Context pContext, 
         ExpandableListView pExpandableListView, 
         ArrayList<GroupEntity> 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) { 
            // TODO Auto-generated method stub 
            groupStatus[arg0] = 1; 
           } 
          }); 
    
        mExpandableListView 
          .setOnGroupCollapseListener(new OnGroupCollapseListener() { 
    
           @Override 
           public void onGroupCollapse(int arg0) { 
            // TODO Auto-generated method stub 
            groupStatus[arg0] = 0; 
           } 
          }); 
    
    
    
    } 
    
    @Override 
    public String getChild(int arg0, int arg1) { 
        // TODO Auto-generated method stub 
        return mGroupCollection.get(arg0).getStatesList().get(arg1).getStateName(); 
    } 
    
    @Override 
    public long getChildId(int arg0, int arg1) { 
        // TODO Auto-generated method stub 
        return 0; 
    } 
    
    @Override 
    public View getChildView(int arg0, int arg1, boolean arg2, View arg3, 
         ViewGroup arg4) { 
        // TODO Auto-generated method stub 
    
        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).getStatesList().get(arg1).getStateName()); 
        return arg3; 
    } 
    
    @Override 
    public int getChildrenCount(int arg0) { 
        // TODO Auto-generated method stub 
        return mGroupCollection.get(arg0).getStatesList().size(); 
    } 
    
    @Override 
    public Object getGroup(int arg0) { 
        // TODO Auto-generated method stub 
        return mGroupCollection.get(arg0); 
    } 
    
    @Override 
    public int getGroupCount() { 
        // TODO Auto-generated method stub 
        return mGroupCollection.size(); 
    } 
    
    @Override 
    public long getGroupId(int arg0) { 
        // TODO Auto-generated method stub 
        return arg0; 
    } 
    
    @Override 
    public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) { 
        // TODO Auto-generated method stub 
        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).getGroupName()); 
    
        return arg2; 
    } 
    
    class GroupHolder { 
        ImageView img; 
        TextView title; 
    } 
    
    class ChildHolder { 
        TextView title; 
    } 
    
    @Override 
    public boolean hasStableIds() { 
        // TODO Auto-generated method stub 
        return true; 
    } 
    
    @Override 
    public boolean isChildSelectable(int arg0, int arg1) { 
        // TODO Auto-generated method stub 
        System.out.println("vcvv.........."+true); 
        return true; 
    } 
    

    }

  5. Activity類

     expandableListView=(ExpandableListView) findViewById(R.id.indiaParts); 
    
        expandableListAdapter=new ExpandableListAdapter(StatesScreen.this, expandableListView, groupEntityList); 
        expandableListView.setAdapter(expandableListAdapter); 
    
    
        expandableListView.setOnChildClickListener(new OnChildClickListener() { 
    
         @Override 
         public boolean onChildClick(ExpandableListView parent, View v, 
           int groupPosition, int childPosition, long id) { 
          // TODO Auto-generated method stub 
          Toast.makeText(StatesScreen.this, "Hi click",Toast.LENGTH_LONG).show(); 
          System.out.println("clicked..............////."+groupPosition); 
          Intent categoryList=new Intent(StatesScreen.this,CategoryList.class); 
          startActivity(categoryList); 
          return false; 
         } 
        }); 
    
+0

是否突出顯示了項目然後您嘗試推送? – neworld

+0

@新世界的兒童項目不是hihlighted。 – mukesh

+0

@neworld這個mehod只在滾動時打印值。 public boolean isChildSelectable(int arg0,int arg1){ \t \t // TODO自動生成方法存根 \t \t System.out.println(「vcvv ..........」+ true); \t \t return true; \t} – mukesh

回答

2

此問題出現,那麼您的孩子的視圖有自己的可聚焦元素。您可以通過刪除Android的刪除:從孩子們可點擊佈局或設置爲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="wrap_content" 
    android:background="@drawable/group_item_normal" 
    android:id="@+id/groupItem" 
    <!-- remove clickable from here --> 
    <!-- or android:clickable="false" --> 
    > 

如果你的孩子元素不具有任何可點擊和可聚焦的元素,然後制定非標準的ListView可獲得焦點。

對不起我的英文不好

+0

@ mukesh不客氣 – neworld

+0

&一件事,誰說你的英語不好。 – mukesh