Android列表视图向右滑动以显示按钮

时间:2017-05-12 11:26:44

标签: java android listview animation

我希望实现类似向右滑动以显示Android开发中每个列表视图行上的按钮。

我有一个完整工作的列表视图,可以动态获取和填充数据:

public class ProfilePastEvent extends Fragment{
View pastEventView;
Context context;
private ListView listview;
private ListAdapter mAdapter;
public ArrayList<Event> _eventlist = new ArrayList<Event>();
EventController eventCtrl = new EventController();

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

    listview = (ListView) pastEventView
            .findViewById(R.id.pastEventListview);

    mAdapter = new ListAdapter(getActivity());
    listview.setAdapter(mAdapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View item,
                int position, long id) {

    });
    return pastEventView;
}

private class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ViewHolder viewHolder;

    public ListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return _eventlist.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = inflater.inflate(
                    R.layout.profile_past_listview_row, null);
            viewHolder = new ViewHolder();

            viewHolder.txt_dpastEventName = (TextView) convertView
                    .findViewById(R.id.txtPastEventName);


            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txt_dpastEventName.setText(_eventlist.get(position)
                .getEventName().trim());


        return convertView;
    }
}

private class ViewHolder {
    TextView txt_dpastEventName;
}
}

但是,此列表视图的工作方式类似于用户单击某行以打开新意图。我不确定如何实现向右滑动以显示“查看更多”或“删除”按钮功能。

有什么想法吗?先谢谢!

3 个答案:

答案 0 :(得分:2)

您可以使用此库AndroidSwipeLayout.它具有以下类型的动画:

enter image description here

如果您想自己编程,请添加以下课程。我在其中一个示例应用程序中实现了这一点。我只在stackoverflow上发现了这个,但是不记得从哪里来。

public class MyGestureDetector extends SimpleOnGestureListener {
private ListView list;

public MyGestureDetector(ListView list) {
    this.list = list;
}

//CONDITIONS ARE TYPICALLY VELOCITY OR DISTANCE    
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (INSERT_CONDITIONS_HERE)
        if (showDeleteButton(e1))
            return true;
    return super.onFling(e1, e2, velocityX, velocityY);
}

private boolean showDeleteButton(MotionEvent e1) {
    int pos = list.pointToPosition((int)e1.getX(), (int)e1.getY());
    return showDeleteButton(pos);
}

private boolean showDeleteButton(int pos) {
    View child = list.getChildAt(pos);
    if (child != null){
        Button delete = (Button) child.findViewById(R.id.delete_button_id);
        if (delete != null)
            if (delete.getVisibility() == View.INVISIBLE)
                delete.setVisibility(View.VISIBLE);
            else
                delete.setVisibility(View.INVISIBLE);
        return true;
    }
    return false;
}
}

并按以下方式调用此类:

MyGestureDetector(YOUR_LIST);

编辑:

要检测滑动,请使用以下条件:

private static final int SWIPE_MIN_DISTANCE = 80;
private static final int SWIPE_THRESHOLD_VELOCITY = 50;
private static final int PEOPLE_FRAGMENT = 0;
private static final int PLACES_FRAGMENT = 2;

向上,向下,向右和向左滑动的条件是:(在您的情况下,只需要一个,无论是右侧还是左侧)。

  if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
    Log.d("SWIPE", "right to left");
    return true; //Right to left
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
    Log.d("SWIPE", "left to right");
    return true; //Left to right 
}

//This will test for up and down movement. 
if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
    return false; //Bottom to top
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
    return false; //Top to bottom 
}

现在从右到左或从左到右添加showDeleteButton()。另请注意,您还可以通过更改上面给出的final变量的值来检测小滑动。

希望这会对你有所帮助:)。

答案 1 :(得分:2)

希望这会对你有帮助,

public class OnSwipeTouchListener implements View.OnTouchListener {


    private final String TAG = OnSwipeTouchListener.class.getSimpleName();
    ListView list;
    private GestureDetector gestureDetector;
    private Context context;


    public OnSwipeTouchListener(Context ctx, ListView list) {
        gestureDetector = new GestureDetector(ctx, new GestureListener());
        context = ctx;
        this.list = list;
    }

