效果图
实现原理
1、比如说一共10条数据,显示的条目是5,所以在RecyclerView的adapter的getItemCount()方法返回的数值就应该是5,其他的都不用更改。
2、要让这些数据切换,需要写一个定时任务ScheduledExecutorService每隔固定时间切换数据显示。
3、数据的切换效果通过adapter的notifyItemRangeRemoved()进行。
代码一览
Adapter代码
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 com.example.zpf.animmenu.R;
import java.util.ArrayList;
/**
* 中奖信息轮播切换的Adapter
*/
public class RvAdapterPrizeInfo extends RecyclerView.Adapter<RvAdapterPrizeInfo.PrizeInfoViewHolder> {
private Context mContext;
private ArrayList<String> prizeList = new ArrayList<>();
public RvAdapterPrizeInfo(Context context) {
this.mContext = context;
}
public void setList(ArrayList<String> list) {
if (list != null && !list.isEmpty()) {
this.prizeList = list;
notifyDataSetChanged();
}
}
public void updateList() {
if (!prizeList.isEmpty()) {
prizeList.add(prizeList.get(0));
prizeList.remove(0);
notifyItemRangeRemoved(0, 1);
}
}
@Override
public PrizeInfoViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_prize_info, viewGroup, false);
return new PrizeInfoViewHolder(view);
}
@Override
public void onBindViewHolder(PrizeInfoViewHolder holder, int i) {
String info = prizeList.get(i);
holder.tv.setText(info);
}
@Override
public int getItemCount() {
return 5;
}
class PrizeInfoViewHolder extends RecyclerView.ViewHolder{
private TextView tv;
PrizeInfoViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.tv_prize_info);
}
}
}
ViewHolder布局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="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_prize_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lines="1"
android:textSize="14sp"
android:textColor="#565656"/>
</LinearLayout>
Activity实现代码
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import adapter.RvAdapterPrizeInfo;
public class RecyclerPrizeInfoActivity extends AppCompatActivity {
private RvAdapterPrizeInfo adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_prize_info);
initView();
}
private void initView() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_prize_info);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
ArrayList<String> strings = new ArrayList<>();
for (int i=0; i<10; i++) {
strings.add("恭喜***抽中" + (i*2) + "元红包!");
}
adapter = new RvAdapterPrizeInfo(this);
recyclerView.setAdapter(adapter);
adapter.setList(strings);
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
ses.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
adapter.updateList();
}
}, 2000, 2000, TimeUnit.MILLISECONDS);
}
}
Activity界面布局xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.example.zpf.animmenu.RecyclerPrizeInfoActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_prize_info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FDE480"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
That’s all ! Thank you ! 有兴趣就去copy代码试试吧