2012-08-19 6 views
5

私はアンドロイドプログラミングの初心者です。私はhttp://shenhengbin.wordpress.comからコードをダウンロードして正常に動作しますが、唯一の問題は onChildClickを引き起こさないことです。ここでAndroidカスタムExpandablelistviewがonChildClickをトリガーしない

が主な活動ここ

public class MenuCategory extends Activity { 
     private ExpandableListView mExpandableListView; 
     private List<GroupEntity> mGroupCollection; 
     ExpandableListAdapter adapter; 

     @Override 
     public void onCreate(Bundle savedInstanceStater) { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.menucategory); 

      prepareResource(); 
      initPage(); 

     } 


     private void prepareResource() { 
      String data = null; 
      FileInputStream fIn = null; 
      InputStreamReader isr = null; 
      String response = null; 
      String getParam = "?get=menuCategory"; 
      try{ 
       fIn = openFileInput("fromfile.dat"); 
       isr = new InputStreamReader(fIn); 
       int size = fIn.available(); 
       char[] inputBuffer = new char[size]; 
       isr.read(inputBuffer); 
       data = new String(inputBuffer); 
       isr.close(); 
       fIn.close(); 
       String[] dataSep = data.split("#"); 
       String Hs = dataSep[0]; 

       try { 
        response = CustomHttpClient.executeHttpGet(Hs+getParam); 
        String res = response.toString(); 
        if(response.length()!=0){ 
         mGroupCollection = new ArrayList<GroupEntity>(); 
         JSONObject jsonObject = null; 
         JSONArray eventArray = new JSONArray(res); 
         for (int i = 0; i < eventArray.length(); i++) { 
          jsonObject = eventArray.getJSONObject(i); 
          GroupEntity ge = new GroupEntity(); 
          ge.Name = jsonObject.getString("Cat"); 

          JSONArray abc = jsonObject.getJSONArray("SubCat"); 
          for (int j=0; j<abc.length(); j++){ 
           GroupItemEntity gi = ge.new GroupItemEntity(); 
           gi.Name = abc.getString(j); 
           ge.GroupItemCollection.add(gi); 
          } 
          mGroupCollection.add(ge); 
         } 
        } 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


      }catch(IOException e){ 
       e.printStackTrace(System.err); 
      } 
     } 

     private void initPage() { 
      mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView); 
      adapter = new ExpandableListAdapter(this,mExpandableListView, mGroupCollection); 
      mExpandableListView.setAdapter(adapter); 
      registerForContextMenu(mExpandableListView); 
     } 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition,long id) { 
      String string = "Child Click"; 
      int duration = Toast.LENGTH_SHORT; 
      Toast toast = Toast.makeText(getApplicationContext(), string, duration); 
      toast.show(); 
      return false; 
     } 
    } 

カスタムBaseExpandableListAdapter

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context mContext; 
    private ExpandableListView mExpandableListView; 
    private List<GroupEntity> mGroupCollection; 
    private int[] groupStatus; 
    private int lastExpandedGroupPosition; 

    public ExpandableListAdapter(Context pContext, 
      ExpandableListView pExpandableListView, 
      List<GroupEntity> pGroupCollection) { 
     mContext = pContext; 
     mGroupCollection = pGroupCollection; 
     mExpandableListView = pExpandableListView; 
     groupStatus = new int[mGroupCollection.size()]; 

     setListEvent(); 
    } 

    private void setListEvent() { 

     mExpandableListView 
       .setOnGroupExpandListener(new OnGroupExpandListener() { 

        public void onGroupExpand(int arg0) { 
         // TODO Auto-generated method stub 
         groupStatus[arg0] = 1; 
         lastExpandedGroupPosition = arg0; 
        } 
       }); 

     mExpandableListView 
       .setOnGroupCollapseListener(new OnGroupCollapseListener() { 

        public void onGroupCollapse(int arg0) { 
         // TODO Auto-generated method stub 
         groupStatus[arg0] = 0; 
        } 
       }); 
    } 

    public String getChild(int arg0, int arg1) { 
     // TODO Auto-generated method stub 
     return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name; 
    } 

    public long getChildId(int arg0, int arg1) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    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).GroupItemCollection.get(arg1).Name); 
     return arg3; 
    } 

    public int getChildrenCount(int arg0) { 
     // TODO Auto-generated method stub 
     return mGroupCollection.get(arg0).GroupItemCollection.size(); 
    } 

    public Object getGroup(int arg0) { 
     // TODO Auto-generated method stub 
     return mGroupCollection.get(arg0); 
    } 

    public int getGroupCount() { 
     // TODO Auto-generated method stub 
     return mGroupCollection.size(); 
    } 

    public long getGroupId(int arg0) { 
     // TODO Auto-generated method stub 
     return arg0; 
    } 

    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).Name); 

     return arg2; 
    } 

    class GroupHolder { 
     ImageView img; 
     TextView title; 
    } 

    class ChildHolder { 
     TextView title; 
    } 

    public boolean hasStableIds() { 
     // TODO Auto-generated method stub 
     return true; 
    } 

    public boolean isChildSelectable(int arg0, int arg1) { 
     // TODO Auto-generated method stub 
     return true; 
    } 

    @Override 
    public void onGroupExpanded(int groupPosition){ 
     //collapse the old expanded group, if not the same 
     //as new group to expand 
     if(groupPosition != lastExpandedGroupPosition){ 
      mExpandableListView.collapseGroup(lastExpandedGroupPosition); 
     } 

     super.onGroupExpanded(groupPosition);   
     lastExpandedGroupPosition = groupPosition; 
    } 
} 

