列表视图的 Android 自定义布局 [英] Android Custom Layout for listview

查看:51
本文介绍了列表视图的 Android 自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的列表视图如下

String[] Shows = new String[] { "Dexter", "Breaking Bad", "The Big Bang Theory", "Leverage"};
ListView tv_show_list = (ListView) view_tvshowAddNew.findViewById(R.id.lv_tv_show_list);

ArrayAdapter<String> showNameAdapter = new ArrayAdapter<String>(getActivity(), R.layout.tv_show_each_show_name, Shows);

tv_show_list.setAdapter(showNameAdapter);

现在我的 tv_show_each_show_name XML 如下所示

Now my tv_show_each_show_name XML is like below

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="58dp" 
    android:background="@drawable/ic_launcher" 

    > 
</TextView>

我现在想做的是让每个节目名称与图片相关联,然后将其放入地图中,例如:-

What I want to do now is make each show name associated with a picture and maybe put that in a map, like :-

Map mMap = new HashMap();
mMap.put("Dexter", "imageDexter.png");
mMap.put("Breaking Bad", "imageDexter.png");

在加载列表视图时,我将显示该地图的名称并设置关联的图像背景,从该地图获取图像名称并从可绘制文件夹中加载它.

and while loading the list view i will show the name from this map and set the associated image background taking the image name from this map and loading it from the drawable folder.

如何做到这一点?

像布局一样跳跃

推荐答案

这是一个教程,适用于您的自定义列表视图

Here is a tutorial for your custom Listview

现在为每个项目设置不同的图像,在适配器的 getView() 方法中

Now for setting different images with respect to each item ,inside your getView() method of adapter

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
    // Change the icon for Windows and iPhone
    String s = values[position];
    imageView.setImageResource(images[position]);
    return rowView;
  }

您的图像数组将像这样

 int[] images={R.drawable.green_button,R.drawable.ic_launcher,R.drawable.shape,R.drawable.splash};

还要确保图像"数组的长度必须与列表中的项目相同.

Also make sure your the length of your 'images' array must be same as your items in list.

这篇关于列表视图的 Android 自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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