gpt4 book ai didi

android - NavigationDrawer,带有图像的适配器

转载 作者:行者123 更新时间:2023-11-29 14:47:51 26 4
gpt4 key购买 nike

我有一个带有此代码的抽屉导航:

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>

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

最佳答案

你应该像下面的代码一样为你的图像Arraylist创建Custom Adapter,你的Custom Adapter像这样创建

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

对于 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));

关于android - NavigationDrawer,带有图像的适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29363640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com