Android recycleview实现左右切换时的条目滑动效果,条目是固定的,不让滑动,在代码里面有个滑动开关,自己设置即可,需求再花里胡哨,也总是有解决的办法

首先导入Recycleview依赖包

    implementation 'com.android.support:recyclerview-v7:24.2.1'

这个点击左边当前条目就会从左到右滚动至前一条数据,(右边就是从右边滚到左边),需要的就赶紧来,记得关注

Android recycleview实现左右切换时的条目滑动效果,条目是固定的,不让滑动,在代码里面有个滑动开关,自己设置即可,需求再花里胡哨,也总是有解决的办法_第1张图片

my_activity.xml




    


        

        

        

            

                

                

            

            

                

                
            
        

    


item_home.xml




    

        

            

            

        

        

            
        


    



HomeAdapter.class

package android_robot.com.example.luping.testadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import android_robot.com.example.luping.R;

/**
 * Created by Administrator on 2019/11/1.
 */

public class HomeAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList list;

    public HomeAdapter(Context context, ArrayList list) {
        this.context=context;
        this.list=list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.item_home, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //  添加数据
    public void addData(int position) {
        // 只有加上这句话,点击的时候才会让条目动起来
        notifyItemChanged(position);
    }
    //  删除数据
    public void removeData(int position) {
        //   同上,刷新当前条目数据
        notifyItemChanged(position);
    }

    /**
     * ViewHolder的类,用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        public MyViewHolder(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.time);
        }
    }
}

禁止条目滑动的工具类

CustomLinearLayoutManager
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;

/**
 * Created by Administrator on 2019/11/1.
 */


public class CustomLinearLayoutManager extends LinearLayoutManager {
    private boolean isScrollEnabled = true;

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

    public void setScrollEnabled(boolean flag) {
        this.isScrollEnabled = flag;
    }

    /**
     * 禁止滑动
     * canScrollHorizontally(禁止横向滑动)
     * @return
     */
    @Override
    public boolean canScrollHorizontally() {
        return isScrollEnabled && super.canScrollVertically();
    }
    /**
     * 禁止滑动
     * canScrollVertically(禁止竖向滑动)
     * @return
     */
    @Override
    public boolean canScrollVertically() {
        return isScrollEnabled && super.canScrollVertically();
    }
}

 

MyActivity

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
import android_robot.com.example.luping.R;
import android_robot.com.example.luping.adapter.CustomLinearLayoutManager;
import android_robot.com.example.luping.utils.ToastUtil;

public class MyActivity extends Activity {

    private RecyclerView mRecyclerView;
    HomeAdapter honmeAdapter = null;
    private ArrayList datas;
    private ImageView mImg_right;
    private ImageView mImg_left;
    private View mView_right, mView_left;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        mImg_right = findViewById(R.id.mImg_right);
        mImg_left = findViewById(R.id.mImg_left);
        mView_left = findViewById(R.id.mView_left);
        mView_right = findViewById(R.id.mView_right);
        mView_left.setVisibility(View.GONE);
        mImg_left.setVisibility(View.GONE);
//      初始化RecyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        //禁止滑动
        CustomLinearLayoutManager customLinearLayoutManager=new CustomLinearLayoutManager(this);
        customLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //滑动开关
        customLinearLayoutManager.setScrollEnabled(false);
        mRecyclerView.setLayoutManager(customLinearLayoutManager);
//      获取数据,向适配器传数据,绑定适配器
        datas = initData();
        honmeAdapter = new HomeAdapter(MyActivity.this, datas);
        mRecyclerView.setAdapter(honmeAdapter);
//      添加动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        myOnClick();
    }

    private int index = 0;//控制跳转页的标识
    private void myOnClick() {
        mImg_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (index < datas.size() - 1) {
                    index++;
                    Log.d("aaaa", "indext++=" + index);
                    honmeAdapter.addData(index);
                    mRecyclerView.scrollToPosition(index);
                    mView_left.setVisibility(View.VISIBLE);
                    mImg_left.setVisibility(View.VISIBLE);
                } else {
                    ToastUtil.show(MyActivity.this, "已经到底了");
                }
                if (index + 1 == datas.size()) {
                    mView_right.setVisibility(View.GONE);
                    mImg_right.setVisibility(View.GONE);
                }

            }
        });
        mImg_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (index > 0) {
                    index--;
                    Log.d("aaaa", "indext--=" + index);
                    honmeAdapter.removeData(index);
                    mRecyclerView.scrollToPosition(index);
                    mView_right.setVisibility(View.VISIBLE);
                    mImg_right.setVisibility(View.VISIBLE);
                } else {
                    ToastUtil.show(MyActivity.this, "已经到底了");
                }
                if (index == 0) {
                    mView_left.setVisibility(View.GONE);
                    mImg_left.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 编写一套假数据
     */
    protected ArrayList initData() {
        ArrayList mDatas = new ArrayList();
        for (int i = 0; i < 20; i++) {
            mDatas.add("我是条目" + i);
        }
        return mDatas;
    }
}

 

你可能感兴趣的:(android,Android,博客nazicsdn订阅)