带有多个xml文件的android listview

时间:2014-07-16 22:01:33

标签: android xml listview android-listview

我想在其中创建一个包含多个视图的列表视图,例如google。 这是我的ArrayAdapter:

public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final List<String> city;
private final List<Integer> image;
private final List<String> temp;
private final List<String> humidity;
private final List<String> windspeed;
private final List<String> condition;


public CustomList(Activity context,
                  List<String> city,
                  List<Integer> image,
                  List<String> temp,
                  List<String>humidity,
                  List<String>windspeed,
                  List<String>condition) {
    super(context, R.layout.weather_item, city);
    this.context = context;
    this.city = city;
    this.temp = temp;
    this.humidity = humidity;
    this.image = image;
    this.windspeed = windspeed;
    this.condition = condition;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.weather_item, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.city);
    TextView txtTemp = (TextView) rowView.findViewById(R.id.info_one);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.info_image);
    TextView txthumidity = (TextView) rowView.findViewById(R.id.humidity);
    TextView txtWind = (TextView) rowView.findViewById(R.id.wind_speed);
    TextView txtCond = (TextView) rowView.findViewById(R.id.info_two);
    txtCond.setText(condition.get(position));
    txtWind.setText(windspeed.get(position));
    txthumidity.setText(humidity.get(position));
    txtTitle.setText(city.get(position));
    txtTemp.setText(String.valueOf(temp.get(position))+"°");
    imageView.setImageResource(image.get(position));
    return rowView;
}

}

它目前会在布局文件夹中膨胀一个名为“weather_item”的xml文件。 我想创建一个包含两个xml文件的listview,这样它将显示weather_item和我的其他布局,所以它看起来像这样 example of listview with several xml views in it

有可能吗?

3 个答案:

答案 0 :(得分:1)

是的,只需覆盖自定义适配器类中的两个方法,并修改getview以处理这两种类型的布局。

private static final int DATA_TYPE_WEATHER = 0;
private static final int DATA_TYPE_OTHER = 1;   
@Override
    public int getViewTypeCount() {
        // Return the total number of xml layouts you'll be using
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        // You have the list position, look up the data in your array and
        // return the proper number for the data type
        if (/* test for weather data type */) {
            return DATA_TYPE_WEATHER;
        } else {
            return DATA_TYPE_OTHER;
        }
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // always check and re-use the convertView if it's not null!
        if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            if (/* test for weather data type */) {
                convertView = inflater.inflate(R.layout.weather_item, null);
            } else {
                convertView = inflater.inflate(R.layout.other_layout, null);
            }
        }
        if (/* test for weather data type */) {
            // TODO setup the fields for weather layout
        } else {
            // TODO setup the fields for other layout
        }
    }

答案 1 :(得分:0)

  

您可能会尝试将这些链接视为起点:

http://www.androidhub4you.com/2013/09/dynamic-list-view-demo-in-android.html http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/

  

此外,您可能希望查看本文以获得更好的效果   理解这个概念:

http://www.developerfusion.com/article/145373/android-listviews-with-dynamic-data/

  

如果您有理解问题,请添加一个起点   关于基本的自定义列表视图:

这也是一个自定义列表视图教程,您可以在深入查看自定义列表视图(如Google +或Facebook)之前查看。

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92

答案 2 :(得分:-1)

您需要将所有布局放入垂直LinearLayout。