欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android使用Recyclerview实现图片水平自动循环滚动效果

时间:2020-11-17 01:10:07|栏目:Android代码|点击:

简介:

本篇博客主要介绍的是如何使用RecyclerView实现图片水平方向自动循环(跑马灯效果)

效果图:

思路:

1.准备m张图片

1.使用Recyclerview实现,返回无数个(实际Interge.MAXVALUE)item,第n个item显示第n%m张图片

3.使用recyclerview.scrollBy  每个一段时间水平滚动一段距离

4.通过layoutManager.findFirstVisibleItemPosition()获取当前显示的第一个View是第几个item,上面的ImageView显示对应de图片

实现代码:

XML文件

1.activity布局文件activity_recy.xml  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <ImageView
  android:layout_width="300dp"
  android:layout_height="350dp"
  android:layout_gravity="center_horizontal"
  android:id="@+id/img"
  android:src="@drawable/p5"
  android:scaleType="fitXY"
  />

  <android.support.v7.widget.RecyclerView
   android:layout_width="match_parent"
   android:layout_height="150dp"
   android:layout_marginTop="10dp"
   android:id="@+id/recyclerview"
   ></android.support.v7.widget.RecyclerView>



</LinearLayout>

2.适配器布局item_horizon.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:orientation="vertical"

 >
 <ImageView
  android:layout_width="100dp"
  android:layout_height="match_parent"
  android:id="@+id/img"
  android:layout_marginLeft="5dp"
  android:layout_marginRight="5dp"
  android:scaleType="fitXY"
  />

</LinearLayout>

Activity

public class HorizontalActivity extends AppCompatActivity implements RecyAdapter.OnItemClickListener {
 private String TAG="HorizontalActivity";

 @BindView(R.id.img)
 ImageView img;
 @BindView(R.id.recyclerview)
 RecyclerView recyclerview;


 private Integer[] mImgIds = {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5,
   R.drawable.pic1, R.drawable.pic5, R.drawable.pic6};
 private List<Integer> datas;
 private RecyAdapter recyAdapter;
 private Handler mHandler=new Handler();
 private LinearLayoutManager layoutManager;

 private int oldItem=0;


 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_recy);
  ButterKnife.bind(this);
  initData();
  initRecy();
  img.setImageResource(datas.get(0));
  recyAdapter.setOnItemClickListener(this);
 }


 Runnable scrollRunnable=new Runnable() {
  @Override
  public void run() {
   recyclerview.scrollBy(3,0);

//   int firstItem = layoutManager.findFirstVisibleItemPosition();
   int firstItem=layoutManager.findFirstVisibleItemPosition();
   if(firstItem!=oldItem&&firstItem>0){
    oldItem=firstItem;
    img.setImageResource(datas.get(oldItem%datas.size()));
   }

   Log.e(TAG, "run: firstItem:"+firstItem );


   mHandler.postDelayed(scrollRunnable,10);
  }
 };

 private void initRecy() {
  recyAdapter=new RecyAdapter(this,datas);

  layoutManager = new LinearLayoutManager(this);
  layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
  recyclerview.setLayoutManager(layoutManager);
  recyclerview.setAdapter(recyAdapter);
 }

 private void initData() {
  datas=new ArrayList<>();
  for (int i = 0; i <mImgIds.length ; i++) {
   datas.add(mImgIds[i]);
  }
 }

 @Override
 protected void onResume() {
  super.onResume();
  mHandler.postDelayed(scrollRunnable,10);
 }

 @Override
 protected void onStop() {
  super.onStop();
  mHandler.removeCallbacks(scrollRunnable);
 }

 @Override
 public void onItemClick(View view, int tag) {
  Toast.makeText(this,"第"+tag+"张图片被点击了",Toast.LENGTH_SHORT).show();
 }
}

适配器RecyAdapter

public class RecyAdapter extends RecyclerView.Adapter<RecyAdapter.ViewHolder> implements View.OnClickListener {

 private Context context;
 private List<Integer> datas;
 private OnItemClickListener onItemClickListener;

 public RecyAdapter(Context context, List<Integer> datas) {
  this.context = context;
  this.datas = datas;
 }

 @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  View view = LayoutInflater.from(context).inflate(R.layout.item_horizontal, parent, false);
  ViewHolder vh=new ViewHolder(view);
  view.setOnClickListener(this);
  return vh;
 }


 @Override
 public void onBindViewHolder(ViewHolder holder, int position) {
  int newPos=position%datas.size();

  holder.img.setImageResource(datas.get(newPos));

  holder.itemView.setTag(position);


 }

 @Override
 public int getItemCount() {
  return Integer.MAX_VALUE;
 }

 @Override
 public void onClick(View view) {
  if(onItemClickListener!=null){
   onItemClickListener.onItemClick(view, (Integer) view.getTag());
  }
 }


 class ViewHolder extends RecyclerView.ViewHolder {
   ImageView img;
  public ViewHolder(View itemView) {
   super(itemView);
   img=itemView.findViewById(R.id.img);
  }
 }

 public void setOnItemClickListener(OnItemClickListener listener){
  this.onItemClickListener=listener;

 }
 interface OnItemClickListener{
  void onItemClick(View view,int tag);
 }

}

上一篇:kotlin实战教程之lambda编程

栏    目:Android代码

下一篇:详解Android提交数据到服务器的两种方式四种方法

本文标题:Android使用Recyclerview实现图片水平自动循环滚动效果

本文地址:http://www.codeinn.net/misctech/23393.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有