參考來源:http://www.tuicool.com/articles/bamqiq java
大體思路:重寫適配器,讓主函數繼承接口接口 ListItemClickHelp,這個接口中包含onclick方法,在主函數中實現其onclick方法後,就能夠完成不一樣按鈕的點擊。參考博客中中定義了每一項的setonitemclicklistener,在跑源碼的時候發現這時候點擊每一項是沒有什麼效果的。因此換成了點擊兩個button來實現不一樣的點擊事件. android
1,重寫適配器
package com.example.yx.pumkin; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; /** * Created by hp on 2016/2/20. */ public class ListItemClickAdapter extends BaseAdapter { private Context contxet; private ArrayList<String> list; public ListItemClickHelp callback; private LayoutInflater mInflater; public ListItemClickAdapter(Context contxet, ArrayList<String> list, ListItemClickHelp callback) { this.contxet = contxet; this.list = list; this.callback = callback; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, final ViewGroup parent) { mInflater = (LayoutInflater) contxet .getSystemService(Context.LAYOUT_INFLATER_SERVICE); ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate( R.layout.list_adapter2, null); holder = new ViewHolder(); holder.mAd_tv_show = (TextView) convertView .findViewById(R.id.textView3); holder.mAd_btn_one = (ImageView) convertView .findViewById(R.id.complete_view); holder.mAd_btn_two= (ImageView) convertView .findViewById(R.id.read_view); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.mAd_tv_show.setText(list.get(position)); final View view = convertView; final int p = position; final int one = holder.mAd_btn_one.getId(); holder.mAd_btn_one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { callback.onClick(view, parent, p, one); } }); final int two = holder.mAd_btn_two.getId(); holder.mAd_btn_two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { callback.onClick(view, parent, p, two); } }); return convertView; } public static class ViewHolder { TextView mAd_tv_show; ImageView mAd_btn_one; ImageView mAd_btn_two; } }
2,接口 ListItemClickHelp app
//這是個很神奇的接口,參數position 就是listview裏面對應每一項的position. //至今不明白爲何?? ide
package com.example.yx.pumkin; import android.view.View; /** * Created by hp on 2016/2/20. */ public interface ListItemClickHelp { void onClick(View item, View widget, int position, int which); }主函數繼承這個接口後重寫onclick方法,就能相應不一樣按鈕的點擊事件了
3,適配器佈局 函數
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:textSize="20dp" android:id="@+id/textView3" android:layout_marginTop="67dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/complete_view" android:layout_toEndOf="@+id/complete_view" android:layout_marginLeft="88dp" android:layout_marginStart="88dp" /> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:id="@+id/complete_view" android:background="@drawable/quan1" android:layout_alignTop="@+id/textView3" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:id="@+id/read_view" android:background="@color/red" android:layout_alignTop="@+id/textView3" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout>
4,主函數 佈局
//列表顯示 listString=getTaskList(); madapter = new ListItemClickAdapter(MainActivity.this,listString,(ListItemClickHelp) this); lv1.setAdapter(madapter);
//響應不一樣listItem上面不一樣按鈕的完成、查看 @Override public void onClick(View item, View widget, int position, int which) { switch (which) { case R.id.complete_view: Toast.makeText(MainActivity.this,"完成了",Toast.LENGTH_LONG).show(); break; case R.id.read_view: listItem=getALLlist(); HashMap one = listItem.get(position); Intent intent = new Intent(MainActivity.this, task_read.class); Bundle data = new Bundle(); data.putInt("_id", Integer.parseInt(String.valueOf(one.get("_id")))); data.putString("task", String.valueOf(one.get("task"))); data.putString("date", String.valueOf(one.get("date"))); data.putString("task_type", String.valueOf(one.get("task_type"))); intent.putExtras(data); startActivityForResult(intent, position); //標識修改項的位置 finish(); break; default: break; } }