ExpandableListView OnLongClick 无法被侦听器识别
我尝试了几种不同的组合(下面的评论是其中的片段)。调试无法识别曾经发生的点击。该项目突出显示,但没有任何反应。我尝试过膨胀自定义 row.xml 文件并使用行布局文件内的视图。没有例外,没有更多的想法。感谢您的关注。
此外,我意识到 onCreateContextMenu 方法有点简单。我只想弹出这个东西,然后我会处理细节!
主要活动
public class BrowseActivity extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
final ExpandableListView browseView = (ExpandableListView) findViewById(android.R.id.list);
DbHelper dbh = new DbHelper(BrowseActivity.this);
SQLiteDatabase db = dbh.getWritableDatabase();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(Items.ITEMS_TABLE_NAME);
Cursor mCursor = queryBuilder.query(db, new String[] { Items.ITEMS_ID,
Items.ITEMS_ITEM, Items.ITEMS_DESC }, null, null, null, null,
Items.DEFAULT_SORT_ORDER);
CursorTreeAdapter mAdapter = new MyExpandabaleListAdapter(this,
mCursor, R.layout.row, R.layout.exprow, new String[] {
Items.ITEMS_ITEM, Items.ITEMS_DESC }, new int[] {
R.id.txtItem, R.id.dscItemTwo }, new String[] {
Items.ITEMS_DESC, Items.ITEMS_MANU }, new int[] {
R.id.dscItem, R.id.manuItem });
browseView.setAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
public class MyExpandabaleListAdapter extends SimpleCursorTreeAdapter {
public MyExpandabaleListAdapter(Context context, Cursor c,
int groupLayout, int childLayout, String[] groupFrom,
int[] groupTo, String[] childrenFrom, int[] childrenTo) {
super(context, c, groupLayout, groupFrom, groupTo, childLayout,
childrenFrom, childrenTo);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {...}
}
public void OnCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 0, 0, "Add");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("Add Item").setIntent(new Intent(this, AddItemActivity.class));
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return false;
}
}
浏览布局 这是布局保存 ELV。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:id="@+id/expandLinLayout"
>
<ExpandableListView
android:id = "@android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:groupIndicator="@drawable/my_group_statelist"
>
</ExpandableListView>
</LinearLayout>
行布局 这是折叠布局。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_gravity="center_vertical|right"
android:id="@+id/txtItem"
android:text="Item"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="15dip"
></TextView>
<TextView
android:layout_gravity="center_vertical|right"
android:id="@+id/dscItemTwo"
android:text="Desciption"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textStyle="italic"
android:textColor="#666666"
></TextView>
展开布局 这是展开的布局。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_gravity="right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/manuItem"
android:textColor="#994020"
android:text="Manufacturer"
android:layout_marginRight="10dip"
></TextView>
<TextView
android:text="Description"
android:id = "@+id/dscItem"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dip"
android:textColor="#994020"
></TextView>
</LinearLayout>
I have tried several different combinations (comments below are fragments of this). Debug doesn't recognize the click as ever happening. The item highlights, but nothing happens. I have tried inflating the custom row.xml file and using the views inside row layout file as well. No exceptions, no more ideas. Thanks for looking.
Also, I realize the onCreateContextMenu method is a little light. I just want to get the thing to pop up, then I will take care of the details!
Main Activity
public class BrowseActivity extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
final ExpandableListView browseView = (ExpandableListView) findViewById(android.R.id.list);
DbHelper dbh = new DbHelper(BrowseActivity.this);
SQLiteDatabase db = dbh.getWritableDatabase();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(Items.ITEMS_TABLE_NAME);
Cursor mCursor = queryBuilder.query(db, new String[] { Items.ITEMS_ID,
Items.ITEMS_ITEM, Items.ITEMS_DESC }, null, null, null, null,
Items.DEFAULT_SORT_ORDER);
CursorTreeAdapter mAdapter = new MyExpandabaleListAdapter(this,
mCursor, R.layout.row, R.layout.exprow, new String[] {
Items.ITEMS_ITEM, Items.ITEMS_DESC }, new int[] {
R.id.txtItem, R.id.dscItemTwo }, new String[] {
Items.ITEMS_DESC, Items.ITEMS_MANU }, new int[] {
R.id.dscItem, R.id.manuItem });
browseView.setAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
public class MyExpandabaleListAdapter extends SimpleCursorTreeAdapter {
public MyExpandabaleListAdapter(Context context, Cursor c,
int groupLayout, int childLayout, String[] groupFrom,
int[] groupTo, String[] childrenFrom, int[] childrenTo) {
super(context, c, groupLayout, groupFrom, groupTo, childLayout,
childrenFrom, childrenTo);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {...}
}
public void OnCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 0, 0, "Add");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("Add Item").setIntent(new Intent(this, AddItemActivity.class));
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return false;
}
}
browse layout This is the layout the holds the ELV.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:id="@+id/expandLinLayout"
>
<ExpandableListView
android:id = "@android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:groupIndicator="@drawable/my_group_statelist"
>
</ExpandableListView>
</LinearLayout>
row layout This is the collapsed layout.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_gravity="center_vertical|right"
android:id="@+id/txtItem"
android:text="Item"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="15dip"
></TextView>
<TextView
android:layout_gravity="center_vertical|right"
android:id="@+id/dscItemTwo"
android:text="Desciption"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textStyle="italic"
android:textColor="#666666"
></TextView>
exprow layout This is the expanded layout.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_gravity="right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/manuItem"
android:textColor="#994020"
android:text="Manufacturer"
android:layout_marginRight="10dip"
></TextView>
<TextView
android:text="Description"
android:id = "@+id/dscItem"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dip"
android:textColor="#994020"
></TextView>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
后台阅读
在您的 registerForContextMenu()你不必须传入声明的列表:
http://developer.android.com/reference/android/app/ListActivity.html#getListView( )
或者根据您的情况:
http://www.androidjavadoc.com/1.0_r1_src/android/app/ExpandableListActivity.html#getExpandableListView()
StackOvflow 参考:如何在Android 上的 ListActivity?
Background reading
In your registerForContextMenu()do you not have to pass in the declared list:
http://developer.android.com/reference/android/app/ListActivity.html#getListView()
or in your case:
http://www.androidjavadoc.com/1.0_r1_src/android/app/ExpandableListActivity.html#getExpandableListView()
StackOvflow reference: How do you implement context menu in a ListActivity on Android?