如何使用自定义项适配器在 ListFragment 中实现右滑?

How to implement right swipe in ListFragment with Custom Item Adapter?

我需要的是检测项目的右滑并显示一些activity。 我做了上一个。调查,但要找到正确的解决方案似乎并不明显。 请帮帮我。

我的代码如下。

public class FragmentTwo extends ListFragment  {

    public FragmentTwo() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_fragment_two, container, false);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ListView listView = getListView();

        listView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){

                    Toast.makeText(getContext(),"ACTION_UP", Toast.LENGTH_SHORT).show();
                    return true;
                }

                if(event.getAction() == MotionEvent.ACTION_DOWN){

                    Toast.makeText(getContext(),"ACTION_DOWN", Toast.LENGTH_SHORT).show();
                    return true;
                }

                if(event.getAction() == MotionEvent.ACTION_MOVE){

                    Toast.makeText(getContext(),"ACTION_MOVE", Toast.LENGTH_SHORT).show();
                    return true;
                }

                return false;
            }
        });

        new FetchTransportData().execute();

    }

    private class FetchTransportData extends AsyncTask<Void, Void, String> {
        @Override
        protected String doInBackground(Void... params) {      

                    // ...
                    return result;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
               // ...  
              CustomListAdapter adapter = new CustomListAdapter(getActivity(), unitViews);
               setListAdapter(adapter); 
        }
    }
}

您可以使用 SwipeActionAdapter 库。

已将您的 ListFragment 定义为:

public class MyListFragment extends ListFragment {
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        String[] content = new String[20];
        for (int i = 0; i < 20; i++) content[i] = "Row " + (i + 1);
        ArrayAdapter<String> stringAdapter = new ArrayAdapter<>(
                getContext(),
                R.layout.row_bg,
                R.id.text,
                new ArrayList<>(Arrays.asList(content))
        );
        SwipeActionAdapter mAdapter = new SwipeActionAdapter(stringAdapter);
        mAdapter.setListView(getListView());
        setListAdapter(mAdapter);

        // Set backgrounds for the swipe directions
        mAdapter.addBackground(SwipeDirection.DIRECTION_FAR_LEFT, R.layout.row_bg_left_far)
                .addBackground(SwipeDirection.DIRECTION_NORMAL_LEFT, R.layout.row_bg_left)
                .addBackground(SwipeDirection.DIRECTION_FAR_RIGHT, R.layout.row_bg_right_far)
                .addBackground(SwipeDirection.DIRECTION_NORMAL_RIGHT, R.layout.row_bg_right);

