PullToRefreshListView增加侧滑删除功能

直接上步骤:
一:使用PullToRefreshListView实现上拉加载和下拉刷新
首先下载PullToRefreshListView:下载源码(https://github.com/chrisbanes/Android-PullToRefresh);
然后将该类库导入到项目中(导入方法就不再多说了);
xml文件中添加代码:
android:id="@+id/lv_groupconversation_message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
android:transcriptMode="normal"
android:divider="@null" />

在Activity代码中:
private PullToRefreshListView lv_message;//下拉刷新
lv_message = (PullToRefreshListView) findViewById(R.id.lv_groupconversation_message);
lv_message.setMode(PullToRefreshBase.Mode.BOTH);//设置上拉加载还是下拉刷新
lv_message.setOnRefreshListener(new RefreshForOldItemListener);//设置监听
lv_message.setAdapter(new BaseAdapter);//设置要显示的数据

// 监听下拉刷新的listener
    private class ReFreshForOldItemListener implements
            PullToRefreshBase.OnRefreshListener2<ListView> {


        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            //下拉刷新,相当于重新加载
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            //上拉加载
        }
    }

二:添加侧滑删除
listview中每一个item的xml文件:


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:descendantFocusability="blocksDescendants"//这一句必须加上,否则删除按钮点击没有反应
    android:layout_height="68dip" >

    

    <LinearLayout
        android:id="@+id/conversatinListview_back"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center|right"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/conversationlist_delete"
            android:layout_width="70dip"
            android:layout_height="60dip"
            android:gravity="center"
            android:text="删除"
            android:background="#f70c34"
            android:textColor="@color/white"
            android:textSize="15sp" />//侧滑显示出来的删除按钮
    LinearLayout>

    

    <LinearLayout
        android:id="@+id/conversatinListview_front"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:layout_gravity="center"
            android:paddingLeft="10.0dip" >

            <ImageView
                android:id="@+id/conversationItem_img"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:src="@drawable/bg_navigation"/>
        RelativeLayout>

        <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="60dip"
            android:layout_margin="10dip"
            android:layout_marginTop="15dip"
            android:layout_weight="8" >

            <TextView
                android:id="@+id/conversationItem_name"
                android:layout_width="wrap_content"
                android:layout_height="20dip"
                android:singleLine="true"
                android:layout_marginLeft="4dip"
                android:text="曙光已在我眼前:"/>
            <TextView
                android:id="@+id/atme"
                android:layout_width="wrap_content"
                android:layout_height="20dip"
                android:maxWidth="70dip"
                android:singleLine="true"
                android:ellipsize="end"
                android:textSize="15sp"
                android:text="\@了你"
                android:layout_toRightOf="@+id/conversationItem_name"
                android:layout_alignBaseline="@+id/conversationItem_name"/>
        RelativeLayout>
    LinearLayout>

FrameLayout>

在Activity的BaseAdapter中:

linearLayout = (LinearLayout) convertView.findViewById(R.id.conversatinListview_front);//获取需要滑动的item
linearLayout.setOnTouchListener(new View.OnTouchListener() {
                private Point pointDownPoint;
                private Point pointUpPoint;
                private boolean isdelete;
                boolean result = false;
                boolean isOpen = false;
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    int bottomWidth = btn_delete.getWidth();

                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            // Log.i("", "ACTION_DOWN");
                            downX = (int) event.getRawX();
                            result = true;
                            break;
                        case MotionEvent.ACTION_MOVE:
                            // Log.i("", "ACTION_MOVE");
                            // if (isAniming)
                            // break;
                            int dx = (int) (event.getRawX() - downX);
                            // Log.i("", "dy___" + dx);
                            if (isOpen) {
                                // 打开状态
                                // 向右滑动
                                if (dx > 0 && dx < bottomWidth) {
                                    v.setTranslationX(dx - bottomWidth);
                                    // 允许移动,阻止点击
                                    result = true;
                                }
                            } else {
                                // 闭合状态
                                // 向左移动
                                if (dx < 0 && Math.abs(dx) < bottomWidth) {
                                    v.setTranslationX(dx);
                                    // 允许移动,阻止点击
                                    result = true;
                                }
                            }
                            break;
                        case MotionEvent.ACTION_CANCEL:
                        case MotionEvent.ACTION_UP:

                            // Log.i("", "ACTION_UP" + v.getTranslationX());

                            // 获取已经移动的
                            float ddx = v.getTranslationX();

                            // 判断打开还是关闭

                            if (ddx <= 0 && ddx > -(bottomWidth / 2)) {
                                // 关闭
                                ObjectAnimator oa1 = ObjectAnimator.ofFloat(v, "translationX", ddx, 0).setDuration(100);
                                oa1.start();
                                oa1.addListener(new Animator.AnimatorListener() {
                                    @Override
                                    public void onAnimationStart(Animator animation) {
                                    }

                                    @Override
                                    public void onAnimationRepeat(Animator animation) {
                                    }

                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        isOpen = false;
                                        result = false;
                                    }

                                    @Override
                                    public void onAnimationCancel(Animator animation) {
                                        isOpen = false;
                                        result = false;
                                    }
                                });

                            }
                            if (ddx <= -(bottomWidth / 2) && ddx > -bottomWidth) {
                                // 打开
                                ObjectAnimator oa1 = ObjectAnimator.ofFloat(v, "translationX", ddx, -bottomWidth)
                                        .setDuration(100);
                                oa1.start();
                                result = true;
                                isOpen = true;
                            }
                            break;
                    }
                    return result;
                }
            });

你可能感兴趣的:(android)