我有疑问。在我的列表视图( lv )中,我有方法可以对其中的项目和长按进行点击。
我的代码: (我必须在java中设置,因为在xml中它不起作用)
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
arg1.findViewById(R.id.jmenoskoly);
TV = (TextView) arg1.findViewById(R.id.jmenoskoly);
TV.setEllipsize(TextUtils.TruncateAt.MARQUEE);
TV.setSingleLine(true);
TV.setMarqueeRepeatLimit(5);
TV.setSelected(true);
return true;
}
});
所以我的问题是: 如何检查用户是否停止在列表视图中保留该项? (请完整代码示例。)
答案 0 :(得分:0)
使用TouchListener创建自定义Adapter类。您可以在向下操作上启动选取框并在向上操作上停止它:
public class MyAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<String> mItems; // However you want to store your data
private static class ViewHolder{
// Put the views you want to interact with here
TextView textView;
ViewHolder(View view){
textView = (TextView) view.findViewById(R.id.text_view);
}
}
public MyAdapter(Context context, ArrayList<String> items){
mContext = context;
mItems = items;
}
@Override
public int getCount(){
return mItems.size();
}
@Override
public String getItem(int position){
return mItems.get(position);
}
@Override
public long getItemId(int position){
return position;
}
public void add(String item){
mItems.add(item);
notifyDataSetChanged();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
// You don't have to use the ViewHolder pattern but it's meant to
// optimize performance
if(convertView == null){
convertView = LayoutInflater.from(mContext)
.inflate(R.layout.my_item_layout, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
// Set the values of your views
viewHolder.textView.setText(mItems.get(position));
// Set your listener on the desired view
viewHolder.textView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// Do something on the down action
return true; // Consume the action
case MotionEvent.ACTION_UP:
// Do something on the up action
return true; // Consume the action
}
return false;
}
});
return convertView;
}
}
然后在你的活动中:
// Create a new instance of your custom Adapter
// passing in the Context and item values
MyAdapter adapter = new MyAdapter(context, myItems);
listview.setAdapter(adapter); // Attach Adapter to ListView