bananer使用:第一:权限配置<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.VIBRATE" />
第二:
compile 'com.squareup.okio:okio:1.5.0'compile 'com.squareup.okhttp3:okhttp:3.2.0'compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'compile 'com.google.code.gson:gson:2.8.2'compile 'com.youth.banner:banner:1.4.9'//compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'compile 'com.github.bumptech.glide:glide:3.7.0'
第三加布局:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="150dp"><com.youth.banner.Bannerandroid:id="@+id/mybanner"android:layout_width="match_parent"android:layout_height="150dp"></com.youth.banner.Banner></LinearLayout>
第四步:创建GlideImageLoader 图片加载类
public class GlideImaGlideImageLoader geLoader extends ImageLoader {@Overridepublic void displayImage(Context context, Object path, ImageView imageView) {Glide.with(context).load(path).into(imageView);}}
第五步:
在onCreate中加mbanner= (Banner) findViewById(R.id.mybanner);
GetDate(mpath);
private void GetDate(String url){OkHttp3Utils.getInstance().doGet(url, new GsonObjectCallback<HomeBean>() {@Overridepublic void onUi(HomeBean homeBean) {
for(int i=0; i<homeBean.getData().getAd1().size();i++){String ad1s = homeBean.getData().getAd1().get(i).getImage();mlist.add(ad1s);}//设置图片加载器mbanner.setImageLoader(new GlideImageLoader());mbanner.setImages(mlist);mbanner.start();}@Overridepublic void onFailed(Call call, IOException e) {
}});}权限:<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" />别忘了加myAPP
xml:<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="150dp" android:layout_below="@+id/linearLayout" > <com.youth.banner.Banner android:id="@+id/mybanner" android:layout_width="match_parent" android:layout_height="150dp"> </com.youth.banner.Banner> </LinearLayout> <LinearLayout android:layout_below="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:layout_below="@+id/mybanner" android:id="@+id/id_recyclerview" android:divider="#ffff0000" android:dividerHeight="10dp" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
package com.bwei.mango.xiangmu1;import android.content.Context;import android.widget.ImageView;import com.bumptech.glide.Glide;import com.youth.banner.loader.ImageLoader;/** * Created by wangjiao on 2017/10/14. */public class GlideImageLoader extends ImageLoader { @Override public void displayImage(Context context, Object path, ImageView imageView) { Glide.with(context).load(path).into(imageView); }}
主代码:
package com.bwei.mango.xiangmu1; import android.graphics.Rect; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.bwei.mango.xiangmu1.bean.Foot; import com.bwei.mango.xiangmu1.bean.User; import com.bwei.mango.xiangmu1.utils.GsonObjectCallback; import com.bwei.mango.xiangmu1.utils.NetWorkUtils; import com.bwei.mango.xiangmu1.utils.OkHttp3Utils; import com.nostra13.universalimageloader.core.ImageLoader; import com.youth.banner.Banner; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.Call; public class Shop extends AppCompatActivity { //bananer接口1 private String mpath="http://result.eolinker.com/umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage"; private Banner mbanner; private Button button3; //商品接口 private String JsonUrl="http://api.tianapi.com/social/?key=71e58b5b2f930eaf1f937407acde08fe&num=20"; private RecyclerView mRecyclerView; private boolean b; private List<Foot.DataBean.Ad1Bean> mdata; private HomeAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shop); button3 = (Button) findViewById(R.id.button3); //Bananer mbanner = (Banner) findViewById(R.id.mybanner); GetDate(mpath); //RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); //设置item之间的间隔 SpacesItemDecoration decoration=new SpacesItemDecoration(16); mRecyclerView.addItemDecoration(decoration); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //切换listview if (b==false){ mRecyclerView.setLayoutManager(new LinearLayoutManager(Shop.this)); b=true; }else { mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); b=false; } } }); } /** * * okhttp+bananer轮播图 * */ private void GetDate(String mpath) { OkHttp3Utils.getInstance().doGet(mpath, new GsonObjectCallback<Foot>() { @Override public void onUi(Foot foot) { for(int i=0; i<foot.getData().getAd1().size();i++){ mdata = foot.getData().getAd1(); } mRecyclerView.setAdapter(mAdapter = new HomeAdapter()); //轮播 List<String> mlist = new ArrayList<String>(); for(int i=0; i<foot.getData().getAd1().size();i++){ String ad1s = foot.getData().getAd1().get(i).getImage(); mlist.add(ad1s); } //设置图片加载器 mbanner.setImageLoader(new GlideImageLoader()); mbanner.setImages(mlist); mbanner.start(); } @Override public void onFailed(Call call, IOException e) { } }); } /** * * okhttp+瀑布流 * */ class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> { @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyViewHolder holder = new MyViewHolder(LayoutInflater.from( Shop.this).inflate(R.layout.item_home, parent, false)); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(mdata.get(position).getTitle()); ImageLoader imageLoader = ImageLoader.getInstance(); imageLoader.displayImage(mdata.get(position).getImage(), holder.image); } @Override public int getItemCount() { return mdata.size(); } class MyViewHolder extends RecyclerView.ViewHolder { TextView tv; ImageView image; public MyViewHolder(View view) { super(view); tv = (TextView) view.findViewById(R.id.id_num); image = (ImageView) view.findViewById(R.id.image); } } } public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int space; public SpacesItemDecoration(int space) { this.space=space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.left=space; outRect.right=space; outRect.bottom=space; if(parent.getChildAdapterPosition(view)==0){ outRect.top=space; } } } }
Bananer与瀑布流
最新推荐文章于 2022-07-08 20:27:56 发布