gpt4 book ai didi

带有自定义适配器的 Android DrawerLayout 和 Listview

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:42 25 4
gpt4 key购买 nike

我正在创建一个带有相对布局和内部 ListView 的抽屉布局(一个用于主要内容,一个用于导航)

对于 ListView ,我创建了一个自定义适配器,并使用一个包含 ImageView 和 TextView 的 list_item xml 文件创建每一行。

该应用程序运行,但当我打开抽屉时,我只看到没有 ListView 的背景。现在,如果我尝试使用 ArrayAdapter(默认),则会显示 ListView 。

有什么建议吗?

我的自定义适配器

public class CustomAdapter extends ArrayAdapter<Categories>{

Context context;
int layoutResourceId;
Categories[] data = null;

public CustomAdapter(Context context, int layoutResourceId, Categories[] categs) {
super(context, layoutResourceId);
this.layoutResourceId = layoutResourceId;
this.context = context;
data = categs;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
PHolder holder = null;

if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

holder = new PHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.categimage);
holder.txtTitle = (TextView)row.findViewById(R.id.categtext);

row.setTag(holder);
}
else
{
holder = (PHolder)row.getTag();
}

Categories categ = data[position];
holder.txtTitle.setText(categ.title);
holder.imgIcon.setImageResource(categ.icon);

return row;
}

static class PHolder
{
ImageView imgIcon;
TextView txtTitle;
}

在我的主要 Activity 中

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

Categories data[] = new Categories[]
{
new Categories(R.drawable.restaurant, R.string.food),
new Categories(R.drawable.bar_coktail, R.string.bar),
new Categories(R.drawable.mall, R.string.shop),
new Categories(R.drawable.agritourism, R.string.out),
new Categories(R.drawable.dance_class, R.string.art),
new Categories(R.drawable.officebuilding, R.string.other),
new Categories(R.drawable.university, R.string.education),
new Categories(R.drawable.townhouse, R.string.house),
new Categories(R.drawable.junction, R.string.transport)
};
CustomAdapter ca = new CustomAdapter(this, R.layout.list_item, data);

View header = (View)getLayoutInflater().inflate(R.layout.list_header, null);
mDrawerList.addHeaderView(header);

mDrawerList.setAdapter(ca);

最佳答案

要么使用:

super(context, layoutResourceId, categs);

在构造函数中或覆盖 getCount() 方法:

@Override
public int getCount() {
return data.length;
}

关于带有自定义适配器的 Android DrawerLayout 和 Listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17876972/

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