[Android-2A] -仿IOS微信滑动删除_SwipeListview左滑删除例子

时间:2016-05-20 08:34:47   收藏:0   阅读:921

https://yunpan.cn/cueUIQkRafQrH (提取码:7ec1)

关于这样类似的例子网上的代码很多,最近发现这个例子里的代码在开发中会遇到一系列的问题。比如ListView的OnItemClickListener中无法获取在AppAdapter中getView方法convertView setTag的数据,所以需要优化下。

原因是控件中的SwipeMenuAdapter对Listview中的条目进行了修改和再封装。

具体优化的思路为,将控件中的SwipeMenuAdapter去掉,把部分代码挪到Acitivity中的AppAdapter中。修改后的代码如下:

class AppAdapter extends BaseAdapter  implements OnSwipeItemClickListener{
        
        @Override
        public int getCount() {
            return mAppList.size();
        }

        @Override
        public ApplicationInfo getItem(int position) {
            return mAppList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            SwipeMenuLayout layout = null;
            if (convertView == null) {
                convertView = View.inflate(getApplicationContext(), R.layout.item_list_app, null);
                holder = new ViewHolder(convertView);
                SwipeMenu menu = new SwipeMenu(mContext);
                menu.setViewType(new Random().nextInt(3));//随机选择样式
                createMenu(menu);
                SwipeMenuView menuView = new SwipeMenuView(menu,(SwipeMenuListView) parent);
                menuView.setOnSwipeItemClickListener(this);
                SwipeMenuListView listView = (SwipeMenuListView) parent;
                layout = new SwipeMenuLayout(convertView, menuView, listView.getCloseInterpolator(), listView.getOpenInterpolator());
                layout.setPosition(position);
                layout.setTag(holder);
            } else {
                layout = (SwipeMenuLayout) convertView;
                layout.closeMenu();
                layout.setPosition(position);
                holder = (ViewHolder)layout.getTag();
            }
            ApplicationInfo item = getItem(position);
            holder.iv_icon.setImageDrawable(item.loadIcon(getPackageManager()));
            holder.tv_name.setText(item.loadLabel(getPackageManager()));
            return layout;
        }

        class ViewHolder {
            ImageView iv_icon;
            TextView tv_name;
            public ViewHolder(View view) {
                iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
                tv_name = (TextView) view.findViewById(R.id.tv_name);
            }
        }


        
        public void createMenu(SwipeMenu menu) {
            if(creator != null) {
                creator.create(menu);
            }
        }

        @Override
        public void onItemClick(SwipeMenuView view, SwipeMenu menu, int index) {
            if (mListView != null && mListView.getOnMenuItemClickListener() != null) {
                mListView.getOnMenuItemClickListener().onMenuItemClick(view.getPosition(), menu, index);
            }
            
            if(mListView != null && mListView.getTouchView() != null) {
                mListView.getTouchView().smoothCloseMenu();
            }
        }
    }

Eclipse源码:https://yunpan.cn/cuepakBM7KaU5 (提取码:b860)

Android Studio源码:https://yunpan.cn/cuephTqj8emRw (提取码:93e5)

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!