首页 > 代码库 > Android ListView复制、删除的实现
Android ListView复制、删除的实现
适配器MyAdapter:
package com.zihao.adapter;import java.util.List;import com.zihao.popdemo.R;import com.zihao.util.TextManager;import android.content.Context;import android.graphics.drawable.BitmapDrawable;import android.view.Gravity;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.View.OnLongClickListener;import android.view.ViewGroup;import android.view.View.OnTouchListener;import android.widget.BaseAdapter;import android.widget.PopupWindow;import android.widget.TextView;import android.widget.Toast;public class MyAdapter extends BaseAdapter { private List<String> mTitleArray;// 标题列表 private LayoutInflater inflater = null; private Context mContext; private PopupWindow popupWindow; private static final int SHOW_TIME = 1000;//点击后显示时间 /** * Adapter构造方法 * * @param titleArray */ public MyAdapter(Context context, List<String> titleArray) { // TODO Auto-generated constructor stub this.mTitleArray = titleArray; this.mContext = context; inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); initPopupWindow(inflater); } /** * 获取总数 */ @Override public int getCount() { // TODO Auto-generated method stub return mTitleArray.size(); } /** * 获取Item对象 */ @Override public Object getItem(int position) { // TODO Auto-generated method stub return mTitleArray.get(position); } /** * 获取Item的ID */ @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.list_item_layout, null); holder.titleTv = (TextView) convertView.findViewById(R.id.item_tv); convertView.setTag(holder);//判断View是否存在,不存在则新建,否则拿过来用 } else { holder = (ViewHolder) convertView.getTag(); } // 设置 holder.titleTv.setText(mTitleArray.get(position)); holder.titleTv.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) {// 长按事件 // TODO Auto-generated method stub showPop(v); copyTv.setOnTouchListener(new tvOnTouch(mContext, position)); deleteTv.setOnTouchListener(new tvOnTouch(mContext, position)); return false; } }); return convertView; } private TextView copyTv, deleteTv; /** * 初始化Popupwindow * * @param inflater */ private void initPopupWindow(LayoutInflater inflater) { View view = inflater.inflate(R.layout.pop_item_layout, null); popupWindow = new PopupWindow(view, 200, 100);//使用弹出窗口的形式显示复制,删除按钮 copyTv = (TextView) view.findViewById(R.id.pop_copy_tv); deleteTv = (TextView) view.findViewById(R.id.pop_delete_tv); } /** * 触摸事件 * * @author zihao * */ class tvOnTouch implements OnTouchListener { private Context mContext; private int mPosition; public tvOnTouch(Context context, int position) { // TODO Auto-generated method stub this.mContext = context; this.mPosition = position; } @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (v.getId() == R.id.pop_copy_tv) { TextView tv = (TextView) v; if (event.getAction() == MotionEvent.ACTION_DOWN) {// 按下 tv.setTextColor(0xff00CD66); } else if (event.getAction() == MotionEvent.ACTION_UP) {// 松开 tv.setTextColor(0xffffffff); TextManager.copyText(mContext, mTitleArray.get(mPosition));//调用复制函数 Toast.makeText(mContext, "复制成功", SHOW_TIME).show(); if (popupWindow != null) { popupWindow.dismiss(); } } } else { TextView tv = (TextView) v; if (event.getAction() == MotionEvent.ACTION_DOWN) {// 按下 tv.setTextColor(0xff00CD66); } else if (event.getAction() == MotionEvent.ACTION_UP) {// 松开 tv.setTextColor(0xffffffff); mTitleArray.remove(mPosition);//删除函数 notifyDataSetChanged(); Toast.makeText(mContext, "删除成功", SHOW_TIME).show(); if (popupWindow != null) { popupWindow.dismiss(); } } } return true; } } /** * Popupwindow显示 * * @param v */ @SuppressWarnings("deprecation") private void showPop(View v) { popupWindow.setFocusable(false); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable());// 设置此项可点击Popupwindow外区域消失,注释则不消失 // 设置出现位置 int[] location = new int[2]; v.getLocationOnScreen(location); popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] + v.getWidth() / 2 - popupWindow.getWidth() / 2, location[1] - popupWindow.getHeight()); } static class ViewHolder { TextView titleTv; }}
pop_item_layout:
<?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="match_parent" android:background="@drawable/tips_manager_bg" android:gravity="center" android:orientation="horizontal" android:padding="0dp" > <TextView android:id="@+id/pop_copy_tv" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center" android:paddingBottom="6dp" android:text="复制" android:textColor="@android:color/white" /> <!--分割线--> <View android:layout_width="4dp" android:layout_height="fill_parent" /> <TextView android:id="@+id/pop_delete_tv" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center" android:paddingBottom="6dp" android:text="删除" android:textColor="@android:color/white" /></LinearLayout>
TextManager:
package com.zihao.util;import android.annotation.SuppressLint;import android.content.ClipboardManager;import android.content.Context;/** * 文本操作工具类 * * @author zihao * */@SuppressLint("NewApi")@SuppressWarnings("deprecation")public class TextManager { /** * 复制文本 * * @param context * @param message * // 被复制的文本 */ public static void copyText(Context context, String message) { // 获取剪贴板管理服务 ClipboardManager cmb = (ClipboardManager) context .getSystemService(Context.CLIPBOARD_SERVICE); // 将文本数据复制到剪贴板 cmb.setText(message.trim()); } /** * 粘贴文本 * * @param context * @return */ public static String pasteText(Context context) { // 得到剪贴板管理器 ClipboardManager cmb = (ClipboardManager) context .getSystemService(Context.CLIPBOARD_SERVICE); return cmb.getText().toString().trim(); }}
MainActivity:在主函数应该有一个事件来触发 pasteText函数
package com.zihao.popdemo;import java.util.ArrayList;import java.util.List;import com.zihao.adapter.MyAdapter;import android.os.Bundle;import android.widget.ListView;import android.app.Activity;/** * 主界面 * * @author zihao * */public class MainActivity extends Activity { private ListView mListView;// ListView private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } /** * 初始化视图 */ private void initView() { mListView = (ListView) findViewById(R.id.my_list); myAdapter = new MyAdapter(this, getListData()); mListView.setAdapter(myAdapter); } /** * 获取ListView数据 * * @return */ private List<String> getListData() { List<String> titleArray = new ArrayList<String>(); for (int i = 0; i < 10; i++) { titleArray.add("这是第" + i + "个子项"); } return titleArray; }}
代码:这里
Android ListView复制、删除的实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。