【问题标题】:Custom adapter for navigation drawer导航抽屉的自定义适配器
【发布时间】:2014-11-22 21:27:29
【问题描述】:

我有一个使用ArrayAdapter 的导航抽屉,如documentation 所示。我想为每个导航抽屉项目设置ImageView 图标,所以我需要创建一个自定义适配器来使用,但我不确定如何做到这一点。我在下面运行的内容不会导致应用程序崩溃,但导航抽屉打开时是空的,里面什么都没有。你能帮帮我吗?提前致谢!

到目前为止,我为自定义适配器所做的工作:

public class CustomAdapter extends ArrayAdapter
{
    private final Context context;

    public CustomAdapter(Context context)
    {
       super(context, R.layout.drawer_list_item);
       this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View v = inflater.inflate(R.layout.drawer_list_item, parent, false);
        TextView textViewHome = (TextView) v.findViewById(R.id.drawerHomeTextView);
        ImageView imageViewHome = (ImageView) v.findViewById(R.id.drawerHomeImage);
        TextView textViewList = (TextView) v.findViewById(R.id.drawerListTextView);
        ImageView imageViewList = (ImageView) v.findViewById(R.id.drawerListImage);

        return v;
    }
}

在MainActivity中设置抽屉:

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

mDrawerList.setAdapter(new CustomAdapter(this));

使用的ListView:

<ListView android:id="@+id/leftDrawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

drawer_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <RelativeLayout
        android:id="@+id/drawerRelativeLayoutHome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/drawerHomeImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:paddingStart="4dp"
            android:paddingEnd="4dp"
            android:paddingLeft="4dp"
            android:src="@drawable/ic_menu_home"/>

        <TextView
            android:id="@+id/drawerHomeTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/drawerHomeImage"
            android:layout_toEndOf="@+id/drawerHomeImage"
            android:background="@drawable/background_activated"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:paddingTop="12dp"
            android:paddingBottom="12dp"
            android:text="Home"
            android:textSize="20sp"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/drawerRelativeLayoutList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/drawerRelativeLayoutHome">

        <!-- same as above, but ic_menu_archive as drawable, and "List" as text -->

    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

    标签: android android-arrayadapter navigation-drawer custom-adapter


    【解决方案1】:

    对于您的自定义适配器:

    public class NavDrawerAdapter extends ArrayAdapter<NavDrawerItem>
    {
        private final Context context;
        private final int layoutResourceId;
        private NavDrawerItem data[] = null;
    
        public NavDrawerAdapter(Context context, int layoutResourceId, NavDrawerItem [] data)
        {
            super(context, layoutResourceId, data);
            this.context = context;
            this.layoutResourceId = layoutResourceId;
            this.data = data;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    
            View v = inflater.inflate(layoutResourceId, parent, false);
    
            ImageView imageView = (ImageView) v.findViewById(R.id.navDrawerImageView);
            TextView textView = (TextView) v.findViewById(R.id.navDrawerTextView);
    
            NavDrawerItem choice = data[position];
    
            imageView.setImageResource(choice.icon);
            textView.setText(choice.name);
    
            return v;
        }
    }
    

    对于NavDrawerItem:

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

    对于drawer_list_item.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="?android:attr/activatedBackgroundIndicator"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:padding ="10dp">
    
        <ImageView
            android:id="@+id/navDrawerImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:paddingRight="10dp"/>
    
        <TextView
           android:id="@+id/navDrawerTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/navDrawerImageView"
            android:paddingRight="10dp"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"/>
    
    </RelativeLayout>
    

    在MainActivity.java 中,实例化NavDrawerItem 对象的数组,并为每个对象使用适当的drawable 和名称,然后在设置适配器时传递此数组,如下所示:

    mDrawerList.setAdapter(new YourAdapter(this, R.layout.drawer_list_item, yourArray));

    【讨论】:

      【解决方案2】:

      更新
      1) 我没有看到要在您的代码中显示的数据源 - 您没有在构造函数或任何其他方式中传递它
      2) 在getView() 中,您应该为您的视图设置值
      3) 为了提高效率,如果不是null,您必须重新使用convertView
      4) 最后 - 为您的 CustomAdapter 覆盖以下方法:

          @Override
          public int getViewTypeCount() {
         ...
          }
      
      
          @Override
          public int getItemViewType(int position) {
         ...
          }
      
          @Override
          public int getCount() {
          ...
          }
      
          @Override
          public YOUR_ITEM_TYPE getItem(int position) {
          ...
          }
      
          @Override
          public long getItemId(int position) {
          ...
          }
      
          @Override
          public boolean hasStableIds() {
          ...
          }
      
          @Override
          public boolean isEmpty() {
          ...
          }
      
          @Override
          public boolean areAllItemsEnabled() {
          ...
          }
      
          @Override
          public boolean isEnabled(int position) {
          ...
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        相关资源
        最近更新 更多