ExpandableListView onChildClickListener

时间:2012-01-06 14:25:24

标签: android android-edittext expandablelistadapter

我是安卓的新蜜蜂。

我正在使用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;
}

}

5 个答案:

答案 0 :(得分:3)

没有看到代码这只是一个猜测,但是childCLickListener可能正在使用该事件,因此它没有到达编辑文本,尝试在childClickListener中返回false

修改

好吧,我刚用你的代码创建了一个小项目,我无法触发onCildClickListener,将其更改为TextView,并且工作正常。它与EditText有关(可能它正在消耗事件或者它没有注册为点击但是作为焦点的变化,但我只是猜测这一点)。

所以在回答你的问题时,是否有可能 - 我不这么认为。但我确实做了一个可能有用的小解决方案。

onTouchListener()附加到EditText并检查MotionEvent.ACTION_DOWN

答案 1 :(得分:3)

这里发生的事情是你的edittext正在关注焦点。所以onChildClick没有被解雇。尝试通过代码使editText焦点属性为false。如果你在.xml文件中执行它仍然无法正常工作。希望它适用于你。:)

答案 2 :(得分:3)

遇到了同样的问题(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;**
    }

}

答案 3 :(得分:1)

我遇到了同样的问题...... N指的是我编码的上述答案......这就是我发现的......

  • 确保SimplerExpandableListAdapters扩展BaseExpandableListAdapter并实现OnChildClickListener。
  • 将onchildclick添加为customExpandableList.setOnChildClickListener(new MyCustomAdapter(MainActivity.this))。 * “argument”是你在SimplerExpandableListAdapter类中收到的。 * “MyCustomAdapter”应替换为您的ExpandableListAdapter(简称为SimplerExpandableListAdapter)

我正在添加一个代码片段以供参考......

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

并在Adapterclass中添加onchildclick。如图所示

    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;
      }
    }

在编译Expandableview的活动中添加调用onChildclickListener。(在我的示例中为MainActivity类)。

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

答案 4 :(得分: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;
                }

            });