2012-01-06 67 views
3

我是一個新的蜜蜂到android。ExpandableListView onChildClickListener

我正在使用ExpandableListViewAdapter並面臨問題。

當我點擊編輯文本框時,onChildClickListener不起作用。

我的問題是它會在編輯文本框或不。

我正在實施BaseExpandableListAdapter。

在此先感謝。

public class TryExpandableListViewActivity extends Activity implements  OnChildClickListener { 

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

    llayout = (LinearLayout) findViewById(R.id.llayout); 

    ExpandableListView list = new ExpandableListView(this); 
    list.setGroupIndicator(null); 
    list.setChildIndicator(null); 
    String[] titles = {"A","B","C"}; 
    String[] fruits = {"a1","a2"}; 
    String[] veggies = {"b1","b2","b3"}; 
    String[] meats = {"c1","c2"}; 
    String[][] contents = {fruits,veggies,meats}; 
    SimplerExpandableListAdapter adapter = new SimplerExpandableListAdapter(this,titles, contents); 

    list.setAdapter(adapter); 

    llayout.addView(list); 

    list.setOnChildClickListener(this); 
} 

@Override 
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
    Log.i("Test","-------------------------------------------"); 
    return false; 
} 

}

class SimplerExpandableListAdapter extends BaseExpandableListAdapter { 

private Context mContext; 
private String[][] mContents; 
private String[] mTitles; 

public SimplerExpandableListAdapter(Context context, String[] titles, String[][] contents) { 
    super(); 
    if(titles.length != contents.length) { 
     throw new IllegalArgumentException("Titles and Contents must be the same size."); 
    } 
    mContext = context; 
    mContents = contents; 
    mTitles = titles; 

    } 

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    return mContents[groupPosition][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) { 

     EditText row = (EditText)convertView; 
     if(row == null) { 
      row = new EditText(mContext); 
     } 
     row.setTypeface(Typeface.DEFAULT_BOLD); 
     row.setText(mContents[groupPosition][childPosition]); 
     return row; 

} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return mContents[groupPosition].length; 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return mContents[groupPosition]; 
} 

@Override 
public int getGroupCount() { 
    return mContents.length; 
} 

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

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
    View convertView, ViewGroup parent) { 
    TextView row = (TextView)convertView; 
    if(row == null) { 
     row = new TextView(mContext); 
    } 
    row.setTypeface(Typeface.DEFAULT_BOLD); 
    row.setText(mTitles[groupPosition]); 
    return row; 
} 

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

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

}

+1

顯示一些代碼。 – 2012-01-06 14:51:20

+0

我編輯帖子並添加代碼.. – kanchan 2012-01-07 05:28:49

回答

3

沒有看到代碼,這只是一個猜測,但childCLickListener可使用該事件所以它沒有達到編輯文本,嘗試在childClickListener

返回false

編輯

好吧,我只是用你的代碼創建了一個小項目,我無法啓動onCildClickListener,將其更改爲TextView,並且工作正常。它與EditText有關(可能是它消耗事件,或者它沒有註冊爲點擊,但是作爲焦點的變化,但我只是在猜測這一點)。

所以在回答你的問題時,有可能 - 我不這麼認爲。但是我做了一個可能有用的小解決方法。

連接的onTouchListener()到的EditText和檢查MotionEvent.ACTION_DOWN

+0

我嘗試,但它沒有工作...我已編輯我的問題,併爲其添加代碼... – kanchan 2012-01-07 05:36:56

+0

@triggs:我也有相同的情況..我有多個(3-4個textviews一個孩子)textviews作爲孩子..我想獲得textview的文本..如何做到這一點..請幫助 – Shruti 2012-11-01 11:04:34

+0

不要忘記啓用適配器中的childSelectable屬性。 .. – 2014-10-20 12:47:45

3

正在發生的事情在這裏是你的EditText正在關注的焦點。所以onChildClick沒有被解僱。試圖通過代碼將editText焦點屬性設置爲false。如果你在.xml文件中執行它,它仍然不會工作。它很適合你。:)

+0

這。太難了。我嘗試了關於這個主題的所有建議,但沒有人提到這個問題。在佈局中設置XML似乎是一個好主意... – Project 2015-10-05 23:31:39

3

已經有同樣的問題OnChildClickListener不處理點擊事件)。爲了處理子項點擊,請確保您的ExpandableListAdapter返回 「true」(這 「費爾斯」 默認情況下)在isChildSelectable:

public class MyExpandableListAdapter extends BaseExpandableListAdapter { 

...

@Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     // TODO Auto-generated method stub 
     **return true;** 
    } 

} 
0

晚的答案,但也許它幫助別人。我有同樣的問題,並設置ExpandableListView到onItemClickListener()或onItemLongClickListener()做的:

mExpandListView 
      .setOnItemLongClickListener(new OnItemLongClickListener() { 

       @Override 
       public boolean onItemLongClick(AdapterView<?> adapterView, 
         View view, int position, long id) { 
        // TODO Auto-generated method stub 
        //do Your Code here 

        return true; 
       } 

      }); 
1

我面臨相同的概率... N指的是上面的回答我...編碼這裏就是我找到...

  • 確保SimplerExpandableListAdapters擴展BaseExpandableListAdapter並實現OnChildClickListener。
  • 將customChildclick添加爲customExpandableList.setOnChildClickListener(new MyCustomAdapter(MainActivity.this,))。 * 「參數」是您在SimplerExpandableListAdapter類中收到的內容。 * 「MyCustomAdapter」 應該與你的ExpandableListAdapter(SimplerExpandableListAdapter在烏拉圭回合的情況下)

我絕對增加了對參考代碼段...

public class MyCustomAdapter extends BaseExpandableListAdapter implements OnChildClickListener { 
     private LayoutInflater inflater; 
     private ArrayList<Parent> mParent; 

更換,添加onchildclick內Adapterclass。如圖所示

public class MyCustomAdapter extends BaseExpandableListAdapter implements OnChildClickListener { 
     private LayoutInflater inflater; 
     private ArrayList<Parent> mParent; 
     public MyCustomAdapter(Context context, ArrayList<Parent> parent) { 
     mParent = parent; 
     inflater = LayoutInflater.from(context); 
     } 

     @Override 
     public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4) { 
     // TODO Auto-generated method stub 
     Log.d("trial", "ExpandableList= "+arg0+" ,View= "+arg1+" ,arg2= "+arg2+", arg3= "+arg3+", arg4= "+arg4); 
     return true; 
     } 
    } 

添加調用的onChildclickListener在該Expandableview被編碼的活動。(在我的例子MainActivity類別)。

mExpandableList.setOnChildClickListener(new MyCustomAdapter(MainActivity.this, arrayParents)); 
+1

好,適合我 – RAY 2013-05-12 15:58:39