防QQ可以在任何地方横向滑动的一个view

这里以仿QQ的聊天记录item左划删除为例,载体是recyclerview,你也可以换成listview.
我想的这个办法可能比较笨。再加上参考网上的一些大佬们的思路,大概思路是自定义一个HorizontalScrollView,通过监听手势的滑动来判断。
感兴趣的可以去我的gayhub上下载,gayhub
废话不多说直接上代码。

CustomHorizontalScrollView.java

package com.robin.SlideItem;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;


public class CustomHorizontalScrollView extends HorizontalScrollView {
    private static final String TAG = "ScrollListViewItem";
    private static int ScreenWidth = 0;
    private static int MenuWidth = 0;
    private static int HalfMenuWidth = 0;
    private boolean turnLeft = false;
    private boolean turnRight = false;
    private boolean once;

    ViewGroup left;
    ViewGroup centre;
    ViewGroup right;

    public CustomHorizontalScrollView(Context context) {
        super(context);
    }

    public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!once) {
            Log.i(TAG, "onMeasure: getChildCount==" + getChildCount());//这个地方要注意一下子view的数量和布局中的数量
            LinearLayout linearLayout = (LinearLayout) getChildAt(0);
            left = (ViewGroup) linearLayout.getChildAt(0);
            centre = (ViewGroup) linearLayout.getChildAt(1);
            right = (ViewGroup) linearLayout.getChildAt(2);

            ScreenWidth = getScreenWidth(getContext());
            MenuWidth = ScreenWidth / 4;
            HalfMenuWidth = MenuWidth / 2;

            if (left != null) {
                left.getLayoutParams().width = MenuWidth;
            }
            if (centre != null) {
                centre.getLayoutParams().width = ScreenWidth;
            }
            if (right != null) {
                right.getLayoutParams().width = MenuWidth;
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            this.scrollTo(MenuWidth, 0);
            once = true;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                //向左滑
                if (turnLeft) {
                    if (getScrollX() < HalfMenuWidth) {
                        //滑到左最末尾
                        this.scrollTo(0, 0);
                    } else if (getScrollX() > HalfMenuWidth && getScrollX() < MenuWidth + HalfMenuWidth) {
                        //滑到中间
                        this.scrollTo(MenuWidth, 0);
                    } else {
                        this.scrollTo(MenuWidth * 2, 0);
                    }
                }
                //向右滑
                if (turnRight) {
                    if (getScrollX() > MenuWidth + HalfMenuWidth) {
                        //滑到最右
                        this.scrollTo(MenuWidth + MenuWidth, 0);
                    } else if (getScrollX() > HalfMenuWidth && getScrollX() < MenuWidth + HalfMenuWidth) {
                        //滑到中间
                        this.scrollTo(MenuWidth, 0);
                    } else {
                        //滑到最左
                        this.scrollTo(0, 0);
                    }
                }
                return true;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (l > MenuWidth) {
            turnLeft = false;
            turnRight = true;
        } else {
            turnLeft = true;
            turnRight = false;
        }
    }
}

MyModel

package com.robin.SlideItem;

/**
 * Created by robin on 2016/3/2.
 */
public class MyModel {

    private String left;
    private String content;
    private String right;

    public MyModel(String left, String content, String right) {
        this.left = left;
        this.content = content;
        this.right = right;
    }

    public String getContent() {
        return content;
    }

    public String getRight() {
        return right;
    }

    public String getLeft() {
        return left;
    }

    public void setRight(String right) {
        this.right = right;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setLeft(String left) {
        this.left = left;
    }
}

MainActivity.java

package com.robin.SlideItem;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private List<MyModel> listmodel=null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listmodel=new ArrayList<MyModel>();
        initData();
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_demo);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        ((SimpleItemAnimator)mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);//更新的时候防止闪动
        mRecyclerView.setAdapter(new MyAdapter(listmodel,this));

    }

    public void initData(){
        MyModel m=null;

        for(int i=1;i<=20;i++){
            m=new MyModel("左边"+i,"这是内容"+i,"删除"+i);
            listmodel.add(m);
        }
    }
    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        private static final String TAG = "MyAdapter";
        private List<MyModel> model;
        private Context context;

        public MyAdapter(List<MyModel> model,Context context) {
            this.model = model;
            this.context = context;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item,null);
            return new ModelHolder(mView);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            if(holder instanceof ModelHolder){
                ((ModelHolder)holder).left.setText(model.get(position).getLeft());
                ((ModelHolder)holder).content.setText(model.get(position).getContent());
                ((ModelHolder)holder).right.setText(model.get(position).getRight());
                ((ModelHolder)holder).right.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        notifyItemRemoved(position);
                        model.remove(position);
                        notifyItemRangeChanged(position,model.size());
                        Log.i(TAG, "onClick: position=="+position+"----------------model.size=="+model.size());
                    }
                });

            }
        }

        @Override
        public int getItemCount() {
            return model.size();
        }
        class ModelHolder extends RecyclerView.ViewHolder{

            public TextView left;
            public TextView content;
            public TextView right;
            public ModelHolder(View itemView) {
                super(itemView);
                left=(TextView)itemView.findViewById(R.id.leftbutton);
                content=(TextView)itemView.findViewById(R.id.contentview);
                right=(TextView)itemView.findViewById(R.id.rightbutton);
            }
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.robin.SlideItem.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_demo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></android.support.v7.widget.RecyclerView>
</RelativeLayout>

listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<com.robin.SlideItem.CustomHorizontalScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include layout="@layout/leftbutton"/>
        <include layout="@layout/context_view"/>
        <include layout="@layout/rightbutton"/>
    </LinearLayout>
</com.robin.SlideItem.CustomHorizontalScrollView>

leftbutton.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorPrimaryDark">
    <TextView
        android:id="@+id/leftbutton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="leftbutton"/>
</RelativeLayout>

content_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorPrimary"
    android:layout_marginBottom="10dp">

    <TextView
        android:id="@+id/contentview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="context_view"/>

</RelativeLayout>

rightbutton.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorAccent">
    <TextView
        android:id="@+id/rightbutton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="rightbutton"/>
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值