我是android的新手。
我正在使用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;
}}
发布于 2012-01-06 23:25:50
在看不到代码的情况下,这只是一个猜测,但childCLickListener可能正在使用事件,因此它不会到达编辑文本,请尝试在childClickListener中返回false
编辑
好的,我刚刚用你的代码创建了一个小项目,我不能让onCildClickListener触发,把它改成一个TextView,它工作得很好。这与EditText有关(可能是它消费了事件,或者它没有注册为单击,而是作为焦点的变化,但我只是在猜测)。
所以在回答你的问题时,有没有可能--我不这么认为。但我确实做了一个可能有用的小变通方法。
将onTouchListener()连接到EditText并检查MotionEvent.ACTION_DOWN
发布于 2012-07-30 18:28:03
这里发生的事情是你的edittext正在成为焦点。因此,onChildClick不会让fired.Try通过code.If将editText focus属性设置为false,您可以在.xml文件中执行此操作,但它仍然不会work.Hopefully,它对您有效。:)
发布于 2012-12-11 22:38:09
我也遇到了同样的问题(OnChildClickListener不处理点击事件)。为了处理子项目点击,请确保您的ExpandableListAdapter在isChildSelectable中返回"true“(默认情况下返回"fales”):
public class MyExpandableListAdapter extends BaseExpandableListAdapter {..。
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
**return true;**
}
}https://stackoverflow.com/questions/8759291
复制相似问题