2012-01-06 66 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