項目グループのレイアウトが

<?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" 
     android:focusable="false" 
     /> 

    <TextView 
     android:id="@+id/group_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="10dip" 
     android:text="" 
     android:textColor="#57810f" 
     android:textSize="18dip" 
     android:textStyle="bold" 
     android:focusable="false" 
     /> 

</RelativeLayout> 

、ここで項目の子のレイアウトは

であるです

あなたはinitPage方法(ないonCreate())でこのようにそれを行うことができますクリックする子供のためのリスナーを設定するには、私に

+0

子供のクリックに対してリスナーを設定して実際にそれらのイベントをリッスンするコードのどこにも表示されません。 – Luksprog

+0

私はmExpandableListView.setOnChildClickListener(新しいonChildClickListener(){ \t \t \t公共ブールonChildClick(ExpandableListView親、ビューV、int型groupPosition、int型childPosition、長いID)を追加するとき、{ \t \t文字列は= "子どもをクリックして"; \t \t \t INT時間= Toast.LENGTH_SHORT; \t \t \tトーストトースト= Toast.makeText(getApplicationContext()、文字列、持続時間); \t \tトースト。show(); \t \t falseを返します。 \t \t} \t \t}); onChildClickListenerは型に解決できないので、onclipse()のエラーを私に示します。 – Manu

+0

@Manu私も同じチュートリアルを使用しました。子のクリックに対してどのようにアクションを実行するか教えてください。 – mukesh

答えて

0

を助けてください:あなたは(isChildSelectableというメソッドを持っているアダプタで

mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView); 
      adapter = new ExpandableListAdapter(this,mExpandableListView, mGroupCollection); 
      mExpandableListView.setAdapter(adapter); 
registerForContextMenu(mExpandableListView); 
mExpandableListView.setOnChildClickListener(new OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
        int groupPosition, int childPosition, long id) { 
       String string = "Child Click"; 
       int duration = Toast.LENGTH_SHORT; 
       Toast toast = Toast.makeText(getApplicationContext(), string, 
         duration); 
       toast.show(); 
       return false; 
      } 
     }); 
+0

申し訳ありませんが、私に同じエラーを与えます – Manu

+0

@ Manuそして何がエラーを伝えますか?あなたは正しいインポートを持っていますか? 'import android.widget.ExpandableListView;'と 'import android.widget.ExpandableListView.OnChildClickListener;'? – Luksprog

+0

はい、私はインポートしました。ではなく、私にエラーを与える:型ExpandableListViewのメソッドsetOnChildClickListener(ExpandableListView.OnChildClickListener)は \t引数には適用されません(新しいOnChildClickListener(){}) \t - OnChildClickListenerタイプ – Manu

13

)魔女は偽を返します、それは問題です。問題を解決するには、戻り値をtrueに変更します。

+1

これはずっと前に正解としていたはずです。気づかないうちに私はすべての毛を摘み取った:P – LeoFarage

+0

正解を選んだ場合、それは他の人を助けたり、最終的にあなたのために働いた解決策を共有するのに役立つかもしれません。 –

1

私はそれが古いスレッドであることを知っていますが、答えを見つけるためにここに来る人がいれば、これが彼らのためです。 this tutorial for expandable list viewをクリックすると、「メイングループアイテムレイアウト」(自分のコードではlist_group_item)に移動し、linearlayoutとtextviewをクリックしてフェイスブックにすることができます。

4

私はこの問題に直面していましたが、onChildClickはトリガされていませんでした。

私はこのコメントが見つかりました:descendantFocusability =:

は解決、問題はあなたがchildviewにいくつかのTextView年代を持っている場合、フォーカス可能なプロパティはアンドロイドを追加する(私は正確な理由を知らない)偽でなければならないということですビューに "blocksDescendantsは、" だから私のために働いたがchildViewにandroid:descendantFocusability="blocksDescendants"を追加Android ExpandableListView and onChildClick not working

に問題

を解決しました。

+2

これは私のために働く唯一の解決策でした。 isChildSelectable()をtrueに設定するなどの他の一般的な提案は必要でしたが、問題を解決するには十分ではありませんでした。 –

+0

Genius !!!これは私のための解決策でした – Merlevede

関連する問題