【问题标题】:ExpandableListView onChildClickListenerExpandableListView onChildClickListener
【发布时间】:2012-01-06 14:25:23
【问题描述】:

我是安卓的新手。

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

}

【问题讨论】:

  • 我已经编辑了帖子并添加了代码..

标签: android android-edittext expandablelistadapter


【解决方案1】:

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

编辑

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

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

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

【讨论】:

  • 我尝试过,但没有成功...我已经编辑了我的问题并为其添加了代码...
  • @triggs :我也有同样的情况..我有多个(一个孩子上有3-4个文本视图)文本视图作为孩子..我想获取文本视图的文本..这怎么可能完成..请帮助
【解决方案2】:

这里发生的事情是您的 edittext 正在成为焦点。所以 onChildClick 没有被触发。尝试通过代码将 editText 焦点属性设置为 false。如果你在 .xml 文件中执行它仍然无法工作。希望它对你有用。:)

【讨论】:

  • 这个。太难了。我尝试了有关该主题的所有建议,但没有人提到这一点;在布局 XML 中设置它似乎是个好主意...
【解决方案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;**
    }

}

【讨论】:

    【解决方案4】:

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

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

    我只是添加一个代码sn-p以供参考...

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

    并在 Adapter 类中添加 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));
    

    【讨论】:

      【解决方案5】:

      迟到的答案,但也许它可以帮助其他人。我有同样的问题并将 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;
                      }
      
                  });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-16
        相关资源
        最近更新 更多