首页 > 其他分享 >android listView控件学习之一

android listView控件学习之一

时间:2023-07-31 22:03:38浏览次数:29  
标签:控件 layout text list convertView android listView holder


android listView控件学习之一
1.基本应用:

1>res/layout/main.xml
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
    <ListView 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/list_view"
     />
 </LinearLayout>
 2>listView 一般形式
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);               //ArrayAdapter为系统自带Adapter
                 Adapter myListAdapter  = new                         ArrayAdapter<String> (this,android.R.layout.simple_list_item_1, mStrings));
   
                 listView = (ListView) findViewById(R.id.list_view);
                }
 3.listView 设置成像checkboxes 多选方法.
 1>设置choice mode属性
 2>list用CHOICE_MODE_MULTIPLE mode.
 如下:
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);    setListAdapter(new ArrayAdapter<String>(this,
        
   listView.setItemsCanFocus(false);
     listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
   
  }
 4. listView 设置成像checkboxes 单选方法
 1>设置choice mode属性
 2>list用CHOICE_MODE_SINGLE
 public void onCreate(Bundle savedInstanceState) {
               setListAdapter(new ArrayAdapter<String>(this,
                 android.R.layout.simple_list_item_single_choice, GENRES));
        
               listView.setItemsCanFocus(false);
         listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
     }
 5. listView 自己定义Adapter 实现效果每行前面为图片后面紧跟文字.(imageView and textView)
 1>构造MyListAdapter类
 2>Adapter样式
 res/layout/list_item_icon_text.xml
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal" android:layout_width="fill_parent"
  android:layout_height="fill_parent"><ImageView android:id="@+id/icon" android:layout_width="48dip"
   android:layout_height="48dip" /><TextView android:id="@+id/text" android:layout_gravity="center_vertical"
   android:layout_width="0dip" android:layout_weight="1.0"
   android:layout_height="wrap_content" />
 </LinearLayout>
 3>引用方式
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main); myListAdapter = new MyListAdapter(this);
 listView = (ListView) findViewById(R.id.list_view);
   listView.setAdapter(myListAdapter);
  }
 4>如何将自定义Adapter样式与View联系在一起.
 mInflater = LayoutInflater.from(context);
 View convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
 5>重写getView()方法
 public View getView(int position, View convertView, ViewGroup parent) {
   // A ViewHolder keeps references to children views to avoid unneccessary
   // calls
   // to findViewById() on each row.
   ViewHolder holder; // When convertView is not null, we can reuse it directly, there is no
   // need
   // to reinflate it. We only inflate a new View when the convertView
   // supplied
   // by ListView is null.
   if (convertView == null) {
    convertView = mInflater.inflate(R.layout.list_item_icon_text, null);  // Creates a ViewHolder and store references to the two children
    // views
    // we want to bind data to.
    holder = new ViewHolder();
    holder.text = (TextView) convertView.findViewById(R.id.text);
    holder.icon = (ImageView) convertView.findViewById(R.id.icon);  convertView.setTag(holder);
   } else {
    // Get the ViewHolder back to get fast access to the TextView
    // and the ImageView.
    holder = (ViewHolder) convertView.getTag();
   } // position = position % DATA.length; //实现向下循环
   // Bind the data efficiently with the holder.
   holder.text.setText(DATA[position]);
   holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); return convertView;
  }static class ViewHolder {
   TextView text;
   ImageView icon;
  }
 }

标签:控件,layout,text,list,convertView,android,listView,holder
From: https://blog.51cto.com/u_3124497/6913767

相关文章

  • android程序调用另一个android应用…
    android程序调用另一个android应用程序请求:例子:    newComponentName("com.gwsoft.player.activity","com.gwsoft.player.activity.PlayerActivity");newIntent();newBundle();"resUrl",resurl);......
  • android画图-----shape的使用文档…
    在GradientDrawable1试图中终于把shape学会了,以前总是似懂非懂,现在终于把里面的东西搞清楚了,同时也挺佩服谷歌的用心,故意设置一些陷阱吧,不认真对待还真以为没有啥效果呢。setContentView(R.layout.shape_drawable_1)shape_drawable_1代码如下:<ScrollViewxmlns:android="htt......
  • 自定义Android组件之带图像的TextV…
    本文为新书《Android/OPhone开发完全讲义》的内容连载。《Android/OPhone开发完全讲义》一书将在近期出版,敬请关注。 Android系统支持的图像格式)的TextView组件。在编写代码之前,先看一下Android组件的配置代码。1.<TextViewandroid:id="@+id/textview1"android:layout_width......
  • Android permission 访问权限大全
    Androidpermission访问权限大全AndroidAndroidpermission0Commentsandroidmanifest.xml中声明相关权限请求,完整列表如下:android.permission.ACCESS_CHECKIN_PROPERTIES允许读写访问”properties”表在checkin数据库中,改值可以修改上传(Allowsread/writeaccess......
  • android学习之TransitionDrawable …
    Drawable的例子,体现出Drawable的强大功能。AndroidSDK中说明了Drawable主要的作用是:在XML中定义各种动画,然后把XML当作Drawable资源来读取,通过Drawable显示动画。下面举个使用TransitionDrawable的例子,创建一个Android工程,然后再这个工程的基础上修改,修改过程如下:1、去掉layout......
  • android 弹出对话框之 AlertDialog
    1.在测试时,如何实现一个提示可以使用Toast.makeText(this,"这是一个提示",Toast.LENGTH_SHORT).show();//从资源文件string.xml里面取提示信息Toast.makeText(this,getString(R.string.welcome),Toast.LENGTH_SHORT).show();这个提示会几秒钟后消失2.可以使用AlertD......
  • android 单元测试之 JUnit
    android里面做单元测试第一,JUnit。     实用范围:     东西,比如业务逻辑,数据封装,数值计算等等。并不能测试androidapi。第二,采用Instrumentation.Android单元测试的主入口是InstrumentationTestRunner。它相当于JUnit当中TestRunner的作用。你可以将Instrumentat......
  • Android开发有必要深耕Framework吗?
    引言关于Android市场,我们也不必探究。市场下滑是众所周知的,凉是不可能凉的,毕竟智能网络时代安卓占据人们生活很大一块。而我们在开发岗位,大家都会多少接触一些framework底层技术。那么framework技术到底有多重要呢?其实掌握AndroidFramework一直是一个不光要熟练而且必须要精通的......
  • Android Fragments 详细使用
    AndroidFragments详细使用 Fragments诞生初衷  自从Android3.0中引入fragments的概念,根据词海的翻译可以译为:碎片、片段。其上的是为了解决不同屏幕分辩率的动态和灵活UI设计。大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间来放更多的UI组件,而多出......
  • android隐式启动Activity的例子
    android隐式启动Activity的例子【原创】android2.2测试通过android隐匿启动Activity的例子,同样适用于Service,BroadcastReceiver<activityandroid:name=".MyActivityTwo"android:label="ThisMyActivityTwo"><!--这样进行匹配:Intentintent=newIntent(Intent.ACT......