Android的ListView的左右滑动,以接受和拒绝 [英] Android ListView Swipe Right and Left to Accept and Reject

查看:368
本文介绍了Android的ListView的左右滑动,以接受和拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发在该列表视图中我想,当我向左或向右滑动,在左上角显示其中一个接受(真)图标(非点击 - 表明,当从左向右轻扫只是颜色变化),如下面的屏幕截图,

I want to develop list view in that i want , when i swipe left to right , in left corner display one accept ( true ) icon ( non clickable - show just color change when swipe left to right ) like following screen shot,

当我刷卡都从左至右它接受(在我打电话接受API)
当我从右向左滑动会显示这个样子,

when i swipe all left to right it accept ( in that i called accept api ) , and when i swipe right to left it will display like this,

这在做iPhone,但我不完全找到了如何在Android的做到这一点,
我这么多的谷歌搜索,但没有发现正是我想要的,

This done in iphone , but i not exactly find how to do this in android , i so much googling but not find exactly that i want,

我尝试下面的例子: http://www.tutecentral.com/android-swipe-列表视图/

但是,当我向左或向右滑动,从右到左相同onOpened(..)方法调用,因此混乱:何时调用接受和拒绝API,因为所谓的相同方法时,任何类型的刷卡

but in that when i swipe left to right and right to left same onOpened(..) method called so it confusing to : when to call accept and reject api because same method called when any type of swipe.

和我也想,当我刚刚刷卡左或右,只留下那个时候接受图片(左侧)和拒绝(拒绝像右侧)显示,当我把手指就必须显示整个列表视图(不两侧的图像)。

and i also want when i just swipe left-right or right-left only that time accept image(left side) and reject ( reject image right side ) display, when i take finger up it will must display entire listview ( not both side image ).

所以,任何一个请帮我如何做到这一点。

So any one please help me to How to do this.

我的问题是有些混乱,也与一些低语法,但我donw知道该怎么解释整个动画,所以我尝试写上面一样,

My question is some confusing and also with some low grammar but i donw know how to explain entire animation so i try to write like above,

如果任何一个可以帮助我很AP preciated。

If any one help me it very appreciated.

推荐答案

解决方案数1:

您需要做的像以下,达到接近的功能,

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

包装你的ListView的适配器

像下面这样:

  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<String> stringAdapter = new ArrayAdapter<String>(
        this,
        R.layout.row_bg,
        R.id.text,
        new ArrayList<String>(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);
  }

创建每个刷卡方向的背景布置

像下面这样:

  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<String> stringAdapter = new ArrayAdapter<String>(
        this,
        R.layout.row_bg,
        R.id.text,
        new ArrayList<String>(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);
}

您得到了来自这里既有库和示例:
https://github.com/wdullaer/SwipeActionAdapter

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.

解决方案2号:

解决方案2是关于你用code更改一些code:的 http://www.tutecentral.com/android-swipe-listview/

我曾尝试你的code从链接 HTTP://www.tutecentral。 COM / Android的刷卡-的ListView / ,并改变一些code,并成功地解决你的问题(你的困惑)约onOpened(..)方法(称为左到右,反之亦然)

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 )

第一个变化:


  1. custom_row.xml布局文件有三个按钮swipe_button1 3,
    删除它们中间。

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

复制粘贴以下code为保持两扣:

Copy paste following code for remain two button :

<Button
    android:id="@+id/swipe_button1"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/your_accept_image" />

<Button
    android:id="@+id/swipe_button3"
    style="@style/MyListButtonAction"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/your_reject_image" />

遗迹code同此布局。

Remains code same for this layout.

第二个变化:

所以,现在你接受和拒绝,并列出code是准备好了,现在大概onOpened(..)方法。

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

混乱液1 - 你叫所谓的一样,你onOpend(...)方法既左至右和副诗句

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);
            }


        }

混乱溶液2 当我把手指就必须显示整个列表视图或ListView双方应妥善保存弯道所以它不工作

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

名为 swipelistview.closeAnimate(位置); 在这两个如果别的,它隐藏左侧接受和拒绝权图像时离开你你刷卡而就在

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

所以最终code为以下内容:

So final code is following :

整个cutom_row.xml布局

Entire cutom_row.xml layout

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" >

  <RelativeLayout
    android:id="@+id/back"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="back" >

    <Button
        android:id="@+id/swipe_button1"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/accept_image" />

    <Button
        android:id="@+id/swipe_button3"
        style="@style/MyListButtonAction"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/reject_image" />
</RelativeLayout>


<RelativeLayout
    android:id="@+id/front"
    style="@style/MyListFrontContent"
    android:orientation="vertical"
    android:tag="front" >

    <ImageView
        android:id="@+id/example_image"
        style="@style/MyListImage" />

    <TextView
        android:id="@+id/example_itemname"
        style="@style/MyListTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/example_image" />
  </RelativeLayout>

</FrameLayout>

整个MainActivity.java

Entire MainActivity.java

public class MainActivity extends Activity {

SwipeListView swipelistview;
ItemAdapter adapter;
List<ItemRow> 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<ItemRow>();
    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;
  }

 }

其他code和库保持不变,这有助于你和其他也因此享受。

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

这篇关于Android的ListView的左右滑动,以接受和拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