gpt4 book ai didi

java - fragment mDrawer 菜单

转载 作者:行者123 更新时间:2023-12-02 05:58:55 25 4
gpt4 key购买 nike

我正在构建一个带有 mDrawer 菜单的简单 webview 应用程序,它在 3.0+ android 上工作就像一个魅力,但我希望它能够与 3 以下的版本一起工作。我已经阅读了有关 v4 支持的信息,并将其添加到项目中。应用程序是一个 Activity,然后我更改为 FragmentActivity,因为关闭了支持库。对不起我的英语。

这是代码。

public class MainActivity extends FragmentActivity  {
private LayoutInflater mInflater;
private DrawerLayout mDrawerLayout;
WebView browser;
// ListView represents Navigation Drawer
private ListView mDrawerList;

// ActionBarDrawerToggle indicates the presence of Navigation Drawer in the action bar
private ActionBarDrawerToggle mDrawerToggle;
private String mTitle = "";


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView browser = (WebView) findViewById(R.id.webView1);
browser.loadUrl("");
browser.setWebViewClient(new WebViewClient());
WebSettings webSettings = browser.getSettings();
webSettings.setJavaScriptEnabled(true);

mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);


mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

mDrawerList = (ListView) findViewById(R.id.drawer_list);

// Getting reference to the ActionBarDrawerToggle
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {

/** Called when drawer is closed */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);

invalidateOptionsMenu();

}

/** Called when a drawer is opened */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("Menu");

invalidateOptionsMenu();
}


};
browser.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
WebView webView = (WebView) v;

switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack())
{
webView.goBack();
return true;
}
break;
}
}

return false;
}
});

// Setting DrawerToggle on DrawerLayout
mDrawerLayout.setDrawerListener(mDrawerToggle);

// Creating an ArrayAdapter to add items to the listview mDrawerList
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),
R.layout.drawer_list_item, getResources().getStringArray(R.array.menus));

// Setting the adapter on mDrawerList
mDrawerList.setAdapter(adapter);

// Enabling Home button
getActionBar().setHomeButtonEnabled(true);

// Enabling Up navigation
getActionBar().setDisplayHomeAsUpEnabled(true);

// Setting item click listener for the listview mDrawerList
mDrawerList.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

// Getting an array of rivers
String[] menuItems = getResources().getStringArray(R.array.menus);

// Currently selected river
mTitle = menuItems[position];

// Creating a fragment object


// Passing selected item information to fragment
Bundle data = new Bundle();
data.putInt("position", position);
data.putString("url", getUrl(position));



// Getting reference to the FragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();

fragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
public void onBackStackChanged() {
if(getFragmentManager().getBackStackEntryCount() == 0) finish();
}
});

// Creating a fragment transaction
FragmentTransaction ft = fragmentManager.beginTransaction();

// Adding a fragment to the fragment transaction


// Committing the transaction
ft.commit();

// Closing the drawer
mDrawerLayout.closeDrawer(mDrawerList);

}
});


}

问题出在

fragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
public void onBackStackChanged() {
if(getFragmentManager().getBackStackEntryCount() == 0) finish();
}
});

FragmentManager 类型中的方法 addOnBackStackChangedListener(FragmentManager.OnBackStackChangedListener) 不适用于参数 (new FragmentManager.OnBackStackChangedListener(){})

最佳答案

您导入了错误的FragmentManager.OnBackStackChangedListener

你拥有什么

import android.app.FragmentManager.OnBackStackChangedListener;

你应该拥有什么

import android.support.v4.app.FragmentManager.OnBackStackChangedListener;

关于java - fragment mDrawer 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22835482/

25 4 0