NavigationDrawer 在 itemclick 处不执行任何操作
Posted
技术标签:
【中文标题】NavigationDrawer 在 itemclick 处不执行任何操作【英文标题】:NavigationDrawer does nothing at itemclick 【发布时间】:2014-02-01 15:50:33 【问题描述】:我搜索了几个小时是否有人遇到或遇到了和我一样的问题,但我找不到任何有助于解决这个问题的东西...
问题是,NotificationDrawer 打开了,里面有我给他的所有项目,但他没有注意到我在任何项目上的 onClick! APP 只是关闭了抽屉,没有任何反应,尝试了记录和不同类型的检查是否有任何项目被点击。
我使用的代码:
public class run extends Activity implements OnInitListener
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.options_array);
// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, drawerListViewItems));
// 2.1 create ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "clos
e drawer" description */
)
public void onDrawerClosed(View view)
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
public void onDrawerOpened(View drawerView)
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
;
// 2.2 Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
// 2.3 enable and show "up" arrow
getActionBar().setDisplayHomeAsUpEnabled(true);
// just styling option
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
drawerListView.setOnItemClickListener(new SlideMenuClickListener());
if (savedInstanceState == null)
// on first time display view for first nav item
Log.d("LOGTAG", "DoSomething");
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
// display view for selected nav drawer item
selectItem(position);
private void selectItem(int position)
Log.d("LOGTAG", "ETWAS WURDE GEÄHLT! " + position);
switch(position)
case 0:
Log.d("LOGTAG", "ITEM 0 GEWÄHLT!");
case 1:
Log.d("LOGTAG", "Hier ist jetzt DING 1 gewählt!");
Toast.makeText(mContext, "Item 1 Clicked", Toast.LENGTH_SHORT).show();
// mDrawerList.setItemChecked(position, true);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Lauf beenden")
.setMessage("Bist du sicher?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Ja",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog,
int which)
// Yes button clicked, do something
finish();
).setNegativeButton("Nein", null).show();
// return true;
default:
// return (position);
@Override
protected void onPostCreate(Bundle savedInstanceState)
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
@Override
public void onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
menu.getItem(0).setVisible(false);
menu.getItem(1).setVisible(false);
menu.getItem(3).setVisible(false);
return super.onCreateOptionsMenu(menu);
@Override
public boolean onOptionsItemSelected(MenuItem item)
// toggle nav drawer on selecting action bar app icon/title
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
// Handle action bar actions click
switch (item.getItemId())
case R.id.close:
return true;
default:
return super.onOptionsItemSelected(item);
字符串.xml
<!-- TestTest -->
<string-array name="options_array">
<item>End run</item>
<item>Test X</item>
</string-array>
<string name="drawer_open">StringDrawerOpen</string>
<string name="drawer_close">StringDrawerClose</string>
drawer_list_items.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
activity_main.xml
<FrameLayout android:id="@+id/content_frame" android:layout_ android:layout_ /> <ListView android:id="@+id/left_drawer" android:layout_ android:layout_ android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="#111"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_ android:layout_ android:background="#FFFFFF" android:orientation="vertical" > <RelativeLayout android:layout_ android:layout_ > <ImageView android:id="@+id/herzschlagImg" android:layout_ android:layout_ android:contentDescription="@string/heartImgDes" android:src="@drawable/heartbeat" /> <ImageView android:id="@+id/distanzImg" android:layout_ android:layout_ android:contentDescription="@string/distImgDes" android:paddingTop="150dp" android:src="@drawable/distance" /> <TextView android:id="@+id/geschwindigkeit" android:layout_ android:layout_ android:layout_alignBaseline="@+id/distanz" android:layout_alignBottom="@+id/distanz" android:layout_toLeftOf="@+id/geschwindigkeitVar" android:text="@string/speed" android:textSize="40sp" /> <TextView android:id="@+id/herzschlag" android:layout_ android:layout_ android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="48dp" android:minHeight="60dp" android:minWidth="200dp" android:text="@string/heartbeat" android:textSize="40sp" /> <TextView android:id="@+id/herzschlagVar" android:layout_ android:layout_ android:layout_alignBaseline="@+id/herzschlag" android:layout_alignBottom="@+id/herzschlag" android:layout_marginLeft="40dp" android:layout_toRightOf="@+id/distanzVar" android:text="@string/heartbeatDes" android:textSize="30sp" /> <TextView android:id="@+id/geschwindigkeitVar" android:layout_ android:layout_ android:layout_alignBaseline="@+id/distanz" android:layout_alignBottom="@+id/distanz" android:layout_alignParentRight="true" android:text="@string/speedDes" android:textSize="30sp" /> <TextView android:id="@+id/distanz" android:layout_ android:layout_ android:layout_alignBottom="@+id/distanzImg" android:layout_alignRight="@+id/start" android:layout_marginBottom="10dp" android:layout_marginRight="20dp" android:text="@string/distance" android:textSize="40sp" /> <TextView android:id="@+id/feedback" android:layout_ android:layout_ android:layout_alignParentBottom="true" android:background="#848482" android:gravity="center" android:text="@string/feed" android:textSize="30sp" /> <Button android:id="@+id/start" android:layout_ android:layout_ android:layout_alignBaseline="@+id/stop" android:layout_alignBottom="@+id/stop" android:layout_toLeftOf="@+id/herzschlag" android:onClick="startTime" android:text="@string/btn_start" android:textSize="35sp" /> <Button android:id="@+id/stop" android:layout_ android:layout_ android:layout_above="@+id/feedback" android:layout_alignParentRight="true" android:layout_marginBottom="29dp" android:layout_marginRight="29dp" android:onClick="stopTime" android:text="@string/btn_stop" android:textSize="35sp" /> <TextView android:id="@+id/distanzVar" android:layout_ android:layout_ android:layout_alignBaseline="@+id/distanz" android:layout_alignBottom="@+id/distanz" android:layout_marginRight="36dp" android:layout_toLeftOf="@+id/geschwindigkeit" android:text="@string/distanceDes" android:textSize="30sp" /> <ImageView android:id="@+id/zeitImg" android:layout_ android:layout_ android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:contentDescription="@string/chronoDes" android:paddingTop="20dp" android:src="@drawable/timer" /> <Chronometer android:id="@+id/zeit" android:layout_ android:layout_ android:layout_alignLeft="@+id/geschwindigkeit" android:layout_below="@+id/geschwindigkeit" android:paddingTop="25dp" android:textSize="40sp" /> </RelativeLayout> </LinearLayout>
如果格式不好,请见谅。 我没有得到异常或其他东西,日志记录也无济于事...... 希望我没有忘记发布任何内容。
我真的希望你们能帮助我!
【问题讨论】:
你的ListView
(left_drawer) 嵌套在什么东西里了吗?
@user2713030 在activity_main.xml中:
Log.d("LOGTAG", "ETWAS WURDE GEÄHLT!" + 位置);你到这一步了吗??
【参考方案1】:
在这段代码中
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
// display view for selected nav drawer item
selectItem(position);
OnItemClickListener 接口仅在 AdapterView 上定义,而不在 ListView 上定义。据此,您的代码甚至在运行之前就应该给出错误。
ListView.OnItemClickListener
不应该工作。正确的应该是AdapterView.OnItemClickListener
【讨论】:
以上是关于NavigationDrawer 在 itemclick 处不执行任何操作的主要内容,如果未能解决你的问题,请参考以下文章
在 Robotium 中打开 NavigationDrawer 并选择项目的正确方法
带有片段的 NavigationDrawer 内的 Android TabLayout
如何在 Bloc Login Flutter 中使用带有身份验证的 NavigationDrawer 路由
NavigationDrawer 在 itemclick 处不执行任何操作