如何在LazyList布局上添加标题?

时间:2011-12-21 18:00:57

标签: android listview lazylist

我正在尝试使用最初由Fedor创建的LazyList(Lazy load of images in ListView)每行添加标题

下面的图片是我想要完成的。请有人帮助我,我一直在努力解决这个问题,我会非常感激。提前谢谢!

enter image description here

这是LazyAdapter.java

package com.fedorvlasov.lazylist;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, String[] d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

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

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item, null);

        TextView text=(TextView)vi.findViewById(R.id.text);;
        ImageView image=(ImageView)vi.findViewById(R.id.image);
        text.setText("item "+position);
        imageLoader.DisplayImage(data[position], image);
        return vi;
    }
}

这是item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <ImageView
      android:id="@+id/image"
      android:layout_width="50dip"
      android:layout_height="50dip" android:src="@drawable/stub" android:scaleType="centerCrop"/>
  <TextView
      android:id="@+id/text"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" android:layout_gravity="left|center_vertical" android:textSize="20dip" android:layout_marginLeft="10dip"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

    data = new ArrayList<Item>(10);
    data.add(new Item(100, "Sample Food1"));
    data.add(new Item(110, "Sample Food2"));
    data.add(new Item(120, "Sample Food3"));
    data.add(new Item(130, "Sample Food4"));
    data.add(new Item(140, "Sample Food5"));
    data.add(new Item(150, "Sample Food6"));
    data.add(new Item(160, "Sample Food7"));
    data.add(new Item(170, "Sample Food8"));
    data.add(new Item(180, "Sample Food9"));
    data.add(new Item(190, "Sample Food10"));
    data.add(new Item(200, "Sample Food11"));
    data.add(new Item(210, "Sample Food12"));
    adapter = new ItemListAdapter(this, data);
    setListAdapter(adapter);

如果是ItemListAdapter,请使用此

public class ItemListAdapter extends ArrayAdapter<Item>
{
    private LayoutInflater li;
    public ItemListAdapter(Context context, List<Item> items)
    {
        super(context, 0, items);
        li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final Item item = getItem(position);
        View v = convertView;
        if (v == null)
        {
            v = li.inflate(R.layout.fooditem, null);
        }
        final TextView idView = (TextView) v.findViewById(R.id.itemId);
        if (idView != null)
        {
            idView.setText("" + item.getId());
        }

        final TextView captionView = (TextView) v.findViewById(R.id.itemCaption);
        if (captionView != null)
        {
            captionView.setText(item.getCaption());
        }
        return v;
    }
    @Override
    public long getItemId(int position)
    {
        return getItem(position).getId();
    }
    @Override
    public boolean hasStableIds()
    {
        return true;
    }
}