        // Listen to swipes
        mAdapter.setSwipeActionListener(new SwipeActionAdapter.SwipeActionListener() {
            @Override
            public boolean hasActions(int position, SwipeDirection direction) {
                if (direction.isLeft()) return false; // Change this to false to disable left swipes
                if (direction.isRight()) return true;
                return false;
            }

            @Override
            public boolean shouldDismiss(int position, SwipeDirection direction) {
                // Only dismiss an item when swiping normal left
                return direction == SwipeDirection.DIRECTION_FAR_RIGHT;
            }

            @Override
            public void onSwipe(int[] positionList, SwipeDirection[] directionList) {
                for (int i = 0; i < positionList.length; i++) {
                    SwipeDirection direction = directionList[i];
                    int position = positionList[i];
                    String dir = "";

                    switch (direction) {
                        case DIRECTION_FAR_RIGHT:
                            dir = "Far right";
                            break;
                        case DIRECTION_NORMAL_RIGHT:
                            dir = "Right";
                            break;
                    }
                    Toast.makeText(
                            getContext(),
                            dir + " swipe Action triggered on " + mAdapter.getItem(position),
                            Toast.LENGTH_SHORT
                    ).show();
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}

您将获得以下输出:

可以找到所有资源 xml 文件 in the library resources

创建自定义 class 扩展 ItemTouchHelper.SimpleCallback

/**
 * Created by admin on 19-12-2017.
 */
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public abstract class SwipeHelper extends ItemTouchHelper.SimpleCallback {

    public static final int BUTTON_WIDTH = 200;
    private RecyclerView recyclerView;
    private List<UnderlayButton> buttons;
    private GestureDetector gestureDetector;
    private int swipedPos = -1;
    private float swipeThreshold = 0.5f;
    private Map<Integer, List<UnderlayButton>> buttonsBuffer;
    private Queue<Integer> recoverQueue;
    public Context context;

    private GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener(){
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            for (UnderlayButton button : buttons){
                if(button.onClick(e.getX(), e.getY()))
                    break;
            }

            return true;
        }
    };

    private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent e) {
            if (swipedPos < 0) return false;
            Point point = new Point((int) e.getRawX(), (int) e.getRawY());
            RecyclerView.ViewHolder swipedViewHolder = recyclerView.findViewHolderForAdapterPosition(swipedPos);
            if(swipedViewHolder==null) return false;

            View swipedItem = swipedViewHolder.itemView;
            Rect rect = new Rect();
            swipedItem.getGlobalVisibleRect(rect);

            if (e.getAction() == MotionEvent.ACTION_DOWN || e.getAction() == MotionEvent.ACTION_UP ||e.getAction() == MotionEvent.ACTION_MOVE) {
                if (rect.top < point.y && rect.bottom > point.y)
                    gestureDetector.onTouchEvent(e);
                else {
                    recoverQueue.add(swipedPos);
                    swipedPos = -1;
                    recoverSwipedItem();
                }
            }
            return false;
        }
    };

    public SwipeHelper(Context context, RecyclerView recyclerView) {
        super(0, ItemTouchHelper.LEFT);
        this.recyclerView = recyclerView;
        this.context=context;
        this.buttons = new ArrayList<>();
        this.gestureDetector = new GestureDetector(context, gestureListener);
        this.recyclerView.setOnTouchListener(onTouchListener);
        buttonsBuffer = new HashMap<>();
        recoverQueue = new LinkedList<Integer>(){
            @Override
            public boolean add(Integer o) {
                if (contains(o))
                    return false;
                else
                    return super.add(o);
            }
        };
        attachSwipe();
    }


    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        int pos = viewHolder.getAdapterPosition();

        if (swipedPos != pos)
            recoverQueue.add(swipedPos);

        swipedPos = pos;

        if (buttonsBuffer.containsKey(swipedPos))
            buttons = buttonsBuffer.get(swipedPos);
        else
            buttons.clear();

        buttonsBuffer.clear();
        swipeThreshold = 0.5f ;
        recoverSwipedItem();
    }

    @Override
    public float getSwipeEscapeVelocity(float defaultValue) {
        return 0.1f * defaultValue;
    }

    @Override
    public float getSwipeVelocityThreshold(float defaultValue) {
        return 2.0f * defaultValue;
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        int pos = viewHolder.getAdapterPosition();
        float translationX = dX;
        View itemView = viewHolder.itemView;
        float height = (float) itemView.getBottom() - (float) itemView.getTop();


        if (pos < 0){
            swipedPos = pos;
            return;
        }

        if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){
            if(dX < 0) {
                List<UnderlayButton> buffer = new ArrayList<>();

                if (!buttonsBuffer.containsKey(pos)){
                    instantiateUnderlayButton(viewHolder, buffer);
                    buttonsBuffer.put(pos, buffer);
                }
                else {
                    buffer = buttonsBuffer.get(pos);
                }

                translationX = dX * buffer.size() * height / itemView.getWidth();
                drawButtons(c, itemView, buffer, pos, translationX);
            }
        }


