gpt4 book ai didi

java - android 抽屉导航不想被点击

转载 作者:行者123 更新时间:2023-12-01 13:09:56 25 4
gpt4 key购买 nike

我有一个奇怪的错误,我的抽屉导航似乎没有意识到它被单击。我搜索了很多东西,但似乎找不到答案。

 package com.BlackSheepApps.Eventpost.activities;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.BlackSheepApps.Eventpost.R;
import com.BlackSheepApps.Eventpost.adapters.AdapterNavigationDrawer;

public class ActivityBasic extends Activity{

private ActionBar actionbar;
private Resources resources;
private String[] drawerStrings;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView drawerList;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.drawer_layout);


//sets the customized action bar
actionbar = getActionBar();
resources = getResources();
actionbar.setBackgroundDrawable(new ColorDrawable(resources.getColor(R.color.opaque_blue)));

//make the drawer
drawerStrings = resources.getStringArray(R.array.drawer_categories);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerList = (ListView)findViewById(R.id.left_drawer);

drawerList.setAdapter(new AdapterNavigationDrawer(this, -1, drawerStrings));
drawerList.setOnItemClickListener(new DrawerItemClickListener());

drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};

// Set the drawer toggle as the DrawerListener
drawerLayout.setDrawerListener(drawerToggle);

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

registerViews();

}

/**
* This method is used for registering the views in the layout, BUT First you'll have to start using a self created method : registerLayout(int layout_id) to register the right layout in the right
* place.
*
*/
protected void registerViews(){

}

protected void ActionBarItemRemover(){

}

protected void registerLayout(int layout_id){
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View childView = inflater.inflate(layout_id, (ViewGroup)findViewById(layout_id));
drawerLayout.addView(childView);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d("Eventpost", "clicked");
selectItem(arg2);

}

}

private void selectItem(int position){
switch(position){
case 0:
startActivity(new Intent(this, ActivityHomeScreen.class));
break;
case 4:
startActivity(new Intent(this, ActivityProperties.class));
break;
}
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}

return super.onOptionsItemSelected(item);
}

}

适配器:

package com.BlackSheepApps.Eventpost.adapters;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.BlackSheepApps.Eventpost.R;

public class AdapterNavigationDrawer extends ArrayAdapter<String>{

String[] button_names;
Resources resources;

public AdapterNavigationDrawer(Context context, int resource,
String[] objects) {
super(context, resource, objects);
button_names = objects;
}

@Override
public View getView(int pos, View convertView, ViewGroup parent){
RelativeLayout row = (RelativeLayout)convertView;

if(null == row){
//No recycled View, we have to inflate one.
LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (RelativeLayout)inflater.inflate(R.layout.drawer_row, null);
}

TextView text = (TextView) row.findViewById(R.id.drawer_text);
//ImageView image =(ImageView) row.findViewById(R.id.drawer_image);
text.setText(button_names[pos]);

return row;
}



}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<TextView
android:id="@+id/drawer_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="23sp"
android:textColor="#FDFFED"
android:layout_marginLeft="5dp"
android:layout_marginTop="20dp"/>

</RelativeLayout>

当我尝试单击抽屉中的某个项目时,调试代码不会显示在控制台中,因此它不知道它已被单击。作为记录,我确实在 list 中注册了 Activity ......

最佳答案

在您调用的抽屉切换上,invalidateOptionsMenu();//创建对 onPrepareOptionsMenu() 的调用,明确指出它将调用 onPrepareOptionsMenu() 方法。但是它尚未实现,因此您的抽屉导航没有响应点击。

将其实现为,

/***
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}

来源here .

根据您的代码和要求自定义上述内容,它应该可以工作。您还可以查看上面的链接,获取有关此主题的分步教程。享受编码的乐趣和快乐。 :)

编辑:

像这样,创建一个 boolean 变量isDrawerOpen并将其设置如下,

       public void onDrawerClosed(View view) {
super.onDrawerClosed(view);

isDrawerOpen = false;
// supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
isDrawerOpen = true;
// supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}

然后在您的项目上单击抽屉中的 View ,调用 closeDrawer(); 方法,如下所示,

public void closeDrawer() {
if (isDrawerOpen) {
mDrawerLayout
.closeDrawer((LinearLayout) findViewById(R.id.left_drawer));
}
}

关于java - android 抽屉导航不想被点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22964358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com