为什么导航抽屉没有出现
why the navigationdrawer is not appearing
在下面发布的代码中,我尝试使用 navigationdrawer
,我按照教程中的步骤进行操作,但是当我运行应用程序时,navigationdrawer
没有显示。
请告诉我为什么它没有出现
mainActivity
public class MainActivity extends Activity {
private DrawerLayout drawerLayout;
private ListView drawerListView;
private String[] navDrawerOptions;
private TypedArray navDrawerIcons;
private ArrayList<NavDrawerModell> navDrawerModel;
private NavDrawerListAdapter adapter;
private final String ECO_ASSISTANT_DESC = "Eco-Assistant menu item is to ......";
private final String DATA_LOGGER_DESC = "Data-Logger menu item item is to ......";
private final String EXIT_DESC = "Press here to exit";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initViews(R.layout.main_activity);
}
private void initViews(int main_activity) {
// TODO Auto-generated method stub
setContentView(main_activity);
navDrawerOptions = getResources().getStringArray(R.array.nav_drawer_items);
navDrawerIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerListView = (ListView) findViewById(R.id.drawerListView);
navDrawerModel = new ArrayList<NavDrawerModell>();
navDrawerModel.add(new NavDrawerModell(navDrawerIcons.getResourceId(0, -1), navDrawerOptions[0], ECO_ASSISTANT_DESC));
navDrawerModel.add(new NavDrawerModell(navDrawerIcons.getResourceId(1, -1), navDrawerOptions[1], DATA_LOGGER_DESC));
navDrawerModel.add(new NavDrawerModell(navDrawerIcons.getResourceId(2, -1), navDrawerOptions[2], ECO_ASSISTANT_DESC));
navDrawerIcons.recycle();
adapter = new NavDrawerListAdapter(getApplicationContext(), this.navDrawerModel);
drawerListView.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
}
适配器:
public class NavDrawerListAdapter extends BaseAdapter{
private Context context;
private ArrayList<NavDrawerModell> navDrawerModel;
public NavDrawerListAdapter(Context context, ArrayList<NavDrawerModell> navDrawerModell) {
// TODO Auto-generated constructor stub
this.context = context;
this.navDrawerModel = navDrawerModell;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.navDrawerModel.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return this.navDrawerModel.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.drawer_list_layout, null);
}
ImageView iv_Icon = (ImageView) convertView.findViewById(R.id.iv_navDrawerOptionImage);
TextView tv_Title = (TextView) convertView.findViewById(R.id.tv_navDrawerOptionTitle);
TextView tv_Desc = (TextView) convertView.findViewById(R.id.tv_navDrawerOptionDescription);
iv_Icon.setImageResource(navDrawerModel.get(position).getIcon());
tv_Title.setText(navDrawerModel.get(position).gettitle());
tv_Desc.setText(navDrawerModel.get(position).getDescription());
return convertView;
}
}
型号:
public class NavDrawerModell {
private int icon;
private String title;
private String description;
public NavDrawerModell(int icon, String title, String description) {
// TODO Auto-generated constructor stub
this.icon = icon;
this.title = title;
this.description = description;
}
public void setIcon(int icon) {
this.icon = icon;
}
public int getIcon() {
return this.icon;
}
public void setTitle(String title) {
this.title = title;
}
public String gettitle() {
return this.title;
}
public void setDescription(String desc) {
this.description = desc;
}
public String getDescription() {
return this.description;
}
}
使用本教程创建导航抽屉https://developer.android.com/training/implementing-navigation/nav-drawer.html
创建抽屉开关并覆盖 onPostCreate
和 onConfigurationChanged
方法。
你实现了Listen for Open and Close Events部分了吗?您需要添加 ActionBarDrawerToggle
来处理打开和关闭事件。
在下面发布的代码中,我尝试使用 navigationdrawer
,我按照教程中的步骤进行操作,但是当我运行应用程序时,navigationdrawer
没有显示。
请告诉我为什么它没有出现
mainActivity
public class MainActivity extends Activity {
private DrawerLayout drawerLayout;
private ListView drawerListView;
private String[] navDrawerOptions;
private TypedArray navDrawerIcons;
private ArrayList<NavDrawerModell> navDrawerModel;
private NavDrawerListAdapter adapter;
private final String ECO_ASSISTANT_DESC = "Eco-Assistant menu item is to ......";
private final String DATA_LOGGER_DESC = "Data-Logger menu item item is to ......";
private final String EXIT_DESC = "Press here to exit";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initViews(R.layout.main_activity);
}
private void initViews(int main_activity) {
// TODO Auto-generated method stub
setContentView(main_activity);
navDrawerOptions = getResources().getStringArray(R.array.nav_drawer_items);
navDrawerIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerListView = (ListView) findViewById(R.id.drawerListView);
navDrawerModel = new ArrayList<NavDrawerModell>();
navDrawerModel.add(new NavDrawerModell(navDrawerIcons.getResourceId(0, -1), navDrawerOptions[0], ECO_ASSISTANT_DESC));
navDrawerModel.add(new NavDrawerModell(navDrawerIcons.getResourceId(1, -1), navDrawerOptions[1], DATA_LOGGER_DESC));
navDrawerModel.add(new NavDrawerModell(navDrawerIcons.getResourceId(2, -1), navDrawerOptions[2], ECO_ASSISTANT_DESC));
navDrawerIcons.recycle();
adapter = new NavDrawerListAdapter(getApplicationContext(), this.navDrawerModel);
drawerListView.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
}
适配器:
public class NavDrawerListAdapter extends BaseAdapter{
private Context context;
private ArrayList<NavDrawerModell> navDrawerModel;
public NavDrawerListAdapter(Context context, ArrayList<NavDrawerModell> navDrawerModell) {
// TODO Auto-generated constructor stub
this.context = context;
this.navDrawerModel = navDrawerModell;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.navDrawerModel.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return this.navDrawerModel.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.drawer_list_layout, null);
}
ImageView iv_Icon = (ImageView) convertView.findViewById(R.id.iv_navDrawerOptionImage);
TextView tv_Title = (TextView) convertView.findViewById(R.id.tv_navDrawerOptionTitle);
TextView tv_Desc = (TextView) convertView.findViewById(R.id.tv_navDrawerOptionDescription);
iv_Icon.setImageResource(navDrawerModel.get(position).getIcon());
tv_Title.setText(navDrawerModel.get(position).gettitle());
tv_Desc.setText(navDrawerModel.get(position).getDescription());
return convertView;
}
}
型号: public class NavDrawerModell {
private int icon;
private String title;
private String description;
public NavDrawerModell(int icon, String title, String description) {
// TODO Auto-generated constructor stub
this.icon = icon;
this.title = title;
this.description = description;
}
public void setIcon(int icon) {
this.icon = icon;
}
public int getIcon() {
return this.icon;
}
public void setTitle(String title) {
this.title = title;
}
public String gettitle() {
return this.title;
}
public void setDescription(String desc) {
this.description = desc;
}
public String getDescription() {
return this.description;
}
}
使用本教程创建导航抽屉https://developer.android.com/training/implementing-navigation/nav-drawer.html
创建抽屉开关并覆盖 onPostCreate
和 onConfigurationChanged
方法。
你实现了Listen for Open and Close Events部分了吗?您需要添加 ActionBarDrawerToggle
来处理打开和关闭事件。