接口:
http://result.eolinker.com/umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage
添加的依赖
implementation 'com.android.support:recyclerview-v7:26.1.0' compile 'com.facebook.fresco:fresco:1.5.0' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//Retrofit2所需要的包 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//ConverterFactory的Gson依赖包 compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'//ConverterFactory的String依赖包 compile 'com.youth.banner:banner:1.4.9' compile 'com.github.bumptech.glide:glide:3.7.0'
activity_main布局
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwie.qiaoxiaohui20171204.view.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#FF5001" android:orientation="horizontal"> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/a" /> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv" android:gravity="center" android:hint="搜索一下" android:textColor="#fff" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_margin="5dp" android:background="@drawable/b" /> </RelativeLayout> <com.youth.banner.Banner android:id="@+id/bar" android:layout_width="wrap_content" android:layout_height="200dp"/> <android.support.v7.widget.RecyclerView android:id="@+id/rv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:textSize="20sp" android:text="热门活动" /> <LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:orientation="horizontal" android:layout_marginTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:textSize="14sp" android:text="倒计时:"/> <TextView android:id="@+id/tv_hour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:paddingTop="3dp" android:paddingBottom="3dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:textColor="@android:color/black" android:textSize="12sp" android:text="02"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:textSize="20sp" android:text="时"/> <TextView android:id="@+id/tv_minute" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:paddingTop="3dp" android:paddingBottom="3dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:textSize="12sp" android:text="15"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:textSize="20sp" android:text="分"/> <TextView android:id="@+id/tv_second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:paddingTop="3dp" android:paddingBottom="3dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:textSize="12sp" android:text="36"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:textSize="20sp" android:text="秒"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:textSize="30sp" android:text="热门专题" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="20dp"/> </LinearLayout> </LinearLayout> </ScrollView>
item_rc01.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/img_rc01" android:layout_width="50dp" android:layout_height="50dp" /> <TextView android:id="@+id/tit" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" /> </LinearLayout>item_rc03.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/img_rc02" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" app:placeholderImage="@mipmap/ic_launcher" /> </LinearLayout>item_rc03.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/img_rc03" android:layout_width="match_parent" android:layout_height="200dp" app:placeholderImage="@mipmap/ic_launcher" /> <TextView android:id="@+id/tit03" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:textSize="25sp" /> </LinearLayout>app-去清单文件注册public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }加载图片工具public class ImageUtil extends ImageLoader{ @Override public void displayImage(Context context, Object path, ImageView imageView) { //Glide 加载图片简单用法 Glide.with(context).load((String) path).into(imageView); } }Apipublic interface Api { public static final String HOST="http://result.eolinker.com/"; public static final String URL="umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage"; }回调接口public interface OnNetListener<T> { //成功回调 public void onSuccess(T t); //失败回调 public void onFailure(Exception e); }单独封装的OkHttpClientpublic class OkHttpUtils { public static OkHttpClient client; public static OkHttpClient getHttpUtil() { if (client == null) { synchronized (OkHttpUtils.class) { if (client == null) { client = new OkHttpClient(); } } } return client; } }ServerApipublic interface ServerApi { @GET(Api.URL) Call<ClassBean> list(); }Retrofit封装
public class RetrofitHelper { public static OkHttpClient client; public static ServerApi serverApi; static { client = OkHttpUtils.getHttpUtil(); } public static ServerApi getServerApi() { if (serverApi == null){ synchronized (ServerApi.class){ if (serverApi == null){ serverApi = OnCreatApi(ServerApi.class,Api.HOST); } } } return serverApi; } public static <T> T OnCreatApi(Class<T> tClass,String url){ Retrofit retrofit = new Retrofit.Builder() .baseUrl(url) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(tClass); } }model层public class MainModel implements IMainModel { @Override public void getData(final OnNetListener<ClassBean> onNetListener) { ServerApi serverApi = RetrofitHelper.getServerApi(); Call<ClassBean> list = serverApi.list(); list.enqueue(new retrofit2.Callback<ClassBean>() { @Override public void onResponse(Call<ClassBean> call, retrofit2.Response<ClassBean> response) { onNetListener.onSuccess(response.body()); } @Override public void onFailure(Call<ClassBean> call, Throwable t) { onNetListener.onFailure((Exception) t); } }); } }model层的接口public interface IMainModel { public void getData(OnNetListener<ClassBean> onNetListener); }presenter层public class MainPresenter { IMainActivity iMainActivity; IMainModel iMainModel; public MainPresenter(IMainActivity iMainActivity) { this.iMainActivity = iMainActivity; iMainModel = new MainModel(); } public void getClassBean(){ iMainModel.getData(new OnNetListener<ClassBean>() { @Override public void onSuccess(ClassBean classBean) { iMainActivity.show(classBean); } @Override public void onFailure(Exception e) { } }); } }view层public class MainActivity extends AppCompatActivity implements IMainActivity { private Banner mBar; private RecyclerView mRv1; private RecyclerView mRv2; List<String> imgs; MainPresenter mainPresenter = new MainPresenter(this); /** * 02 */ private TextView mTvHour; /** * 15 */ private TextView mTvMinute; /** * 36 */ private TextView mTvSecond; private long mHour = 02; private long mMin = 15; private long mSecond = 36; private boolean isRun = true; private Handler timeHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==1) { computeTime(); if (mHour<10){ mTvHour.setText("0"+mHour+""); }else { mTvHour.setText("0"+mHour+""); } if (mMin<10){ mTvMinute.setText("0"+mMin+""); }else { mTvMinute.setText(mMin+""); } if (mSecond<10){ mTvSecond.setText("0"+mSecond+""); }else { mTvSecond.setText(mSecond+""); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); mainPresenter.getClassBean(); ActionBar bar = getSupportActionBar(); bar.hide(); mRv1.setLayoutManager(new GridLayoutManager(this,4)); mRv2.setLayoutManager(new LinearLayoutManager(this)); startRun(); } private void startRun() { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (isRun) { try { Thread.sleep(1000); // sleep 1000ms Message message = Message.obtain(); message.what = 1; timeHandler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } } } }).start(); } /** * 倒计时计算 */ private void computeTime() { mSecond--; if (mSecond < 0) { mMin--; mSecond = 59; if (mMin < 0) { mMin = 59; mHour--; } } } @Override public void show(final ClassBean classBean) { runOnUiThread(new Runnable() { @Override public void run() { imgs = new ArrayList<>(); List<ClassBean.DataBean.Ad1Bean> ad1 = classBean.getData().getAd1(); for (int i = 0; i < ad1.size(); i++) { imgs.add(ad1.get(i).getImage()); } mBar.setImageLoader(new ImageUtil()); mBar.setImages(imgs); mBar.setDelayTime(1000); mBar.start(); //拿到第一个数据 List<ClassBean.DataBean.Ad5Bean> ad5 = classBean.getData().getAd5(); mRv1.setAdapter(new RvAdapter1(ad5,MainActivity.this)); //拿到第二个数据 List<ClassBean.DataBean.DefaultGoodsListBean> beanList = classBean.getData().getDefaultGoodsList(); mRv2.setAdapter(new RvAdapter2(beanList,MainActivity.this)); } }); } private void initView() { mBar = findViewById(R.id.bar); mRv1 = findViewById(R.id.rv1); mRv2 = findViewById(R.id.rv2); mTvHour = findViewById(R.id.tv_hour); mTvMinute = findViewById(R.id.tv_minute); mTvSecond = findViewById(R.id.tv_second); } @Override protected void onDestroy() { super.onDestroy(); mainPresenter = null; } }view层接口public interface IMainActivity { public void show(ClassBean classBean); }适配器1public class RvAdapter1 extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<ClassBean.DataBean.Ad5Bean> list; private Context context; public RvAdapter1(List<ClassBean.DataBean.Ad5Bean> list, Context context) { this.list = list; this.context = context; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item_rc01, null); return new MyViewholder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ClassBean.DataBean.Ad5Bean bean = list.get(position); MyViewholder holder1 = (MyViewholder) holder; holder1.sdv.setImageURI(bean.getImage()); holder1.tv.setText(bean.getTitle()+""); } @Override public int getItemCount() { return list.size(); } class MyViewholder extends RecyclerView.ViewHolder{ private SimpleDraweeView sdv; private TextView tv; public MyViewholder(View itemView) { super(itemView); sdv = itemView.findViewById(R.id.img_rc01); tv = itemView.findViewById(R.id.tit); } } }适配器2public class RvAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<ClassBean.DataBean.DefaultGoodsListBean> list; private Context context; int TYPE_NUM = 0; int TYPE_TWO = 1; public RvAdapter2(List<ClassBean.DataBean.DefaultGoodsListBean> list, Context context) { this.list = list; this.context = context; } /** * 实现多条目 * @param position * @return */ @Override public int getItemViewType(int position) { if (position%2 == 0){ return TYPE_NUM; } else { return TYPE_TWO; } } @Override public long getItemId(int position) { return super.getItemId(position); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_NUM){ View view = View.inflate(context, R.layout.item_rc02, null); return new MyViewHolder1(view); } else if (viewType == TYPE_TWO){ View view = View.inflate(context, R.layout.item_rc03, null); return new MyViewHolder2(view); } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (getItemViewType(position) == TYPE_NUM){ ClassBean.DataBean.DefaultGoodsListBean bean = list.get(position); MyViewHolder1 holder1 = (MyViewHolder1) holder; holder1.sdv.setImageURI(bean.getGoods_img()); } else if (getItemViewType(position) == TYPE_TWO){ ClassBean.DataBean.DefaultGoodsListBean bean = list.get(position); MyViewHolder2 holder2 = (MyViewHolder2) holder; holder2.sdv.setImageURI(bean.getGoods_img()); holder2.tv.setText(bean.getGoods_name()+""); } } @Override public int getItemCount() { return list.size(); } class MyViewHolder1 extends RecyclerView.ViewHolder{ SimpleDraweeView sdv; public MyViewHolder1(View itemView) { super(itemView); sdv=itemView.findViewById(R.id.img_rc02); } } class MyViewHolder2 extends RecyclerView.ViewHolder{ SimpleDraweeView sdv; TextView tv; public MyViewHolder2(View itemView) { super(itemView); sdv=itemView.findViewById(R.id.img_rc03); tv=itemView.findViewById(R.id.tit03); } } }