如何将图标添加到片段中的列表视图

时间:2013-12-11 14:57:37

标签: android android-listview android-adapter

我在片段中制作了一个列表视图。现在我需要在listview项目中添加一个简单的图标。每个项目上的图标必须相同。这将是一个简单的箭头(imageview)。 这可能与我制作的列表视图有关吗?如果是的话,我该怎么做?

所以喜欢这种格式: 我的文字 - 空格 - 空间 - >


片段的代码:

  public class BiblioFragment extends Fragment {

final String[] items = new String[] { "Astma en alcohol", "Astma en huisdieren", "Astma en lichaamsgewicht",
        "Astma en ouder worden", "Astmamedicatie", "Bekende mensen met astma", "Longfunctieonderzoek", "Reizen en vakantie",
   "Sociaal leven", "Weetjes over astma", "Enzovoort", "Enzovoort", "Enzovoort" };

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_biblio, container, false);

    ListView list = (ListView)view.findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Log.v("TAG", "CLICKED row number: " + arg2);

            Intent myIntent = new Intent(getActivity(), BiblioDetail.class);
            myIntent.putExtra("welkerij", arg2);
            startActivity(myIntent); 

        }

    });

    return view;

   }
  }

我的那个片段的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" 
  android:background="#ffffff">

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

创建 ListAdapter

public class ListAdapter extends ArrayAdapter<Item> {

public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    // TODO Auto-generated constructor stub
}

private List<Item> items;

public ListAdapter(Context context, int resource, List<Item> items) {

    super(context, resource, items);

    this.items = items;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {

        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.itemlistrow, null);

    }

    Item p = items.get(position);

    if (p != null) {


        ImageView ICon = ( ImageView) v.findViewById(R.id.description);




        }
    }

    return v;

}

活动

ListView yourListView = (ListView) findViewById(R.id.itemListView);

// get data from the table by the ListAdapter
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, List<yourItem>);

yourListView .setAdapter(customAdapter);

答案 1 :(得分:0)

onCreateView

ListView list = (ListView)view.findViewById(R.id.listView1);
CustomAdapter cus = new CustomAdapter(getActivity(),items);   
list.setAdapter(cus);

使用自定义适配器

public class CustomAdapter extends BaseAdapter {

    String items[];
    LayoutInflater mInflater;

    public CustomAdapter(Context context, String[] items) {
        mInflater = LayoutInflater.from(context);
        this.items = items;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView ==null)
        {
            convertView = mInflater.inflate(R.layout.list_item,parent,false);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.textView1);
            holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag(); 
        }
        holder.tv.setText(items[position]) 
            // use holder.iv to set whatever image you want according to the position
        return convertView;
    }

    static class ViewHolder
    { 
        ImageView iv;
        TextView tv;
    }
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="32dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginLeft="64dp"
        android:layout_marginTop="14dp"
        android:layout_toRightOf="@+id/imageView1"
        android:text="TextView" />

</RelativeLayout>

对齐

enter image description here

编辑:

如果您认为这是过度杀戮,您可以使用复合文本视图,左侧是图像,右侧是文本。

在上面我使用了list_item.xml这是一个自定义布局。我在自定义适配器的getView中扩充了该布局。我将文本设置为textview。我已将默认启动器图标设置为imageview。你可以改变它。

我还使用了ViewHolder

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

同时检查

How ListView's recycling mechanism works