gpt4 book ai didi

android - 无法调用 onDrawerOpened 和 onDrawerClosed

转载 作者:行者123 更新时间:2023-11-29 15:12:09 25 4
gpt4 key购买 nike

所以,我试图创建我的应用程序的抽屉导航,但我不知道为什么我不能调用 public void onDrawerOpened,它只是显示一个错误说 void is变量 onDrawerOpened 的无效类型 当我手动输入它时

所以这是我的 NavigationDrawerFragment.java 代码

package id.WKKR.ktcafe;

import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class NavigationDrawerFragment extends Fragment {

public DrawerLayout mDrawerLayout;
public ActionBarDrawerToggle mDrawerToggle;

public NavigationDrawerFragment() {
// TODO Auto-generated constructor stub
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_navigation_drawer, container,
false);

}

public void setUp(DrawerLayout drawerLayout, Toolbar toolbar) {
// TODO Auto-generated method stub
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout,
toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
{
public void onDrawerOpened(View drawerView){
super.onDrawerOpened(drawerView);
}

public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
}

};
}
}

感谢所有提供帮助的人。

(已编辑)

所以这是我的 mainactivity.java(称为 MenuUtama.java)

► ► package id.WKKR.ktcafe;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v7.widget.Toolbar;

@SuppressWarnings("deprecation")
public class MenuUtama extends ActionBarActivity{

Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_utama);

toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);

NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp((DrawerLayout)findViewById(R.id.drawer_layout),toolbar);


@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_utama, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(this, "This Is " + item.getTitle() + " Button", Toast.LENGTH_SHORT).show();
return true;
}

if (id == R.id.bill) {
startActivity(new Intent(this, BillTagihanAnda.class));
}
return super.onOptionsItemSelected(item);
}
}

最佳答案

您需要覆盖 onDrawerOpened 和 onDrawerClosed 方法。添加@override 注解或实现如下所示:

mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerlayout, toolBar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);

}

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);

};

将 Activity 中的设置方法更改为:

NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setup(R.id.fragment_navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

fragment 中的设置方法包括我的一条评论中所说的 id fragment 。

关于android - 无法调用 onDrawerOpened 和 onDrawerClosed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29982600/

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