自定义适配器抽屉式导航 [英] Custom adapter for navigation drawer

查看:128
本文介绍了自定义适配器抽屉式导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用一个工作抽屉式导航 ArrayAdapter 如图所示的文档。我想设置的ImageView 图标,每个抽屉式导航项,所以我需要创建一个自定义适配器使用,但我不确定如何做到这一点。我下面没有崩溃的应用程序运行,但导航抽屉打开空的,什么也没有在里面。你能帮帮我吗?在此先感谢!

我已经为一个自定义的适配器,这样做的远:

 公共类CustomAdapter扩展ArrayAdapter
{
    私人最终上下文的背景下;

    公共CustomAdapter(上下文的背景下)
    {
       超(背景下,R.layout.drawer_list_item);
       this.context =背景;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        视图V = inflater.inflate(R.layout.drawer_list_item,父母,假);
        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);

        返回伏;
    }
}
 

设置抽屉中 MainActivity

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

mDrawerList.setAdapter(新CustomAdapter(本));
 

的ListView 使用:

 < ListView的机器人:ID =@ + ID / leftDrawer
    机器人:layout_width =240dp
    机器人:layout_height =match_parent
    机器人:layout_gravity =左
    机器人:choiceMode =singleChoice
    机器人:分隔=@机器人:彩色/透明
    机器人:dividerHeight =0dp
    机器人:后台=#111/>
 

drawer_list_item.xml

 < XML版本=1.0编码=UTF-8&GT?;

< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =match_parent>

    < RelativeLayout的
        机器人:ID =@ + ID / drawerRelativeLayoutHome
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT>

        < ImageView的
            机器人:ID =@ + ID / drawerHomeImage
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_alignParentStart =真
            机器人:layout_centerVertical =真
            机器人:paddingStart =4DP
            机器人:paddingEnd =4DP
            机器人:以下属性来=4DP
            机器人:SRC =@可绘制/ ic_menu_home/>

        <的TextView
            机器人:ID =@ + ID / drawerHomeTextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_toRightOf =@ + ID / drawerHomeImage
            机器人:layout_toEndOf =@ + ID / drawerHomeImage
            机器人:背景=@可绘制/ background_activated
            机器人:以下属性来=4DP
            机器人:paddingRight =4DP
            机器人:paddingTop =12dp
            机器人:paddingBottom会=12dp
            机器人:文本=家
            机器人:TEXTSIZE =20SP/>
    < / RelativeLayout的>

    < RelativeLayout的
        机器人:ID =@ + ID / drawerRelativeLayoutList
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / drawerRelativeLayoutHome>

        &所述;! - 上述相同,但ic_menu_archive作为可拉伸,和列表作为文本 - >

    < / RelativeLayout的>

< / RelativeLayout的>
 

解决方案

有关您的自定义适配器:

 公共类NavDrawerAdapter扩展ArrayAdapter< NavDrawerItem>
{
    私人最终上下文的背景下;
    私人最终诠释layoutResourceId;
    私人NavDrawerItem数据[] = NULL;

    公共NavDrawerAdapter(上下文的背景下,INT layoutResourceId,NavDrawerItem []数据)
    {
        超级(上下文,layoutResourceId,数据);
        this.context =背景;
        this.layoutResourceId = layoutResourceId;
        this.data =数据;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        LayoutInflater充气=((活动)上下文).getLayoutInflater();

        视图V = inflater.inflate(layoutResourceId,父母,假);

        ImageView的ImageView的=(ImageView的)v.findViewById(R.id.navDrawerImageView);
        TextView中的TextView =(TextView中)v.findViewById(R.id.navDrawerTextView);

        NavDrawerItem选择=数据[位置]

        imageView.setImageResource(choice.icon);
        textView.setText(choice.name);

        返回伏;
    }
}
 

有关 NavDrawerItem

 公共类NavDrawerItem
{
    公众诠释图标;
    公共字符串名称;

    公共NavDrawerItem(INT图标,字符串名称)
    {
        this.icon =图标;
        this.name =名称;
    }
}
 

有关 drawer_list_item.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =match_parent
    机器人:背景=机器人:ATTR / activatedBackgroundIndicator
    机器人:=了minHeight机器人:ATTR /列表preferredItemHeightSmall
    机器人:填充=10dp>

    < ImageView的
        机器人:ID =@ + ID / navDrawerImageView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentStart =真
        机器人:layout_alignParentTop =真
        机器人:paddingRight =10dp/>

    <的TextView
       机器人:ID =@ + ID / navDrawerTextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerVertical =真
        机器人:layout_toRightOf =@ + ID / navDrawerImageView
        机器人:paddingRight =10dp
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceListItemSmall?

< / RelativeLayout的>
 

MainActivity.java ,实例化 NavDrawerItem 对象的数组,用适当的绘制并为每个名称,然后当你设置适配器,像这样通过这个数组:

mDrawerList.setAdapter(新YourAdapter(这一点,R.layout.drawer_list_item,yourArray));

I have a working navigation drawer that uses ArrayAdapter as shown in the documentation. I want to set ImageView icons for each of the navigation drawer items, so I need to create a custom adapter to use, but I am unsure of how to do that. What I have below runs without crashing the app, but the navigation drawer opens up empty, with nothing in it. Can you please help me out? Thanks in advance!

What I've done for a custom adapter so far:

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;
    }
}

Setting up the drawer in MainActivity:

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

mDrawerList.setAdapter(new CustomAdapter(this));

The ListView used:

<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>

解决方案

For your custom adapter:

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;
    }
}

For NavDrawerItem:

public class NavDrawerItem
{
    public int icon;
    public String name;

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

For 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>

In MainActivity.java, instantiate an array of NavDrawerItem objects, with the appropriate drawable and name for each, and then pass this array when you set the adapter, like so:

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

这篇关于自定义适配器抽屉式导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