gpt4 book ai didi

java - 从 Activity 类中打开 Android 抽屉导航

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:35 25 4
gpt4 key购买 nike

我正在开发 android Navigation Drawer ,通过他们的文档看起来,抽屉只能扩展 Fragment Activity,因此要从我的所有 Activity 中打开抽屉,我需要将我的所有 Activity 设为片段,这不是一个可行的解决方案。

有没有办法打开一个从 Activity 扩展 FragmentActivity 的抽屉?

当我尝试从 Activity 类扩展抽屉 Activity ,以及另一个将打开扩展抽屉 Activity 类(此处为 SlideMenuActivity)的抽屉的 Activity 时,应用程序崩溃并给出 NullPointerException。

下面是打开抽屉布局的代码,但是一旦第一个 Activity 启动,我就无法访问抽屉。

应用程序在 onPostCreate 方法中的同步状态点崩溃

@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
getActionDrawerToggle().syncState();
}

public class SlideMenuActivity extends FragmentActivity implements OnItemClickListener


{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setDrawerLayout();
setDrawerList();
if (savedInstanceState == null)
{
getDrawerListView().setSelectionAfterHeaderView();
getDrawerListView().setSelection(1);
selectItem(1);
}
}
//
// @Override
// public void setContentView(int layoutResID) {
// // TODO Auto-generated method stub
// super.setContentView(layoutResID);
// }

private DrawerLayout getDrawerView()
{
return (DrawerLayout)findViewById(R.id.drawer_layout);
}

private ListView getDrawerListView()
{
return (ListView) findViewById(R.id.left_drawer);
}

private ActionBarDrawerToggle getActionDrawerToggle()
{
ActionBarDrawerToggle drawerToggle=new ActionBarDrawerToggle(
this, /* host Activity */
getDrawerView(), /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
@Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}

@Override
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
}
};
return drawerToggle;
}

private void setDrawerLayout(){
// set a custom shadow that overlays the main content when the drawer opens
getDrawerView().setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK);
// enable ActionBar app icon to behave as action to toggle navigation drawer
getDrawerView().setDrawerListener(getActionDrawerToggle());

}

/**
* Set up the drawer's list view with items and click listener
*/
private void setDrawerList()
{
ImageView imageView=new ImageView(this);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.precision_logo));
CustomBaseAdapter adapter=new CustomBaseAdapter();
adapter.list=getListViewData();
adapter.context=this;
ListView drawerList=getDrawerListView();
drawerList.setHeaderDividersEnabled(true);
drawerList.addHeaderView(imageView, null, false);
drawerList.setScrollingCacheEnabled(false);
drawerList.setAdapter(adapter);
drawerList.setOnItemClickListener(this);

}

private void selectItem(int position){

// update the main content by replacing fragments

// Fragment fragment=null;
// FragmentManager manager=getSupportFragmentManager();
switch (position) {
case 1:
this.startActivity(new Intent(this,SavedTankListActivity.class));
// fragment = new SavedMixesFragment();
// fragment = new SavedTankListActivity();
// manager.beginTransaction().replace(R.id.content_frame, fragment).commit();
break;

case 2:
// fragment=new MixGuideFragment();
// manager.beginTransaction().replace(R.id.content_frame, fragment).commit();
break;

default:
// fragment = new SavedMixesFragment();
// manager.beginTransaction().replace(R.id.content_frame, fragment).commit();

break;
}

getDrawerView().closeDrawer(getDrawerListView());

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
getActionDrawerToggle().syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
getActionDrawerToggle().onConfigurationChanged(newConfig);
}

private ArrayList<DrawerListModel> getListViewData()
{
ArrayList<DrawerListModel> listViewData=new ArrayList<DrawerListModel>();
String[] listItemArray=getResources().getStringArray(R.array.slide_bar_list_item_array);
for(int index=0;index<listItemArray.length;index++)
{
DrawerListModel model=new DrawerListModel();
model.listItem=listItemArray[index];
listViewData.add(model);
}
return listViewData;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
view.setSelected(true);
selectItem(position);
}

public void openDrawerList(View view){
getDrawerView().openDrawer(getDrawerListView());
}
}

最佳答案

我也在查看文档,并认为我必须将所有 Activity 转换为片段。澄清一下,事实并非如此。您可以在线性布局或任何需要的情况下拥有任意数量的元素。

例如,如果您没有 appdrawer 的基本 Activity 是:

<RelativeLayout>
<TextView>
</TextView>
<Button>
<Button>
</RelativeLayout>

简单地做

<android.support.v4.widget.DrawerLayout>

<RelativeLayout>
<TextView>
</TextView>
<Button>
<Button>
</RelativeLayout>

<ListView>
</ListView>
</android.support.v4.widget.DrawerLayout>

关于java - 从 Activity 类中打开 Android 抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17779915/

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