为什么导航器没有出现

时间:2015-01-08 14:26:45

标签: android navigation-drawer

在下面发布的代码中我试着拥有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;
}

}

潜行:     公共类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;
}

}

2 个答案:

答案 0 :(得分:0)

使用本教程创建导航抽屉https://developer.android.com/training/implementing-navigation/nav-drawer.html

创建抽屉切换并覆盖onPostCreateonConfigurationChanged方法。

答案 1 :(得分:0)

您是否实施了Listen for Open and Close Events部分?您需要添加ActionBarDrawerToggle来处理打开和关闭事件。