    public OnSwipeTouchListener() {
        super();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    public void onSwipeRight(int pos) {
        //Do what you want after swiping left to right

        Log.e(TAG, "onSwipeRight: " + pos);

        mShopAdapter.swipeRight(pos);
    }

    public void onSwipeLeft(int pos) {

        //Do what you want after swiping right to left
        Log.e(TAG, "onSwipeLeft: " + pos);
        mShopAdapter.swipeLeft(pos);
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        private int getPostion(MotionEvent e1) {
            return list.pointToPosition((int) e1.getX(), (int) e1.getY());
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2,
                               float velocityX, float velocityY) {
            float distanceX = e2.getX() - e1.getX();
            float distanceY = e2.getY() - e1.getY();
            if (Math.abs(distanceX) > Math.abs(distanceY)
                    && Math.abs(distanceX) > SWIPE_THRESHOLD
                    && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (distanceX > 0)
                    onSwipeRight(getPostion(e1));
                else
                    onSwipeLeft(getPostion(e1));
                return true;
            }
            return false;
        }

    }


}

然后附加到ListView

 mLstShopList.setOnTouchListener(new OnSwipeTouchListener(getActivity(),
                mLstShopList));

-mShopAdapter - 您的适配器

您将在适配器内左右滑动手势。

答案 2 :(得分:0)

您可以在此内部列表视图中使用View寻呼机,因为此处的项目是代码:

Activity.class:

            import android.content.Context;
            import android.os.Bundle;
            import android.support.v4.view.PagerAdapter;
            import android.support.v4.view.ViewPager;
            import android.support.v7.app.AppCompatActivity;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import android.widget.BaseAdapter;
            import android.widget.ListView;


            public class TestActivity extends AppCompatActivity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_l);
                    ListView listview = (ListView)findViewById(R.id.my_list);
                    listview.setAdapter(new ListAdapter(this));
                }

                private class ListAdapter extends BaseAdapter {
                    LayoutInflater inflater;
                    ViewHolder viewHolder;

                    public ListAdapter(Context context) {
                        inflater = LayoutInflater.from(context);
                    }

                    public int getCount() {
                        return 10;
                    }

                    public Object getItem(int position) {
                        return position;
                    }

                    public long getItemId(int position) {
                        return position;
                    }

                    public View getView(int position, View convertView, ViewGroup parent) {
                        if (convertView == null) {

                            convertView = inflater.inflate(
                                    R.layout.list_item_view_pager, null);
                            viewHolder = new ViewHolder();

                            viewHolder.lst_vp = (ViewPager) convertView
                                    .findViewById(R.id.lst_vp);


                            convertView.setTag(viewHolder);
                        } else {
                            viewHolder = (ViewHolder) convertView.getTag();
                        }

                        viewHolder.lst_vp.setAdapter(new ViewPagerAdapter(TestActivity.this));



                        return convertView;
                    }
                }

                private class ViewHolder {
                    ViewPager lst_vp;
                }

                class ViewPagerAdapter extends PagerAdapter {

                    Context mContext;
                    LayoutInflater mLayoutInflater;

                    public ViewPagerAdapter(Context context) {
                        mContext = context;
                        mLayoutInflater = LayoutInflater.from(mContext);
                    }


                    @Override
                    public int getCount() {
                        return 2;
                    }

                    @Override
                    public boolean isViewFromObject(View view, Object object) {
                        return view == object;
                    }

                    // This method should create the page for the given position passed to it as an argument.
                    @Override
                    public Object instantiateItem(ViewGroup container, int position) {
                        View view =   null;
                        if(position==0) {
                            view = mLayoutInflater.inflate(R.layout.first_row, container, false);
                        }else{
                            view = mLayoutInflater.inflate(R.layout.second_row, container, false);
                        }

                        container.addView(view);
                        return view;
                    }

                    // Removes the page from the container for the given position.
                    @Override
                    public void destroyItem(ViewGroup container, int position, Object object) {
                        container.removeView((View) object);
                    }

                    @Override
                    public float getPageWidth(int position) {
                        if (position != 0 ) {
                            return 0.3f;
                        }
                        return 1f;
                    }
                }
            }

活动xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:layout_width="match_parent"
            android:id="@+id/my_list"
            android:layout_height="match_parent">

        </ListView>

    </LinearLayout>

列出项目xml:

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v4.view.ViewPager
                android:id="@+id/lst_vp"
                android:layout_width="match_parent"
                android:layout_height="196dp">
            </android.support.v4.view.ViewPager>

        </LinearLayout>

查看寻呼机项目第一行xml:

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="196dp">
            <TextView
                android:layout_width="match_parent"
                android:text="First item "
                android:padding="20dp"
                android:gravity="center"
                android:background="@color/colorAccent"
                android:layout_height="match_parent" />
        </LinearLayout>

第二行xml:

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="196dp">
          <TextView
                android:layout_width="match_parent"
                android:text="Delete "
                android:padding="20dp"
                android:gravity="center"
                android:background="@color/colorPrimaryDark"
                android:layout_height="match_parent" />

        </LinearLayout>