ListView item 中TextView 如何获取长按事件
昨天晚上小伙伴突然来信, ListView item中嵌套的TextView 无法获取长按事件
从前从来没有仔细留意过, coding后发现...果然没什么动静
而且没有合适的API让我调用获取TextView 的 setOnLongClickListener 事件
思考ing
ListView几大要素
xml: item模板 --- 这个不可能绑定事件
java: ListView本身 --- 有可能, 但是本身没有提供API
adapter: 直接使用现成的 SimpleAdapter --- 无法做到, 以前做过重写Adapter的事好像...
有个 getView 方法是必须重写的, 然后里面貌似可以操作单个控件 .... 恩 有可能,动手试试
定义MyAdapter类:
import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; import java.util.Map; /** * Created by root on 15-11-6. */ public class MyAdapter extends BaseAdapter { LayoutInflater mLayoutInflater; List<Map<String, Object>> mData; public static final String KEY_NAME = "name"; // Context context, MyAdapter public MyAdapter(Context context, List<Map<String, Object>> data) { mLayoutInflater = LayoutInflater.from(context); mData = data; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null) { convertView = mLayoutInflater.inflate(R.layout.listview_item_text, null); TextView tv = (TextView) convertView.findViewById(R.id.text1); tv.setText(mData.get(position).get(KEY_NAME).toString()); tv.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Log.d("carlozlog", "TextView be long click"); return true; } }); } return convertView; } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } }
此处用到一个ListViiew的 item 模板
listview_item_text.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" android:layout_width="fill_parent" android:textColor="#000000" android:layout_height="50dp" />
使用adaper: MainActivity.java 中
public class MainActivity extends AppCompatActivity { // view private ListView listView1; // data Context mContext; //SimpleAdapter mSimpleAdapter; MyAdapter mMyAdapter; String tag = "carlozlog"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); Map<String, Object> map1 = new HashMap<>(); map1.put(MyAdapter.KEY_NAME, "Hello World"); Map<String, Object> map2 = new HashMap<>(); map2.put(MyAdapter.KEY_NAME, "Lu Xun"); Map<String, Object> map3 = new HashMap<>(); map3.put(MyAdapter.KEY_NAME, "Zhu De"); Map<String, Object> map4 = new HashMap<>(); map4.put(MyAdapter.KEY_NAME, "Lao Mao"); List<Map<String, Object>> listData = new ArrayList<>(); listData.add(map1); listData.add(map2); listData.add(map3); listData.add(map4); //mSimpleAdapter = new SimpleAdapter(mContext, listData, R.layout.listview_item_text, new String[]{"name"}, new int[]{R.id.text1}); mMyAdapter = new MyAdapter(mContext, listData); listView1 = (ListView)findViewById(R.id.listview1); listView1.setAdapter(mMyAdapter); listView1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Log.d(tag, "listview item " + view.getId() + " has been long clicked"); return false; } }); } }
如此就成了, 看log, 搜关键字 carlozlog
11-06 06:56:58.204 12719-12719/? D/carlozlog﹕ TextView be long click 11-06 06:56:59.869 12719-12719/? D/carlozlog﹕ TextView be long click 11-06 06:57:02.115 12719-12719/? D/carlozlog﹕ TextView be long click 11-06 06:57:04.845 12719-12719/? D/carlozlog﹕ TextView be long click 11-06 06:57:06.596 12719-12719/? D/carlozlog﹕ TextView be long click 11-06 06:57:08.755 12719-12719/? D/carlozlog﹕ TextView be long click 11-06 06:57:10.189 12719-12719/? D/carlozlog﹕ TextView be long click