How can I make my Android SwipeableCardViews more like the IOS 7 mail app (swipe to show buttons)

后端 未结 3 1816
悲&欢浪女
悲&欢浪女 2021-02-05 18:37

How can I make my SwipeableCardViews more like the IOS 7 mail app (swipe to show buttons)

So far I have created an android application which allows the user

相关标签:
3条回答
  • 2021-02-05 19:11

    Just for a reference you can use these swipe layout library . its includes swiping actions which you were searching . Hope it ill be helpful

    enter image description here

    0 讨论(0)
  • 2021-02-05 19:11

    I've just done this. This is how it looks like:

    I don't have any CardView but you can have any layout (so you can add the support lib CardView or whatever you like).

    This is how I did it. I used this library. Since the library has minSdkVersion 15 and we have 14, I copy-pasted the following classes directly from GitHub: OnItemClickListener, RecyclerViewAdapter, SwipeableItemClickListener, SwipeToDismissTouchListener and ViewAdapter (this are the only ones you need if you use a RecyclerView).

    Because the lib has not been updated yet, you'll need to add the following method into SwipeableItemClickListener:

    @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
    

    Your list item layout should have the following structure:

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="@dimen/list_item_article_height"
        >
    
        <!-- This is the layout you see always -->    
        <include
            layout="@layout/list_item_article"
            tools:visibility="gone"
            />
    
        <!-- This is the layout shown after swiping. Must have visibility="gone" -->
        <LinearLayout
            android:visibility="gone"
            tools:visibility="visible"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            >    
            <TextView
                android:id="@+id/swipe_cancel"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:text="@string/swipe_cancel"
                android:gravity="center"
                />    
            <TextView
                android:id="@+id/swipe_delete"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="@string/swipe_delete"
                android:gravity="center"
                android:background="#f00"
                android:textColor="#fff"
                />    
        </LinearLayout>    
    </FrameLayout>
    

    Then simply add this code to your Activity/Fragment after creating the RecyclerView and setting it's adapter:

    final SwipeToDismissTouchListener<RecyclerViewAdapter> touchListener =
            new SwipeToDismissTouchListener<>(
                    new RecyclerViewAdapter(mRecyclerView),
                    new SwipeToDismissTouchListener.DismissCallbacks<RecyclerViewAdapter>() {
                        @Override
                        public boolean canDismiss(int position) {
                            return true;
                        }
                        @Override
                        public void onDismiss(RecyclerViewAdapter recyclerView, int position) {
                            // remove item at position and notify adapter
                        }
                    }
            );
    mRecyclerView.setOnTouchListener(touchListener);
    mRecyclerView.addOnScrollListener((RecyclerView.OnScrollListener) touchListener.makeScrollListener());
    mRecyclerView.addOnItemTouchListener(new SwipeableItemClickListener(
            getActivity(),
            new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (view.getId() == R.id.swipe_delete) {
                        touchListener.processPendingDismisses();
                    } else {
                        touchListener.undoPendingDismiss();
                    }
                }
            }
    ));
    
    0 讨论(0)
  • 2021-02-05 19:11

    If you want to have only add and delete button you can use RecyclerView but with RecyclerView.Adapter not CardViewAdapter. Here is an example:

    public class MyActivity extends Activity {
        private RecyclerView mRecyclerView;
        private RecyclerView.Adapter mAdapter;
        private RecyclerView.LayoutManager mLayoutManager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.my_activity);
            mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    
            // use this setting to improve performance if you know that changes
            // in content do not change the layout size of the RecyclerView
            mRecyclerView.setHasFixedSize(true);
    
            // use a linear layout manager
            mLayoutManager = new LinearLayoutManager(this);
            mRecyclerView.setLayoutManager(mLayoutManager);
    
            // specify an adapter (see also next example)
            mAdapter = new MyAdapter(myDataset);
            mRecyclerView.setAdapter(mAdapter);
        }
        ...
    }
    
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private String[] mDataset;
    
        // Provide a reference to the views for each data item
        // Complex data items may need more than one view per item, and
        // you provide access to all the views for a data item in a view holder
        public static class ViewHolder extends RecyclerView.ViewHolder {
            // each data item is just a string in this case
            public TextView mTextView;
            public ViewHolder(TextView v) {
                super(v);
                mTextView = v;
            }
        }
    
        // Provide a suitable constructor (depends on the kind of dataset)
        public MyAdapter(String[] myDataset) {
            mDataset = myDataset;
        }
    
        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext())
                                   .inflate(R.layout.my_text_view, parent, false);
            // set the view's size, margins, paddings and layout parameters
            ...
            ViewHolder vh = new ViewHolder(v);
            return vh;
        }
    
        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            holder.mTextView.setText(mDataset[position]);
    
        }
    
        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return mDataset.length;
        }
    }
    
    0 讨论(0)
提交回复
热议问题