滑动以从列表视图中删除项目

时间:2018-06-05 14:35:12

标签: android listview

在我的应用程序上,我需要实现滑动以从列表视图中删除项目。为此,我尝试使用SwipeMenuListView。但它不适合我。这是我的代码。 我有一个BookmarkAdapter:

public class BookmarkAdapter extends BaseAdapter {
    private ListItemListener listener;
    Context mContext;
    ArrayList<String> mSource;
    public BookmarkAdapter(Context context, ArrayList<String> source) {
        this.mSource = source;
        this.mContext = context;
    }
    @Override
    public int getCount() {
        return mSource.size();
    }
    @Override
    public Object getItem(int position) {
        return mSource.get(position);
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.bookmark_layout_item, parent, false);
            viewHolder.textView = (TextView)convertView.findViewById(R.id.tvWord);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.textView.setText(mSource.get(position));
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                listener.onItemClick(position);
            }
        });

        return convertView;
    }
    public void clear() {
        mSource.clear();
    }
    class ViewHolder {
        TextView textView;
    }

}

在课堂BookmarkFragment上,我尝试实现滑动列表视图。

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setHasOptionsMenu(true);
        SwipeMenuListView bookmarkList = (SwipeMenuListView) view.findViewById(R.id.bookmarkList);
        adapter = new BookmarkAdapter(getActivity(), mDBHelper.getAllWordFromBookmark());
        bookmarkList.setAdapter(adapter);
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem openItem = new SwipeMenuItem(
                        MainActivity.getContext());
                openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
                openItem.setWidth(170);
                openItem.setTitle("Open");
                openItem.setTitleSize(18);
                openItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(openItem);
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        MainActivity.getContext());
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                deleteItem.setWidth(170);
                deleteItem.setIcon(R.drawable.ic_delete);
                menu.addMenuItem(deleteItem);
            }
        };
        bookmarkList.setMenuCreator(creator);
        bookmarkList.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        Log.d(TAG, "onMenuItemClick: clicked item " + index);
                        break;
                    case 1:
                        Log.d(TAG, "onMenuItemClick: clicked item " + index);
                        break;
                }
                return false;
            }
        });
        bookmarkList.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
        bookmarkList.setOnMenuStateChangeListener(new SwipeMenuListView.OnMenuStateChangeListener() {
            @Override
            public void onMenuOpen(int position) {

            }

            @Override
            public void onMenuClose(int position) {

            }
        });

        bookmarkList.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
            @Override
            public void onSwipeStart(int position) {

            }

            @Override
            public void onSwipeEnd(int position) {

            }
        });

但它不适合我。请帮助我,我怎么能解决这个问题。

0 个答案:

没有答案