Android ListView Swipe Right and Left to Accept and Reject

前端 未结 6 1167
遥遥无期
遥遥无期 2020-12-05 11:37

I want to develop a list view that when swiped left to right - displays in the left corner an accept (true) icon (non clickable - show just a color change when swiping left

6条回答
  •  旧巷少年郎
    2020-12-05 12:10

    Solution number 1 :

    You Have to do like following to reach "near" to your functionality,

    Wrap the adapter of your ListView

    Like following :

      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Create an Adapter for your content
        String[] content = new String[20];
        for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
        ArrayAdapter stringAdapter = new ArrayAdapter(
            this,
            R.layout.row_bg,
            R.id.text,
            new ArrayList(Arrays.asList(content))
        );
    
       // Wrap your content in a SwipeActionAdapter
       mAdapter = new SwipeActionAdapter(stringAdapter);
    
       // Pass a reference of your ListView to the SwipeActionAdapter
       mAdapter.setListView(getListView());
    
       // Set the SwipeActionAdapter as the Adapter for your ListView
       setListAdapter(mAdapter);
      }
    

    Create a background layout for each swipe direction

    Like Following:

      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
       // Create an Adapter for your content
       String[] content = new String[20];
       for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
       ArrayAdapter stringAdapter = new ArrayAdapter(
            this,
            R.layout.row_bg,
            R.id.text,
            new ArrayList(Arrays.asList(content))
       );
    
       // Wrap your content in a SwipeActionAdapter
       mAdapter = new SwipeActionAdapter(stringAdapter);
    
       // Pass a reference of your ListView to the SwipeActionAdapter
       mAdapter.setListView(getListView());
    
       // Set the SwipeActionAdapter as the Adapter for your ListView
       setListAdapter(mAdapter);
    
       // Set backgrounds for the swipe directions
        mAdapter.addBackground(SwipeDirections.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)
              .addBackground(SwipeDirections.DIRECTION_NORMAL_LEFT,R.layout.row_bg_left)
             .addBackground(SwipeDirections.DIRECTION_FAR_RIGHT,R.layout.row_bg_right_far)
            .addBackground(SwipeDirections.DIRECTION_NORMAL_RIGHT,R.layout.row_bg_right);
    }
    

    You got both libraries and Example from the here : https://github.com/wdullaer/SwipeActionAdapter

    This will not exact like you want, but i hope this will help you so much to fulfilled your functionality.

    Solution number 2 :

    Solution 2 is about Changing some code in your Used code : http://www.tutecentral.com/android-swipe-listview/

    i had try your code from the link http://www.tutecentral.com/android-swipe-listview/ and changing some code and successfully solve your problem(your confusion) about onOpened(..) method ( called left to right and vice versa )

    First change :

    1. custom_row.xml layout file has three button swipe_button1 to 3 , remove middle of them.

    2. Copy paste following code for remain two button :

    Remains code same for this layout.

    Second change :

    So now your accept and reject and list code is ready , now discuss about onOpened(..) method.

    Confusion solution 1- You told , your onOpend(...) method called same both left to right and vice-verse

    -> just to change following :

    public void onOpened(int position, boolean toRight) {
    
                if(toRight)
                {
    
                    //  for left to right your api calling here
                    swipelistview.closeAnimate(position);
                }
                else
                {
                    // for right to left your api calling here
                    swipelistview.closeAnimate(position);
                }
    
    
            }
    

    Confusion solution 2 when i take finger up it will must display entire listview or listview sides should retain it corners so its not working

    -> i already answered it in above

    called swipelistview.closeAnimate(position); in both if else, it hide left accept and right reject image when your your swipe left and right over.

    So final code is following :

    Entire cutom_row.xml layout

     
     
    
      
    
        

    Entire MainActivity.java

    public class MainActivity extends Activity {
    
    SwipeListView swipelistview;
    ItemAdapter adapter;
    List itemData;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        swipelistview=(SwipeListView)findViewById(R.id.example_swipe_lv_list); 
        itemData=new ArrayList();
        adapter=new ItemAdapter(this,R.layout.custom_row,itemData);
    
        swipelistview.setSwipeListViewListener(new BaseSwipeListViewListener() {
            @Override
            public void onOpened(int position, boolean toRight) {
    
                if(toRight)
                {
    
                    //  for left to right your api calling here
                    swipelistview.closeAnimate(position);
                }
                else
                {
                    // for right to left your api calling here
                    swipelistview.closeAnimate(position);
                }
    
    
            }
    
            @Override
            public void onClosed(int position, boolean fromRight) {
                // close list slide
            }
    
            @Override
            public void onListChanged() {
    
            }
    
            @Override
            public void onMove(int position, float x) {
    
    
            }
    
            @Override
            public void onStartOpen(int position, int action, boolean right) {
                Log.d("swipe", String.format("onStartOpen %d - action %d", position, action));
    
    
    
            }
    
            @Override
            public void onStartClose(int position, boolean right) {
                Log.d("swipe", String.format("onStartClose %d", position));
    
    
            }
    
            @Override
            public void onClickFrontView(int position) {
                Log.d("swipe", String.format("onClickFrontView %d", position));
    
    
    
    
            }
    
            @Override
            public void onClickBackView(int position) {
                Log.d("swipe", String.format("onClickBackView %d", position));
    
                swipelistview.closeAnimate(position);//when you touch back view it will close
    
            }
    
            @Override
            public void onDismiss(int[] reverseSortedPositions) {
    
            }
    
        });
    
        //These are the swipe listview settings. you can change these
        //setting as your requirement 
        swipelistview.setSwipeMode(SwipeListView.SWIPE_MODE_BOTH); // there are five swiping modes
        //    swipelistview.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_DISMISS); //there are four swipe actions 
        swipelistview.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);
        swipelistview.setOffsetLeft(convertDpToPixel(0f)); // left side offset
        swipelistview.setOffsetRight(convertDpToPixel(80f)); // right side offset
        swipelistview.setAnimationTime(500); // Animation time
        swipelistview.setSwipeOpenOnLongPress(true); // enable or disable SwipeOpenOnLongPress
    
        swipelistview.setAdapter(adapter);
    
    
        for(int i=0;i<10;i++)
        {
            itemData.add(new ItemRow("Swipe Item"+ i,getResources().getDrawable(R.drawable.ic_launcher) ));
    
        }
    
        adapter.notifyDataSetChanged();
    }
    
    public int convertDpToPixel(float dp) {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return (int) px;
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
      }
    
     }
    

    Other code and library remains same, It helpful to you and other also, So enjoy.

提交回复
热议问题