如何将图标片段添加到列表视图 [英] How to add an icon to a listview in a fragment

查看:172
本文介绍了如何将图标片段添加到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个片段列表视图。现在我需要一个简单的图标添加到ListView项目。该图标必须对每一个项目相同。这将是一个简单的箭头(ImageView的)。
这可能与我所做的列表视图?如果是,我怎么办呢?

所以像这样的格式:
我这里的文字 - 空间 - 空间 - >


在code的片段:

 公共类BiblioFragment扩展片段{最终的String []项目=新的String [] {ASTMA恩醇,ASTMA恩huisdieren,ASTMA恩lichaamsgewicht
        恩ASTMA沃登ouder,Astmamedicatie,Bekende mensen满足ASTMA,Longfunctieonderzoe​​k,REIZEN恩vakantie
   利文Sociaal,过ASTMA Weetjes,Enzovoort,Enzovoort,Enzovoort};@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    查看查看= inflater.inflate(R.layout.fragment_biblio,集装箱,FALSE);    ListView控件列表=(ListView控件)view.findViewById(R.id.listView1);
    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(getActivity(),android.R.layout.simple_list_item_1,项目);
    list.setAdapter(适配器);    list.setOnItemClickListener(新OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
                长ARG3){
            // TODO自动生成方法存根
            Log.v(TAG,CLICKED行数:+ ARG2);            意图myIntent =新意图(getActivity(),BiblioDetail.class);
            myIntent.putExtra(welkerij,ARG2);
            startActivity(myIntent);        }    });    返回视图。   }
  }

我的XML该片段:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =match_parent
  机器人:layout_height =match_parent
  机器人:方向=垂直
  机器人:背景=#FFFFFF>< ListView控件
    机器人:ID =@ + ID / ListView1的
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真正的>
< /&的ListView GT;< / LinearLayout中>


解决方案

onCreateView

 的ListView列表=(ListView控件)view.findViewById(R.id.listView1);
CustomAdapter CUS =新CustomAdapter(getActivity(),项目);
list.setAdapter(CUS);

使用自定义适配器

 公共类CustomAdapter延伸BaseAdapter {    字符串项[];
    LayoutInflater mInflater;    公共CustomAdapter(上下文的背景下,字符串[]项){
        mInflater = LayoutInflater.from(上下文);
        this.items =物品;
    }    @覆盖
    公众诠释的getCount(){
        返回items.length;
    }    @覆盖
    公共对象的getItem(INT位置){
        返回的位置;
    }    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        ViewHolder持有人;        如果(convertView == NULL)
        {
            convertView = mInflater.inflate(R.layout.list_item,父母,假);
            持有人=新ViewHolder();
            holder.tv =(TextView中)convertView.findViewById(R.id.textView1);
            holder.iv =(ImageView的)convertView.findViewById(R.id.imageView1);
            convertView.setTag(保持器);
        }
        其他
        {
            支架=(ViewHolder)convertView.getTag();
        }
        holder.tv.setText(项目[位置])
            //使用holder.iv设置你根据位置想要的任何图像
        返回convertView;
    }    静态类ViewHolder
    {
        ImageView的IV;
        TextView的电视;
    }
}

list_item.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:layout_marginLeft =42dp
        机器人:layout_marginTop =32dp
        机器人:SRC =@绘制/ ic_launcher/>    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignTop =@ + ID / imageView1
        机器人:layout_marginLeft =64dp
        机器人:layout_marginTop =14dp
        机器人:layout_toRightOf =@ + ID / imageView1
        机器人:文字=TextView的/>< / RelativeLayout的>

对齐

编辑:

如果你觉得这是比杀了你可以在左边和文字的使用权复合的TextView与形象。

在上面我已经使用 list_item.xml 这是一个自定义布局。我抬高自定义适配器的 getView 的布局。我设置的TextView文本。我已经设置了默认的启动器图标来ImageView的。你可以尽管改变它。

我也用一个ViewHolder

http://developer.android.com/training/improving-布局/平滑scrolling.html

另外,请检查

ListView的回收机制如何工作

I made a listview in a fragment. Now I need to add a simple icon to the listview items. The icon must be the same on every item. This will be a simple arrow (imageview). Is this possible with the listview I made? And if yes, how do I do it?

So like this format: My text here --space--space-- >


The code for the fragment:

  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;

   }
  }

My XML for that fragment:

<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>

解决方案

In onCreateView

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

Use a custom adapter

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>

Snap

Edit:

If you feel this is over kill you can use a compound textview with image on the left and text on the right.

In the above I have used list_item.xml which is a custom layout. I inflate that layout in getView of custom adapter. I set the text to textview. I have set the default launcher icon to imageview. You can change it though.

I have also used a ViewHolder

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

Also check

How ListView's recycling mechanism works

这篇关于如何将图标片段添加到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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