Display button while sliding Listview item

Asked

Viewed 642 times

2

I need to make two buttons appear when I slide an item from ListView to the left. I even managed to do that, but it’s pretty ugly. I would like them to appear as if they were being "pulled" from the right corner of the screen, just like in the Gmail app. My code:

items from ListView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relativelayout">

    <ImageButton
        android:layout_width="60dp"
        android:layout_height="65dp"
        android:background="@color/vermelho"
        android:src="@drawable/lixobotao"
        android:id="@+id/btnexcluir"
        android:layout_alignParentTop="true"
        android:layout_toStartOf="@+id/btnfavorito"
        android:visibility="invisible"/>

    <ImageButton
        android:layout_width="60dp"
        android:layout_height="65dp"
        android:background="@color/amarelo"
        android:src="@drawable/estrelabotao"
        android:id="@+id/btnfavorito"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:visibility="invisible" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/primary_text"
        android:id="@+id/nomelista"
        android:textSize="20dp"
        android:text="nomelista"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/secondary_text"
        android:id="@+id/datalista"
        android:textSize="15dp"
        android:text="datalista"
        android:layout_below="@+id/nomelista"
        android:layout_alignParentStart="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/secondary_text"
        android:id="@+id/precolista"
        android:textSize="15dp"
        android:text="precolista"
        android:layout_below="@+id/datalista"
        android:layout_alignStart="@+id/datalista" />

</RelativeLayout>

Activity:

@Override
    protected void onCreate(Bundle savedInstanceStade){
        super.onCreate(savedInstanceStade);
        setContentView(R.layout.minhas_listas);

        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        a = new AdaptadorListas(listasCompra,this);

        listas = (ListView) findViewById(R.id.listaListas);
        Button novaLista = (Button) findViewById(R.id.novalista);
        listas.setAdapter(a);


        final SwipeDetector swipeDetector= new SwipeDetector();
        listas.setOnTouchListener(swipeDetector);

        listas.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(swipeDetector.swipeDetected()){
                    if(swipeDetector.getAction() == Action.RL){
                        ImageButton addfav = (ImageButton) view.findViewById(R.id.btnfavorito);
                        ImageButton excluir = (ImageButton) view.findViewById(R.id.btnexcluir);
                        addfav.setVisibility(View.VISIBLE);
                        excluir.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

Swipedetector.java:

public class SwipeDetector implements View.OnTouchListener {


        public static enum Action {
            LR, // Left to Right
            RL, // Right to Left
            TB, // Top to bottom
            BT, // Bottom to Top
            None // when no action was detected
        }

        private static final String logTag = "SwipeDetector";
        private static final int MIN_DISTANCE = 100;
        private float downX, downY, upX, upY;
        private Action mSwipeDetected = Action.None;

        public boolean swipeDetected() {
            return mSwipeDetected != Action.None;
        }

        public Action getAction() {
            return mSwipeDetected;
        }

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    downX = event.getX();
                    downY = event.getY();
                    mSwipeDetected = Action.None;
                    return false; // allow other events like Click to be processed
                }
                case MotionEvent.ACTION_MOVE: {
                    upX = event.getX();
                    upY = event.getY();

                    float deltaX = downX - upX;
                    float deltaY = downY - upY;

                    // horizontal swipe detection
                    if (Math.abs(deltaX) > MIN_DISTANCE) {
                        // left or right
                        if (deltaX < 0) {
                            Log.i(logTag, "Swipe Left to Right");
                            mSwipeDetected = Action.LR;
                            return true;
                        }
                        if (deltaX > 0) {
                            Log.i(logTag, "Swipe Right to Left");
                            mSwipeDetected = Action.RL;
                            return true;
                        }
                    } else

                        // vertical swipe detection
                        if (Math.abs(deltaY) > MIN_DISTANCE) {
                            // top or down
                            if (deltaY < 0) {
                                Log.i(logTag, "Swipe Top to Bottom");
                                mSwipeDetected = Action.TB;
                                return false;
                            }
                            if (deltaY > 0) {
                                Log.i(logTag, "Swipe Bottom to Top");
                                mSwipeDetected = Action.BT;
                                return false;
                            }
                        }
                    return true;
                }
            }
            return false;
        }
    }

I’d like to keep it that way when I slide: inserir a descrição da imagem aqui

When I swipe left in my app the buttons appear but without any effect, they simply "appear" on the right side of the screen.

  • 1

    Put a picture of how you are and a drawing of how you want it to look, so it’s clear.

  • You have a very good lib on github https://github.com/daimajia/AndroidSwipeLayout I hope to help :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.