zoukankan      html  css  js  c++  java
  • Android新手之旅(8) ListView的使用

      希望使用ListView来展示信息,每行一个图标,右侧是文字,分为两行布局。经过尝试,这样可以实现:

    1、Layout下新建item.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout  
             android:layout_width="fill_parent"  
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_height="wrap_content"  
             android:paddingBottom="2dip"  
             android:paddingLeft="12dip"> 
             <ImageView  
                   android:layout_width="wrap_content"  
                   android:id="@+id/itemImage" android:layout_height="fill_parent"
                   android:paddingTop="2dip" >  
             </ImageView> 
             <TextView  
                   android:text="TextView01"  
                   android:layout_height="wrap_content"  
                   android:layout_width="fill_parent"  
                   android:id="@+id/itemTitle" android:layout_toRightOf="@+id/itemImage" android:textSize="16dip"
                   android:textColor="#000"> 
             </TextView> 
             <TextView  
                   android:text="TextView02"  
                   android:layout_height="wrap_content"  
                   android:layout_width="fill_parent"  
                   android:id="@+id/itemText" android:layout_toRightOf="@+id/itemImage" android:layout_below="@+id/itemTitle"
                   android:textSize="10dip"
                   android:textColor="#000"> 
             </TextView> 
    </RelativeLayout>

    2、主页面中Listview的样式

    <ListView android:layout_marginTop="20px"  android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/MyListView"></ListView>

    3、通过以下核心函数应用样式

    private View makeItemView(String strTitle, String strText, int resId) {
        LayoutInflater inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // 使用View的对象itemView与R.layout.item关联
        View itemView = inflater.inflate(R.layout.item, null);

        // 通过findViewById()方法实例R.layout.item内各组件
        TextView title = (TextView) itemView.findViewById(R.id.itemTitle);
        title.setText(strTitle);
        TextView text = (TextView) itemView.findViewById(R.id.itemText);
        text.setText(strText);
        ImageView image = (ImageView) itemView.findViewById(R.id.itemImage);
        image.setImageResource(resId);
        return itemView;
    }

    4、主程序中的使用,把一系列数组传递进去

    listView=(ListView)this.findViewById(R.id.MyListView);
    listView.setAdapter(new ListViewAdapterImageText(this, titles,texts,resIds));

    5、效果

    image

    6、ListView的单击处理

    import android.widget.AdapterView.OnItemClickListener;
    import android.view.View; 

            //添加点击  
            listView.setOnItemClickListener(new OnItemClickListener() {  
                @Override 
                public void onItemClick(AdapterView<?> arg0,View arg1, int arg2,  
                        long arg3) {  
                    setTitle("点击第"+arg2+"个项目");  
                }  
            });

    参考:Android ListView常用用法 

    参考:Android入门第七篇之ListView (二)

  • 相关阅读:
    solr和mongodb比较
    IKAnalyzer原理分析
    maven项目java包名的路径问题
    solr的EmbeddedSolrServer原理深入探讨
    团队冲刺第二阶段06
    构建之法读书笔记06——敏捷流程
    团队冲刺第二阶段05
    团队冲刺第二阶段04
    团队冲刺第二阶段03
    团队冲刺第二阶段02
  • 原文地址:https://www.cnblogs.com/jetz/p/2115228.html
Copyright © 2011-2022 走看看