Swipe To Choose Android

后端 未结 6 879
半阙折子戏
半阙折子戏 2020-12-04 10:17

I want to make view like below image... swipe to choose in ANDROID.

\"enter

i

6条回答
  •  忘掉有多难
    2020-12-04 11:19

    I have used this library: https://github.com/kikoso/Swipeable-Cards

    You need to modify it. After modification's, you will achieve it (See Screenshots below). Let me Explain.

    1.) std_card_inner.xml

    This xml is used to inflate the card row in the adapter class of the library. I have modified it to add two imageviews containing the like and dislike button and a textview to show the text "like" or "dislike" when user clicks any imageview.

    
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
    
    
    
        
    
        
    
    
    
    
    
    

    2.) SimpleCardStackAdapter.java

    This is the adapter for the cards. I have modified it to add the click listeners for both like and dislike imageview and a textview to show the text. When user clicks like button, i have added a boolean variable in the card model which stores the like/dislike value. True for like and false for dislike.

    package com.andtinder.view;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.andtinder.R;
    import com.andtinder.model.CardModel;
    
    public final class SimpleCardStackAdapter extends CardStackAdapter {
    
        public SimpleCardStackAdapter(Context mContext) {
            super(mContext);
        }
    
        @Override
        public View getCardView(int position, final CardModel model, View convertView, ViewGroup parent) {
            if(convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(R.layout.std_card_inner, parent, false);
                assert convertView != null;
            }
    
            ((ImageView) convertView.findViewById(R.id.image)).setImageDrawable(model.getCardImageDrawable());
            ((TextView) convertView.findViewById(R.id.title)).setText(model.getTitle());
            ((TextView) convertView.findViewById(R.id.description)).setText(model.getDescription());
    
    
            final TextView like_dislike_text = ((TextView) convertView.findViewById(R.id.like_dislike_text));
            if(model.isLike())
                like_dislike_text.setText("Liked");
            else
                like_dislike_text.setText("DisLiked");
    
            ((ImageView) convertView.findViewById(R.id.like)).setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    model.setLike(true);
                    like_dislike_text.setText("Liked");
                }
            });
    
            ((ImageView) convertView.findViewById(R.id.dislike)).setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    model.setLike(false);
                    like_dislike_text.setText("DisLiked");
                }
            });
    
    
            return convertView;
        }
    }
    

    3.) CardModel.java

    Finally, here I have added that boolean variable which stores the value for like/dislike.

    private boolean isLike = false;
    public boolean isLike() {
        return isLike;
    }
    
    public void setLike(boolean isLike) {
        this.isLike = isLike;
    }
    

    This is the final result:

    Screenshot 1

    enter image description here

    Screenshot 2

    enter image description here

提交回复
热议问题