与Android的形象自定义布局的ListView [英] Android custom layout listview with images

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

问题描述

我想设法获得动态的项目列表视图。
我建立了一个包含一个ImageView的row_layout和2 TextViews。
当我建立适配器,我希望能够以显示ImageView的或不依赖于行号。
所以,我想有第2行IMG1,IMG2 3排,然后在5行和所有其他行,我不希望任何ImageView的显示(还有textviews的对齐方式必须改变填补留下的空白IMG3 ImageView的丢失)。

这可能吗?

我在这里填充我适配器:

 的JSONObject json_data;
尝试{
   INT长度= lststat.length();
   detalii_client =新客户端[长度]
   的for(int i = 0; I<长度;我++){
     json_data = lststat.getJSONObject(ⅰ);
     detalii_client [I] =新客户();
     字符串categoria = json_data.getString(巢穴);
     如果(1.equals(categoria)){// 1行
        detalii_client [I] .desc =一些标题;
        detalii_client [I] .icon = 0; //这里我想没有图像
     }
     如果(2.equals(categoria)){//第2行
        detalii_client [I] .desc =一些其他的标题;
        detalii_client [I] .icon = 0; //这里我要的图片分配给ImageView的
     }
....

我有一个类:

 公共类客户{
    公众诠释图标;
            公共字符串说明;
            公共字符串称号;
            公共字符串ID;
            公共客户端(){
                超();
            }            公共客户端(INT图标,desc字符串,字符串title,字符串ID){
                超();
                this.icon =图标;
                this.desc =说明;
                this.id = ID;
                this.title =称号;
            }
    }

和一个自定义的Adapter类

 公共类ClientAdapter扩展ArrayAdapter<客户> {    上下文语境;
    INT layoutResourceId;
    客户端数据[] = NULL;    公共ClientAdapter(上下文的背景下,INT layoutResourceId,客户端[]数据){
        超级(上下文,layoutResourceId,数据);
        this.layoutResourceId = layoutResourceId;
        this.context =背景;
        this.data =数据;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看排= convertView;
        ClientHolder支架=无效;        如果(行== NULL)
        {
            LayoutInflater充气=((活动)上下文).getLayoutInflater();
            行= inflater.inflate(layoutResourceId,父母,假);            持有人=新ClientHolder();
            holder.hldIcon =(ImageView的)row.findViewById(R.id.imgIconx);
            holder.hldTitle =(TextView中)row.findViewById(R.id.txtTitlu);
            holder.hldDesc =(TextView中)row.findViewById(R.id.txtDescr);
            holder.hldId =(TextView中)row.findViewById(R.id.txtId);
            row.setTag(保持器);
        }
        其他
        {
            支架=(ClientHolder)row.getTag();
        }        客户clientul =数据[位置]
        holder.hldTitle.setText(clientul.title);
        holder.hldDesc.setText(clientul.desc);
        holder.hldId.setText(clientul.id);
        holder.hldIcon.setImageResource(clientul.icon);        返回行;
    }    静态类ClientHolder
    {
        ImageView的hldIcon;
        TextView的hldTitle;
        TextView的hldId;
        TextView的hldDesc;
    }
}

另外我有一个活动的布局(无关)一ListView和底部按钮(RelativeLayout的)。
我有,我用它来显示行的row_layout。

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>        < ImageView的
            机器人:ID =@ + ID / imgIconx
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:SRC =@绘制/ client64/>        <的LinearLayout
            机器人:layout_width =145dp
            机器人:layout_height =match_parent
            机器人:layout_weight =1.50
            机器人:方向=垂直>            <的TextView
                机器人:ID =@ + ID / txtDescr
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginTop =10dp
                机器人:文字=@字符串/ lbl_loc
                机器人:文字颜色=@彩色/ darkred
                机器人:TEXTSIZE =@扪/ descriere_detaliu
                机器人:字体=衬/>            <的TextView
                机器人:ID =@ + ID / txtTitlu
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文字=@字符串/ lbl_client
                机器人:textAppearance =机器人:ATTR / textAppearanceLarge
                机器人:文字颜色=@彩色/ darkred
                机器人:TEXTSIZE =18sp
                机器人:TEXTSTYLE =正常
                机器人:字体=衬/>            <的TextView
                机器人:ID =@ + ID / txtId
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文字=@字符串/ lbl_idviz
                机器人:textAppearance =机器人:ATTR / textAppearanceSmall
                机器人:文字颜色=@色/白/>
        < / LinearLayout中>< / RelativeLayout的>


  • 我在哪里可以指定我的照片我的ImageView?

  • 我怎样才能让ImageView的消失了某些行?

感谢您


解决方案

  

我在哪里可以指定我的照片我的ImageView?


做到这一点你的适配器getView()。它看起来像你现在这样做。


  

和我怎样才能让ImageView的消失了某些行?


使用的如果语句和如果是不正确的行(位置)调用

  holder.hldIcon.setVisibility(View.INVISIBLE)`

  holder.hldIcon.setVisibility(View.GONE)

这取决于你想要什么

I would like to manage to obtain a listview with dynamic items. I built a row_layout containing an ImageView, and 2 TextViews. When I build the adapter, I want to be able to display the ImageView OR NOT depending on the row number. So I want to have img1 on row 2, img2 on row 3, then img3 on row 5. And on all other rows I do not want any imageView displayed (and also the alignment of the textviews must change filling the empty space left by the ImageView missing).

Is that possible?

I populate my adapter here:

JSONObject json_data;
try {
   int length = lststat.length();
   detalii_client = new Client[length];
   for (int i = 0; i < length; i++) {
     json_data = lststat.getJSONObject(i);
     detalii_client[i] = new Client();
     String categoria = json_data.getString("den");
     if ("1".equals(categoria)){ // row 1
        detalii_client[i].desc ="Some Title";
        detalii_client[i].icon = 0; // here I want NO IMAGE
     }
     if ("2".equals(categoria)){ // row 2
        detalii_client[i].desc ="Some other Title";
        detalii_client[i].icon = 0; // here I want to assign a picture to the imageView
     }
....

I have a class:

public class Client {
    public int icon;
            public String desc;
            public String title;
            public String id;
            public Client(){
                super();
            }

            public Client(int icon, String desc, String title, String id) {
                super();
                this.icon = icon;
                this.desc = desc;
                this.id = id;
                this.title= title;
            }
    }

and a custom Adapter class

public class ClientAdapter extends ArrayAdapter<Client>{

    Context context;
    int layoutResourceId;   
    Client data[] = null;

    public ClientAdapter(Context context, int layoutResourceId, Client[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ClientHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ClientHolder();
            holder.hldIcon = (ImageView)row.findViewById(R.id.imgIconx);
            holder.hldTitle = (TextView)row.findViewById(R.id.txtTitlu);
            holder.hldDesc = (TextView)row.findViewById(R.id.txtDescr);
            holder.hldId = (TextView)row.findViewById(R.id.txtId);
            row.setTag(holder);
        }
        else
        {
            holder = (ClientHolder)row.getTag();
        }

        Client clientul = data[position];
        holder.hldTitle.setText(clientul.title);
        holder.hldDesc.setText(clientul.desc);
        holder.hldId.setText(clientul.id);
        holder.hldIcon.setImageResource(clientul.icon);

        return row;
    }

    static class ClientHolder
    {
        ImageView hldIcon;
        TextView hldTitle;
        TextView hldId;
        TextView hldDesc;
    }    
}   

Also I have an activity layout (irrelevant) One listview and a bottom button (relativeLayout). And I have a row_layout which I use to display the rows.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imgIconx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/client64" />

        <LinearLayout
            android:layout_width="145dp"
            android:layout_height="match_parent"
            android:layout_weight="1.50"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txtDescr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="@string/lbl_loc"
                android:textColor="@color/darkred"
                android:textSize="@dimen/descriere_detaliu"
                android:typeface="serif" />

            <TextView
                android:id="@+id/txtTitlu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lbl_client"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/darkred"
                android:textSize="18sp"
                android:textStyle="normal"
                android:typeface="serif" />

            <TextView
                android:id="@+id/txtId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lbl_idviz"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/white" />
        </LinearLayout>

</RelativeLayout>

  • Where can I assign my pictures to my ImageView?
  • And How can I make the ImageView "disappear" for certain rows?

Thank you

解决方案

Where can I assign my pictures to my ImageView?

Do this in the getView() method of your Adapter. It looks like you are doing that now.

And How can I make the ImageView "disappear" for certain rows?

Use anif statement and if it isn't the correct row (position) call

holder.hldIcon.setVisibility(View.INVISIBLE)`

or

holder.hldIcon.setVisibility(View.GONE)

depending on what you want

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

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