自定义NavigationDrawer

时间:2014-11-21 00:47:56

标签: android navigation-drawer

我的自定义NavigationDrawer遇到了一些问题。事实上,它打开和关闭很好,但它没有显示列表视图...... 由于logcat没有显示任何错误,所以很难找到我的错误,所以也许另外一双眼睛会轻易发现它!

ObjectDrawerItem:

  public class ObjectDrawerItem {

  public int icon;
  public String name;

  public ObjectDrawerItem(int icon, String name) {
      this.icon = icon;
      this.name = name;
  }
}

DrawerItemCustomAdapter:

public class DrawerItemCustomAdapter extends ArrayAdapter<ObjectDrawerItem> {

  Context mContext;
  int mLayoutResourceId;
  ObjectDrawerItem mData[] = null;

  public DrawerItemCustomAdapter(Context context, int resource, ObjectDrawerItem[] data) {

      super(context, resource);
      this.mLayoutResourceId = resource;
      this.mContext = context;
      this.mData = data;
  }



  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

      View listItem = convertView;
      ObjectDrawerItem folder = mData[position];


      LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
      listItem = inflater.inflate(mLayoutResourceId, parent, false);

      ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
      TextView textViewName = (TextView) listItem.findViewById(R.id.textViewName);

      imageViewIcon.setImageResource(folder.icon);
      textViewName.setText(folder.name);

      return listItem;
  }

}

NavigationDrawerHelper:

public class NavigationDrawerHelper{

  private String[] mNavigationDrawerItemTitles;
  DrawerLayout mDrawerLayout;
  ListView mDrawerListView;
  private ActionBarDrawerToggle mDrawerToggle;

  public void init(Activity activity, ListView.OnItemClickListener listener) {

      mNavigationDrawerItemTitles = activity.getResources().getStringArray(R.array.navigation_drawer_items_array);
      mDrawerLayout = (DrawerLayout) activity.findViewById(R.id.drawer_layout);
      mDrawerListView = (ListView) activity.findViewById(R.id.left_drawer);

      // list the drawer items
      ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];

      drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_action_copy, "Create");
      drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_action_refresh, "Read");
      drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_share, "Help");


      DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(activity, R.layout.listview_item_row, drawerItem);
      mDrawerListView.setAdapter(adapter);
      mDrawerListView.setOnItemClickListener(listener);


      mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
      mDrawerListView.setItemChecked(0, true);

      setupActionBar(activity);

  }
}

在我的 Activity(实现ListView.OnItemClickListener)的onCreate()中,我调用了NavigationDrawerHelper init():

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

    updateWeather();

    mNavigationDrawerHelper = new NavigationDrawerHelper();
    mNavigationDrawerHelper.init(this,this);
}

1 个答案:

答案 0 :(得分:0)

最终,看起来问题与您的适配器一样,它扩展了ArrayAdapter。改为扩展并使用BaseAdapter。

问题在于你传入了数据,但是ArrayAdapter本身并不知道数据的大小[因此不知道要创建多少行,因此不会调用你的getView() ]。可能有一种方法可以进一步扩展和修改ArrayAdapter以自行设置数据,但此时您实际上正在使用BaseAdapter。

[此外,假设您的xml格式正确且完美]