        super.onChildDraw(c, recyclerView, viewHolder, translationX, dY, actionState, isCurrentlyActive);
    }

    private synchronized void recoverSwipedItem(){
        while (!recoverQueue.isEmpty()){
            Iterator iterator=recoverQueue.iterator();
            while(iterator.hasNext())
            {
                int pos = recoverQueue.poll();
                if (pos > -1) {
                    //clearView(recyclerView,recyclerView.findViewHolderForAdapterPosition(pos));
                    recyclerView.getAdapter().notifyItemChanged(pos);
                    recyclerView.requestLayout();
                    //recyclerView.updateViewLayout(recyclerView.getChildAt(pos),recyclerView.getChildAt(pos).getLayoutParams()) ;
                    //itemTouchUIUtil.clearView(recyclerView,recyclerView.getChildAt(pos));
                }
            }
            recoverQueue.clear();

        }
    }

    private void drawButtons(Canvas c, View itemView, List<UnderlayButton> buffer, int pos, float dX){
        //float right = itemView.getRight();
       /// float dButtonWidth = (-1) * dX / buffer.size();



        for (UnderlayButton button : buffer) {
            //float left = right - dButtonWidth;
            button.onDraw(c,itemView,dX, pos ,context           );

            //right = left;
        }
    }

    public void attachSwipe(){
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(this);
        itemTouchHelper.attachToRecyclerView(recyclerView);

    }

    public abstract void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons);

    public static class UnderlayButton {
        private String text;
        private int imageResId;
        private int color;
        private int pos;
        private RectF clickRegion;
        private UnderlayButtonClickListener clickListener;

        public UnderlayButton(String text, int imageResId, int color, UnderlayButtonClickListener clickListener) {
            this.text = text;
            this.imageResId = imageResId;
            this.color = color;
            this.clickListener = clickListener;
        }

        public boolean onClick(float x, float y){
            if (clickRegion != null && clickRegion.contains(x, y)){
                clickListener.onClick(pos);
                return true;
            }

            return false;
        }

        public void onDraw(Canvas c,View itemView, float dX, int pos,Context context){

            Bitmap icon;
            Paint p = new Paint();

            float height = (float) itemView.getBottom() - (float) itemView.getTop();
            float width = height / 3;

            // Draw background
            p.setColor(color);
            RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop()+12,(float) itemView.getRight(), (float) itemView.getBottom()-6);
            c.drawRect(background,p);
            icon = BitmapFactory.decodeResource(context.getResources(), imageResId);
            RectF icon_dest = new RectF((float) itemView.getRight() - 2*width ,(float) itemView.getTop() + width,(float) itemView.getRight() - width,(float)itemView.getBottom() - width);
            c.drawBitmap(icon,null,icon_dest,p);

            clickRegion = background;
            this.pos = pos;
        }
    }

    public interface UnderlayButtonClickListener {
        void onClick(int pos);
    }
}

在 Activity.Java 或片段

中使用 Like This
 SwipeHelper swipeHelper=new SwipeHelper(getContext(),recyclerView) {
            @Override
            public void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons) {

                underlayButtons.add(new UnderlayButton("call",
                        R.drawable.call,
                        Color.parseColor("#388E3C"), new UnderlayButtonClickListener() {
                    @Override
                    public void onClick(final int pos) {
                        //Utils.Toast_L(getContext(),"Swipe call" +Temp_filter.get(pos).getName());
                        new AlertDialog.Builder(context)
                                .setCancelable(true)
                                .setTitle("Call")
                                .setMessage("Do you want to make a call to : "+Temp_filter.get(pos).getName())
                                .setPositiveButton("call", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+Temp_filter.get(pos).getMobile())));
                                    }
                                })
                                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {

                                    }
                                })
                                .create()
                                .show();
                    }
                }));

            }
        };

您好,请按照下面的 link 进行操作,它运行良好,我已经尝试过了,之后告诉您使用了下面的 link 并按照它进行操作。

https://github.com/daimajia/AndroidSwipeLayout

希望这对您有用并节省您的时间。 如果它工作正常,请关闭此问题并对其进行投票。