這篇文章將為大家詳細(xì)講解有關(guān)Android使用Recyclerview實(shí)現(xiàn)圖片輪播效果的方法,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對(duì)外擴(kuò)展宣傳的重要窗口,一個(gè)合格的網(wǎng)站不僅僅能為公司帶來(lái)巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺(tái),創(chuàng)新互聯(lián)公司面向各種領(lǐng)域:柴油發(fā)電機(jī)等成都網(wǎng)站設(shè)計(jì)、全網(wǎng)整合營(yíng)銷(xiāo)推廣解決方案、網(wǎng)站設(shè)計(jì)等建站排名服務(wù)。
思路:
1.準(zhǔn)備m張圖片
1.使用Recyclerview實(shí)現(xiàn),返回?zé)o數(shù)個(gè)(實(shí)際Interge.MAXVALUE)item,第n個(gè)item顯示第n%m張圖片
3.使用recyclerview.scrollBy 每個(gè)一段時(shí)間水平滾動(dòng)一段距離
4.通過(guò)layoutManager.findFirstVisibleItemPosition()獲取當(dāng)前顯示的第一個(gè)View是第幾個(gè)item,上面的ImageView顯示對(duì)應(yīng)de圖片
實(shí)現(xiàn)代碼:
XML文件
1.activity布局文件activity_recy.xml
<?xml version="1.0" encoding="utf-8"?>
2.適配器布局item_horizon.xml
<?xml version="1.0" encoding="utf-8"?>
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 Listdatas; 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
適配器RecyAdapter
public class RecyAdapter extends RecyclerView.Adapterimplements View.OnClickListener { private Context context; private List datas; private OnItemClickListener onItemClickListener; public RecyAdapter(Context context, List 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); } }
關(guān)于Android使用Recyclerview實(shí)現(xiàn)圖片輪播效果的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。