android listview系列之item的点击事件及item布局中的点击事件(四)

很多时候listview只显示简略信息,我们需要点击子项去跳转或在显示详细信息的位置,将相关的详细信息显示出来,listview提供了onItemClickListener()方法,在方法中我们可以执行我们需要的内容。

listview.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //我们需要的内容,跳转页面或显示详细信息
            }
        });

listview还提供了OnItemSelectedListener()方法,可以配合界面显示的其它组件,关联显示。

//listView子菜单选择事件
helpcenterlistview.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {

        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

下面关键点来了,关于item中包含点击事件,如有拍照按钮,数据提交按钮等,我们就需要在自定义的adapter中添加点击事件的回掉接口,来实现一些操作。
下面通过代码来具体说说。
我遇到一个项目要求在listview的item包含拍照按钮,数据提交按钮,数据输入等,这时我就发愁了,怎么去实现具体的功能,如拍照,拍照返回显示的图片不要串行等,遇到了好多问题,这儿就以这个为例给大家讲一下。
下面是item的布局文件代码,里面包含Textview显示信息,ImageButton拍照和数据上传

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:padding="8dp">
    <View
        android:layout_width="fill_parent"
        android:layout_height="0.5px"
        android:background="#B8B8B8"
        android:visibility="visible"
        />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">
        <LinearLayout 
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
            <LinearLayout 
                android:id="@+id/ll_index_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="8dp"
                android:orientation="horizontal">
                <TextView 
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="指标名称"
                    android:textColor="#ffffff"
                    android:textSize="20sp"/>
                <TextView
                    android:id="@+id/tv_index"
                    android:layout_marginLeft="16dp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="监测指标"
                    android:textColor="#ffffff"
                    android:textSize="20sp" />
            </LinearLayout>
            <LinearLayout 
                android:id="@+id/ll_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="16dp"
                android:orientation="horizontal">
                <Button 
                    android:id="@+id/btn_photo"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_gravity="center"
                    android:background="@drawable/btn_paizhao"/>
                <ImageButton
                    android:id="@+id/ib_upload"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="18dp"
                    android:layout_gravity="center"
                    android:background="@drawable/tijiao"/>
            </LinearLayout>
        </LinearLayout>
        <ImageView 
            android:id="@+id/iv_data_photo"
            android:layout_height="300dp"
            android:layout_width="300dp"
            android:layout_gravity="center_vertical"/>
    </LinearLayout>
    <View
        android:layout_width="fill_parent"
        android:layout_height="0.5px"
        android:background="#B8B8B8"
        android:visibility="visible" 
        />
</LinearLayout>

要实现这些功能原先的适配器就有些不够用了,就需要一个强大的adapter来帮助listview来完成。
下面在代码的注释会帮助你去理解实现这些功能。
注:下面代码是不完整代码,留下了相关代码,别的代码都删了,要学习的朋友可以自己去理解的写,或者发评论问问


/**
 * 自定义的任务下的监测点的监测指标的listview适配器
 * @author jing_jie
 *
 */
public class IndexListViewAdapter extends BaseAdapter{
    //拍照工具类,用来处理一些照片的方法
    PhotoUtil photoUtil = new PhotoUtil();
    //检测指标的集合
    List<MeasurementUtil> indexs;
    //检测指标状态的集合
    List<MeasurementState> ms;
    //传入按钮点击事件
    private MyClickListener mListener;
    private MyClickListener pListener;
    //传入当前的DBmanager
    DBManager dbmanager;
    LayoutInflater inflater;
    //需要传入点击事件
    public IndexListViewAdapter(Context ctx,MyClickListener mListener,MyClickListener pListener,MyClickListener xListener){
        inflater = LayoutInflater.from(ctx);
        this.mListener = mListener;
        this.pListener = pListener;
        this.xListener = xListener;
    }
    @Override
    public int getCount() {
        return indexs.size();
    }
    @Override
    public Object getItem(int position) {
        return indexs.get(position);
    }
    @Override
    public long getItemId(int position) {
        return indexs.get(position).getId();
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        int valuetype = indexs.get(position).getValueType();

        view = inflater.inflate(R.layout.activity_table_photo, null);
        TextView tv_index = (TextView)view.findViewById(R.id.tv_index);
        TextView tv_description = (TextView)view.findViewById(R.id.tv_description);
        Button btn_photo = (Button)view.findViewById(R.id.btn_photo);
        ImageButton ib_upload = (ImageButton)view.findViewById(R.id.ib_upload); 

        ImageView iv_photo = (ImageView)view.findViewById(R.id.iv_data_photo);
        EditText et_note = (EditText)view.findViewById(R.id.et_index_note);

        //给点击事件的view添加tag和点击事件
        btn_photo.setTag(position);
        btn_photo.setOnClickListener(pListener);
        ib_upload.setTag(position);
        ib_upload.setOnClickListener(mListener);
        return view;
    }

    /**
      * 用于回调的抽象类
      */
     public static abstract class MyClickListener implements OnClickListener {
         /**
          * 基类的onClick方法
          */
         @Override
         public void onClick(View v) {
             myOnClick((Integer) v.getTag(), v);
         }
         public abstract void myOnClick(int position, View v);
     }
}

adapter的使用

mIndexAdapter = new IndexListViewAdapter(DataActivity.this,mListener,pListener);
lv_data.setAdapter(mIndexAdapter);

注意在使用的时候需要响应点击事件的实现类,及传入构造方法的mListener,pListener。
这而就实现一下拍照按钮

//拍照按钮的点击事件
    private MyClickListener pListener = new MyClickListener() {
        @Override
        public void myOnClick(int position, View v) {
            //获得组件
            //在GridView和ListView中,getChildAt ( int position ) 方法中position指的是当前可见区域的第几个元素。
            //如果你要获得GridView或ListView的第n个View,那么position就是n减去第一个可见View的位置
            view = lv_data.getChildAt(position - lv_data.getFirstVisiblePosition());
            //获得item中的对应的Imageview,用来拍照返回的时候显示照片略缩图
            iv_photo = (ImageView)view.findViewById(R.id.iv_data_photo);
            try {
                String state = Environment.getExternalStorageState();
                if (state.equals(Environment.MEDIA_MOUNTED)) {//判断sd卡是否插入
                    //设置路径
                    mPhotoPath = Environment.getExternalStorageDirectory().getPath()
                            + "//patrol//" + "//" + photoUtil.getPhotoFileName();
                    mPhotoFile = new File(mPhotoPath);
                    if (!mPhotoFile.exists()) {
                        mPhotoFile.createNewFile();
                    }
                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                    // 加载路径
                    Uri uri = Uri.fromFile(mPhotoFile);
                    // 指定存储路径,这样就可以保存原图了
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    startActivityForResult(intent, CAMERA_RESULT);
                }else {
                    appContext.showInfo("请插入SD卡");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

本篇博客有点粗犷,希望看的朋友们耐心点。。。

  • 20
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值