关于SwipeMenuListView的侧滑menu的内容修改

  • SwipeMenuListView只提供了统一样式的menu,一个或者多个,但是如何修改每个menu可以动态的自定义显示的内容

看源码知道重写了setAdapter(SwipeMenuAdapter)方法

 @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(new SwipeMenuAdapter(getContext(), adapter) {
            @Override
            public void createMenu(SwipeMenu menu) {
                if (mMenuCreator != null) {
                    mMenuCreator.create(menu);
                }
            }

            @Override
            public void onItemClick(SwipeMenuView view, SwipeMenu menu,
                                    int index) {
                boolean flag = false;
                if (mOnMenuItemClickListener != null) {
                    flag = mOnMenuItemClickListener.onMenuItemClick(
                            view.getPosition(), menu, index);
                }
                if (mTouchView != null && !flag) {
                    mTouchView.smoothCloseMenu();
                }
            }
          }

进入SwipeMenuAdapter的代码看

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SwipeMenuLayout layout = null;
        if (convertView == null) {
            View contentView = mAdapter.getView(position, convertView, parent);
            SwipeMenu menu = new SwipeMenu(mContext);
            menu.setViewType(getItemViewType(position));
            createMenu(menu);
            SwipeMenuView menuView = new SwipeMenuView(menu,
                    (SwipeMenuListView) parent);
            menuView.setOnSwipeItemClickListener(this);
            SwipeMenuListView listView = (SwipeMenuListView) parent;
            layout = new SwipeMenuLayout(contentView, menuView,
                    listView.getCloseInterpolator(),
                    listView.getOpenInterpolator());
            layout.setPosition(position);
        } else {
            layout = (SwipeMenuLayout) convertView;
            layout.closeMenu();
            layout.setPosition(position);
            View view = mAdapter.getView(position, layout.getContentView(),
                    parent);
        }
        bindView(layout,position);//暴露要复写的方法(新加的)
        if (mAdapter instanceof BaseSwipListAdapter) {
            boolean swipEnable = (((BaseSwipListAdapter) mAdapter).getSwipEnableByPosition(position));
            layout.setSwipEnable(swipEnable);
        }
        return layout;
    }

回到SwipeMenuAdapter内部实现bindView方法

//datas是外部添加进来的数据  设置menu的数据源
@Override
            public void bindView(SwipeMenuLayout layout, int position) {
                if (datas != null && datas.size() > 0){
                    SwipeMenuView swipeMenuView = (SwipeMenuView) layout.getChildAt(1);
                    ViewGroup group = (ViewGroup) swipeMenuView.getChildAt(0);
                    TextView text = (TextView) group.getChildAt(0);
                    if (datas.get(position)){
                        text.setText("打折");
                    } else {
                        text.setText("不打折");
                    }
                }
            }

datas的数据源变化了 我们只要notifyDataSetChanged就可以了

你可能感兴趣的:(android)