zoukankan      html  css  js  c++  java
  • listview研究

    我们经常遇见这样的事情,在listview的item中包含有textview和checkBox。我们既想获取listitem的点击事件,又想获取listitem中textview的点击事件和listitem中checkBox的点击事件,那么有没有办法实现呢?答案是肯定的,我们只需重新创建listview的适配器继承BaseAdpter就可以了。另外如果有checkBox或者imageview在内的话就必须设置它聚焦为false。

    1 package com.test;
    2
    3
    4  import android.app.Activity;
    5  import android.os.Bundle;
    6  import android.view.View;
    7  import android.view.ViewGroup;
    8  import android.view.View.OnClickListener;
    9  import android.widget.AdapterView;
    10  import android.widget.BaseAdapter;
    11 import android.widget.CheckBox;
    12 import android.widget.CompoundButton;
    13 import android.widget.ListView;
    14 import android.widget.TextView;
    15 import android.widget.Toast;
    16 import android.widget.AdapterView.OnItemClickListener;
    17 import android.widget.CompoundButton.OnCheckedChangeListener;
    18
    19 publicclass Main extends Activity {
    20 private ListView list;
    21 private ListAdapter listadapter;
    22 private String[] arr;
    23 /** Called when the activity is first created. */
    24 @Override
    25 publicvoid onCreate(Bundle savedInstanceState) {
    26 super.onCreate(savedInstanceState);
    27 setContentView(R.layout.main);
    28 arr=new String[]{"111","222","333"};
    29 // 绑定Layout里面的ListView
    30 list = (ListView) findViewById(R.id.ListView);
    31 listadapter =new ListAdapter();
    32 // 添加并且显示
    33 list.setAdapter(listadapter);
    34 // 添加点击事件
    35 list.setOnItemClickListener(new OnItemClickListener() {
    36
    37 publicvoid onItemClick(AdapterView<?> parent, View view,
    38 int position, long id) {
    39 // TODO Auto-generated method stub
    40 //这里放Item点击事件
    41 Toast.makeText(Main.this,"Item点击事件",Toast.LENGTH_SHORT).show();
    42 }
    43 });
    44
    45 }
    46 privateclass ListAdapter extends BaseAdapter {
    47
    48 publicint getCount() {
    49 // TODO Auto-generated method stub
    50 return arr.length;
    51 }
    52
    53 public Object getItem(int position) {
    54 // TODO Auto-generated method stub
    55 return position;
    56 }
    57
    58 publiclong getItemId(int position) {
    59 // TODO Auto-generated method stub
    60 return position;
    61 }
    62
    63 public View getView(int position, View view, ViewGroup parent) {
    64 // TODO Auto-generated method stub
    65 //获取布局文件
    66 if (view ==null) {
    67 view = getLayoutInflater().inflate(R.layout.listview, null);
    68 }
    69 //获取控件
    70 TextView name = (TextView) view.findViewById(R.id.wishname);
    71 CheckBox ck = (CheckBox) view.findViewById(R.id.checkBox1);
    72
    73 if(arr!=null)
    74 {
    75 name.setText(arr[position]);
    76 name.setOnClickListener(new OnClickListener() {
    77
    78 @Override
    79 publicvoid onClick(View v) {
    80 // TODO Auto-generated method stub
    81 //这里放点击改变事件
    82 Toast.makeText(Main.this,"TextView点击事件",Toast.LENGTH_SHORT).show();
    83 }
    84 });
    85 ck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    86
    87 publicvoid onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    88 // TODO Auto-generated method stub
    89
    90 //这里放点击改变事件
    91 Toast.makeText(Main.this,"CheckBox点击事件",Toast.LENGTH_SHORT).show();
    92 }
    93 });
    94 }
    95 return view;
    96 }
    97
    98 }
    99 }

    主页面的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:padding
    ="5dip"
    android:id
    ="@+id/ListView"
    >
    </ListView>
    </LinearLayout>

    listitem的xml布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android
    ="http://schemas.android.com/apk/res/android"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:padding
    ="10dip"
    android:id
    ="@+id/linear"
    >
    <TextView
    android:text
    ="TextView01"
    android:layout_width
    ="100px"
    android:layout_height
    ="wrap_content"
    android:textSize
    ="20dip"
    android:gravity
    ="left"
    android:id
    ="@+id/wishname"/>
    <CheckBox
    android:layout_width
    ="40px"
    android:layout_height
    ="wrap_content"
    android:layout_alignParentRight
    ="true"
    android:layout_marginLeft
    ="140dp"
    android:focusable
    ="false"//加这句的原因是因为checkBox的点击事件优先级高于listview的点击事件,所以要屏蔽之
    android:id
    ="@+id/checkBox1"
    >
    </CheckBox>
    </LinearLayout>

    以上代码还没有优化,当数据多了就效率就会下降。等有时间再优化一下。

    运行效果图如下:

  • 相关阅读:
    broncho a1 hack指南-准备硬件
    嵌入式GUI ftk0.1发布
    ASP.net页面防刷新
    C#反射入门教程(转)
    万物生萨顶顶
    [转载内容]C# win程序中主窗体菜单的权限控制
    VB.net技巧更新(一)
    XML与dataset里相互读取操作
    操作EXCEL代码(c#完全版)
    [转载内容]动态创建菜单,menustrip,根据权限显示菜单,控制菜单可用,反射,给窗体传值,反射对象传值,public static Object CreateInstance ( Type type, params Object[] args )
  • 原文地址:https://www.cnblogs.com/tanlon/p/1958692.html
Copyright © 2011-2022 走看看