Android : Xui- RecyclerView+BannerLayout 轮播图简单应用

实例图:

Android : Xui- RecyclerView+BannerLayout 轮播图简单应用_第1张图片

1.引用XUI

http://t.csdnimg.cn/Wb4KR

2.创建显示图片布局 banner_item.xml




    


3.写个适配器继承 RecyclerView  BannerAdapter.java

package com.example.xuicarousel.xui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.xuicarousel.R;
import com.xuexiang.xui.widget.imageview.ImageLoader;

public class BannerAdapter extends RecyclerView.Adapter {

    private String[] urls;//图片地址
    private Context context;

    public BannerAdapter(Context context, String[] urls) {
        this.urls = urls;
        this.context = context;
    }

    @NonNull
    @Override
    public BannerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.banner_item, parent, false);
        return new BannerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BannerViewHolder holder, int position) {
        ImageView imageView = holder.imageView;
        //XUI 的图片加载器
        ImageLoader.get().loadImage(imageView, urls[position]);
    }

    @Override
    public int getItemCount() {
        return urls.length;
    }


    class BannerViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;//轮播条的 item 项

        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_item);
        }


    }
}

4.主布局xml  activity_main.xml







    

    

    

        

            

            
            



            
            

            

            

            



            

        
    


5.MainActivity.java

package com.example.xuicarousel;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.example.xuicarousel.xui.adapter.BannerAdapter;
import com.xuexiang.xui.widget.banner.recycler.BannerLayout;

public class MainActivity extends AppCompatActivity {

    private BannerAdapter mAdapter;

    private BannerLayout mBl, mBlTwo, mBlThree;

    public String[] urls = new String[]{//轮播图片地址
            "http://photocdn.sohu.com/tvmobilemvms/20150907/144160323071011277.jpg",
            "http://photocdn.sohu.com/tvmobilemvms/20150907/144158380433341332.jpg",
            "http://photocdn.sohu.com/tvmobilemvms/20150907/144160286644953923.jpg",
            "http://photocdn.sohu.com/tvmobilemvms/20150902/144115156939164801.jpg",
            "http://photocdn.sohu.com/tvmobilemvms/20150907/144159406950245847.jpg",
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdapter = new BannerAdapter(this, urls);

        mBl = findViewById(R.id.bl);
        mBl.setAdapter(mAdapter);


        mBlTwo = findViewById(R.id.bltwo);
        mBlTwo.setAdapter(mAdapter);

        mBlThree = findViewById(R.id.blthree);
        mBlThree.setAdapter(mAdapter);


      /*
      // 事件
      mBlThree.setOnIndicatorIndexChangedListener(new BannerLayout.OnIndicatorIndexChangedListener() {
            @Override
            public void onIndexChanged(int position) {
                Toast.makeText(MainActivity.this,"轮播到了第 "+position+" 个"  ,Toast.LENGTH_SHORT).show();
            }
        });
*/

    }
}

6.其他配置

AndroidMainifest.xml 中

联网权限:

    


//
上网配置


buid.gradle 中:导入

dependencies {

    //Xui
    implementation 'com.github.xuexiangjys:XUI:1.1.5'
...
    //解决加载图片报错
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    implementation 'com.github.bumptech.glide:compiler:4.11.0'
...

}





你可能感兴趣的:(Android,相关,android,笔记)