首先导入Recycleview依赖包
implementation 'com.android.support:recyclerview-v7:24.2.1'
这个点击左边当前条目就会从左到右滚动至前一条数据,(右边就是从右边滚到左边),需要的就赶紧来,记得关注
my_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EDEDED"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/x73">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="@dimen/x73">
</android.support.v7.widget.RecyclerView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="60dp"
android:layout_height="match_parent">
<View
android:id="@+id/mView_left"
android:layout_width="@dimen/x13"
android:layout_height="@dimen/x60"
android:layout_centerVertical="true"
android:background="#FFFFFF"></View>
<ImageView
android:id="@+id/mImg_left"
android:layout_width="@dimen/x33"
android:layout_height="@dimen/x33"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/x6"
android:src="@mipmap/left" />
</RelativeLayout>
<RelativeLayout
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
>
<View
android:id="@+id/mView_right"
android:layout_width="@dimen/x13"
android:layout_height="@dimen/x60"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:background="#FFFFFF"></View>
<ImageView
android:id="@+id/mImg_right"
android:layout_width="@dimen/x33"
android:layout_height="@dimen/x33"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/x6"
android:src="@mipmap/right" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
item_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="@dimen/x313"
android:layout_height="@dimen/x73"
android:background="#73BBF7"
android:orientation="horizontal"
android:layout_marginLeft="@dimen/x23"
android:layout_marginRight="@dimen/x23"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/x46"
android:layout_height="@dimen/x46"
android:layout_marginLeft="@dimen/x26"
android:layout_marginTop="@dimen/x13"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="@dimen/x18"
android:layout_marginTop="@dimen/x20"
android:text="66"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="@dimen/x44"
android:layout_marginTop="@dimen/x20"
android:gravity="center"
android:text="66"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
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<HomeAdapter.MyViewHolder> {
private Context context;
private ArrayList<String> list;
public HomeAdapter(Context context, ArrayList<String> 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<String> 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<String> initData() {
ArrayList<String> mDatas = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
mDatas.add("我是条目" + i);
}
return mDatas;
}
}