RecyclerView获取滑动距离

RecyclerView获取滑动距离_第1张图片

1.普通的recyclerview布局



    
    
    
    
    
    



2.普通的适配器

package com.myapplication.adapter;

import android.animation.ObjectAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.myapplication.R;

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

/**
 * 作者: Nathans'Liu
 * 邮箱: [email protected]
 * 时间: 2017/12/26 15:47
 * 描述:
 */

public class ItemDifferAdapter extends RecyclerView.Adapter {
    private Random mRandom = new Random();
    private List mBeanList = new ArrayList<>();


    @Override
    public LinearLayoutHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        for (int i = 0; i < 20; i++) {
            if (mRandom.nextBoolean()) {
                mBeanList.add(R.mipmap.ic_tk);
            } else {
                mBeanList.add(R.mipmap.ic_mm);
            }
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false);
        return new LinearLayoutHolder(view);
    }

    @Override
    public void onBindViewHolder(LinearLayoutHolder holder, int position) {
        //加个动画
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(holder.itemView, "scaleY", 0.5f, 1f);
        scaleX.start();
        holder.mImageView.setImageResource(mBeanList.get(position));
        holder.mTextView.setText(position + "");
    }

    @Override
    public int getItemCount() {
        return 20;
    }


    class LinearLayoutHolder extends RecyclerView.ViewHolder {
        private ImageView mImageView;
        private TextView mTextView;

        public LinearLayoutHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.iv);
            mTextView = itemView.findViewById(R.id.tv_position);
        }
    }
}

3.计算Recyclerview滑动距离

package com.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.myapplication.adapter.ItemDifferAdapter;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 作者: Nathans'Liu
 * 邮箱: [email protected]
 * 时间: 2017/12/26 15:38
 * 描述:
 */

public class MainActivity extends AppCompatActivity {
    @InjectView(R.id.tv_1)
    TextView mTv1;
    @InjectView(R.id.rv)
    RecyclerView mRecyclerView;
    @InjectView(R.id.tv_3)
    TextView mTv3;
    @InjectView(R.id.tv_4)
    TextView mTv4;
    @InjectView(R.id.tv_5)
    TextView mTv5;
    @InjectView(R.id.tv_6)
    TextView mTv6;
    LinearLayoutManager linearLayoutManager;
    private ItemDifferAdapter mDifferAdapter;

    private int itemH,itemW;
    //存放item宽或高
    private Map mMapList = new HashMap<>();
    private int iposition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        initView();
    }

    private void initView() {
        DisplayMetrics dm = new DisplayMetrics();
        //取得窗口属性
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        //窗口的宽度
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        mTv1.setText("屏幕宽度:" + screenWidth + "    " + "屏幕高度:" + screenHeight);
        mDifferAdapter = new ItemDifferAdapter();
        initRecyclerVH_(LinearLayout.VERTICAL);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                mHandler.sendEmptyMessage(1);
            }
        });
    }

    public void initRecyclerVH_(int orientation) {
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(orientation);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mDifferAdapter);
        mDifferAdapter.notifyDataSetChanged();
    }

    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            mTv6.setText("当前ITEM宽:" + itemW + "" + "   " + "高:" + itemH + "");
            mTv3.setText("首可见位置:" + linearLayoutManager.findFirstVisibleItemPosition());
            mTv4.setText("尾可见位置:" + linearLayoutManager.findLastVisibleItemPosition());
            mTv5.setText("滚动距离:" + unlikeVertical());
            return true;
        }
    });

    /**
     * 不同Item VERTICAL
     */
    public int unlikeVertical() {
        int itemWH = 0;
        int itemTR = 0;
        int distance = 0;
        int position = linearLayoutManager.findFirstVisibleItemPosition();
        View firstVisiableChildView = linearLayoutManager.findViewByPosition(position);
        //判断是横着还是竖着,得出宽或高
        itemWH = firstVisiableChildView.getHeight();
        //一层判断mMapList是否为空,若不为空则根据键判断保证不会重复存入
        if (mMapList.size() == 0) {
            mMapList.put(position, itemWH);
        } else {
            if (!mMapList.containsKey(position)) {
                mMapList.put(position, itemWH);
                Log.d("poi", mMapList + "");
            }
        }
        //判断是横着还是竖着,得出未滑出屏幕的距离
        itemTR = firstVisiableChildView.getTop();
        //position为动态获取,目前屏幕Item位置
        for (int i = 0; i < position; i++) {
            //iposition移出屏幕的距离
            iposition = iposition + mMapList.get(i);
        }
        //根据类型拿iposition减未移出Item部分距离,最后得出滑动距离
        distance = iposition - itemTR;
        //item宽高
        itemW = firstVisiableChildView.getWidth();
        itemH = firstVisiableChildView.getHeight();
        //归零
        iposition = 0;
        return distance;
    }


}

 

你可能感兴趣的:(RecyclerView获取滑动距离)