导航抽屉,带图像的适配器

时间:2015-03-31 08:07:18

标签: android xml image adapter navigation-drawer

我有一个带有此代码的导航器用于填充:

mDrawerListView.setAdapter(new ArrayAdapter<String>(
            getActionBar().getThemedContext(),
            R.layout.item_menu,
            R.id.item1,
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
                    getString(R.string.title_section4),
                    getString(R.string.title_section5),
                    getString(R.string.title_section6),
            }));

我需要为每个项目添加一个图像,而不仅仅是一个字符串。 我已经有了我的项目xml(R.layout.item_menu):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="1"
android:layout_height="wrap_content">
<ImageView
    android:layout_width="0dp"
    android:layout_weight="0.2"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:id="@+id/image1"
    android:layout_centerVertical="true" />
<TextView
    android:id="@+id/item1"
    android:layout_width="0dp"
    android:layout_weight="0.8"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:padding="5dp"
    android:layout_toRightOf="@+id/image1"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />
</LinearLayout>

但我不知道如何传递图像。 任何帮助?感谢

4 个答案:

答案 0 :(得分:3)

您应该为您的图片Custom Adapter创建Arraylist,例如下面的代码,您的自定义适配器就像这样创建

 public class YourAdapter 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>

MainActivity.java中,实例化一个NavDrawerItem对象数组,每个对象都有相应的drawable和name,然后在设置适配器时传递此数组,如下所示:         mDrawerList.setAdapter(new YourAdapter(this, R.layout.drawer_list_item, yourArray));

答案 1 :(得分:1)

您首先要创建一个自定义对象,例如:

public class DrawerItem{
    String title;
    int image_id;

    public DrawerItem(String title, int image_id){
        this.title = title;
        this.image_id = image_id;
    }
}

然后你必须创建一个自定义适配器:

public class DrawerAdapter extends ArrayAdapter<DrawerItem>{
    //all adapter method
}

选中此链接以了解如何创建自定义适配器:http://www.javacodegeeks.com/2013/06/android-listview-custom-adapter-with-imageview.html

然后在您的活动中,您必须传递图像的ID和标题;

List d_list = new ArrayList<DrawerItem>();
d_list.add(new DrawerItem("my first item", R.drawable.myimageid));
d_list.add(new DrawerItem("my second item", R.drawable.mysecondimageid));

DrawerAdapter adapter = new DrawerAdapter(context, list);
mDrawerListView.setAdapter(adapter);

答案 2 :(得分:1)

您应该为ListView创建一个自定义适配器,如下所示: Implementing your own adapter

并将此适配器设置为ListView

答案 3 :(得分:1)

这是一个非常好的例子,解释了listview的自定义适配器。 http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

您可以使用它来创建自定义适配器并将其应用于 mDrawerListView