【问题标题】:Android list view swipe right to reveal buttonAndroid列表视图向右滑动以显示按钮
【发布时间】:2017-05-12 11:26:43
【问题描述】:

我想在 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;
}
}

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

有什么想法吗?提前致谢!

【问题讨论】:

    标签: java android listview animation


    【解决方案1】:

    你可以使用这个库AndroidSwipeLayout.它有以下类型的动画:

    如果您想自己编程,请添加以下课程。我已经在其中一个示例应用程序中实现了这一点。我只在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 变量的值来检测小滑动。

    希望对你有帮助:)

    【讨论】:

    • 但是有什么方法可以实现它而无需从头开始重新编码我的代码?因为我看到它使用的是 SwipeLayout 而不是 LinearLayout,我担心结构可能不同
    • 酷我会尽快尝试并明天再次更新你!谢谢!
    • 嘿抱歉,我应该为“INSERT_CONDITIONS_HERE”添加什么。还有,怎么称呼?
    • 检测滑动的条件。即触摸是否滑动。我正在编辑我的答案。对于调用部分,我已经添加了。 @DeniseTan
    【解决方案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 - 你的适配器

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

    【讨论】:

    • 酷我会试试看,明天再更新你。谢谢!
    【解决方案3】:

    您可以为此内部列表视图使用查看寻呼机,因为这里的项目是代码:

    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>
    

    查看pager item第一行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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 2014-01-14
      相关资源
      最近更新 更多