activity_main
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
news_item2
<?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="horizontal" android:padding="10dp"> <ImageView android:id="@+id/img1" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:scaleType="centerCrop" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="10dp"> <TextView android:id="@+id/title1" android:layout_marginLeft="10dp" android:text="我是标题" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
news_item
<?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" android:padding="10dp"> <TextView android:id="@+id/title3" android:text="我是标题" android:layout_width="match_parent" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <ImageView android:id="@+id/img1" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_weight="1" android:scaleType="centerCrop" android:layout_height="50dp"/> <ImageView android:id="@+id/img2" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:scaleType="centerCrop" android:layout_weight="1" android:layout_height="50dp"/> <ImageView android:id="@+id/img3" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_weight="1" android:scaleType="centerCrop" android:layout_height="50dp"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="340dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1256评论"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="120个赞"/> </LinearLayout> </LinearLayout>
MainActivity
package com.example.kson.monthdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import com.example.kson.monthdemo.adapter.NewsAdapter; import com.example.kson.monthdemo.bean.News; import com.example.kson.monthdemo.common.Constants; import com.example.kson.monthdemo.presenter.NewsPresenter; import com.example.kson.monthdemo.view.INews; import com.jcodecraeer.xrecyclerview.XRecyclerView; public class MainActivity extends AppCompatActivity implements INews { private XRecyclerView rv; private NewsPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { presenter = new NewsPresenter(this); presenter.getData(Constants.GET_URL); } private void initView() { rv = findViewById(R.id.rv); rv.setPullRefreshEnabled(true);//刷新配置 rv.setLoadingMoreEnabled(true);//上拉配置 rv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { rv.refreshComplete(); } @Override public void onLoadMore() { rv.loadMoreComplete(); } }); } @Override public void success(News news) { rv.setLayoutManager(new LinearLayoutManager(this)); System.out.println("size:"+news.data.size()); NewsAdapter newsAdapter = new NewsAdapter(news.data,this); rv.setAdapter(newsAdapter); } @Override protected void onDestroy() { super.onDestroy(); presenter.detach(); } }
Constants
package com.example.kson.monthdemo.common; /** * Author:kson * E-mail:19655910@qq.com * Time:2018/05/29 * Description: */ public class Constants { public static final String GET_URL = "http://ttpc.dftoutiao.com/jsonpc/refresh?type=5010"; public static final int TYPE1 = 3;//条目是三张图 public static final int TYPE2 = 1;//条目是1张图 }
NewsModel
package com.example.kson.monthdemo.model; import com.example.kson.monthdemo.utils.OkhttpUtils; /** * Author:kson * E-mail:19655910@qq.com * Time:2018/05/29 * Description: */ public class NewsModel { /** * 请求数据 * @param getUrl */ public void getData(String getUrl, final ResponseCallback responseCallback) { OkhttpUtils.getInstance().getData(getUrl, new OkhttpUtils.ICallback() { @Override public void success(String result) { responseCallback.success(result); } @Override public void fail(String msg) { responseCallback.fail(msg); } }); } public interface ResponseCallback{ void success(String result); void fail(String msg); } }
NewsPresenter
package com.example.kson.monthdemo.presenter; import android.text.TextUtils; import com.example.kson.monthdemo.MainActivity; import com.example.kson.monthdemo.bean.News; import com.example.kson.monthdemo.model.NewsModel; import com.example.kson.monthdemo.utils.OkhttpUtils; import com.example.kson.monthdemo.view.INews; import com.google.gson.Gson; /** * Author:kson * E-mail:19655910@qq.com * Time:2018/05/29 * Description:p */ public class NewsPresenter { private INews iNews; private NewsModel model; public NewsPresenter(INews iNews) { model = new NewsModel(); attach(iNews); } /** * 绑定view * @param iNews */ public void attach(INews iNews){ this.iNews = iNews; } /** * 获取数据的方法 * * @param getUrl */ public void getData(String getUrl) { model.getData(getUrl, new NewsModel.ResponseCallback() { @Override public void success(String result) { if (!TextUtils.isEmpty(result)) { String s = result.replace("null(","") .replace(")",""); News news = new Gson().fromJson(s, News.class); iNews.success(news); } } @Override public void fail(String msg) { } }); // OkhttpUtils.getInstance().getData(getUrl, new OkhttpUtils.ICallback() { // @Override // public void success(String result) { // // } // // @Override // public void fail(String msg) { // // } // }); } /** * 解绑 */ public void detach(){ this.iNews = null; } }
OkhttpUtils
package com.example.kson.monthdemo.utils; import android.content.Context; import android.os.Handler; import java.io.IOException; import java.util.List; import java.util.Map; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; /** * Author:kson * E-mail:19655910@qq.com * Time:2018/05/29 * Description: */ public class OkhttpUtils { private static OkhttpUtils okhttpUtils; private OkHttpClient okHttpClient; private Handler handler; private OkhttpUtils() { okHttpClient = new OkHttpClient.Builder() .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .build(); handler = new Handler(); } public static OkhttpUtils getInstance() { if (okhttpUtils == null) { okhttpUtils = new OkhttpUtils(); } return okhttpUtils; } /** * get方式 */ public void getData(String url, final ICallback callback){ final Request request = new Request.Builder() .url(url).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { if (callback!=null){ handler.post(new Runnable() { @Override public void run() { callback.fail("请求失败"); } }); } } @Override public void onResponse(Call call, Response response) throws IOException { if (callback!=null){ if (response.isSuccessful()&&response.code()==200){ final String result = response.body().string(); handler.post(new Runnable() { @Override public void run() { callback.success(result); } }); } } } }); } /** * post方式 */ public void postData(String url, Map<String,String> params, final ICallback callback){ FormBody.Builder builder = new FormBody.Builder(); for (Map.Entry<String, String> bean : params.entrySet()) { builder.add(bean.getKey(),bean.getValue()); } final Request request = new Request.Builder() .url(url).post(builder.build()).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { if (callback!=null){ handler.post(new Runnable() { @Override public void run() { callback.fail("请求失败"); } }); } } @Override public void onResponse(Call call, Response response) throws IOException { if (callback!=null){ if (response.isSuccessful()&&response.code()==200){ final String result = response.body().string(); handler.post(new Runnable() { @Override public void run() { callback.success(result); } }); } } } }); } public interface ICallback{ void success(String result); void fail(String msg); } }
News
package com.example.kson.monthdemo.bean; import java.util.List; /** * Author:kson * E-mail:19655910@qq.com * Time:2018/05/29 * Description: */ public class News { public String stat; public List<Data> data; public class Data { public String topic; public String source; public List<IMG> miniimg; public class IMG { public String src; } } }
NewsAdapter
package com.example.kson.monthdemo.adapter; import android.animation.LayoutTransition; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.text.Layout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.kson.monthdemo.R; import com.example.kson.monthdemo.bean.News; import com.example.kson.monthdemo.common.Constants; import com.jcodecraeer.xrecyclerview.XRecyclerView; import java.util.List; /** * Author:kson * E-mail:19655910@qq.com * Time:2018/05/29 * Description: */ public class NewsAdapter extends XRecyclerView.Adapter<XRecyclerView.ViewHolder> { private List<News.Data> list; private Context context; public NewsAdapter(List<News.Data> list, Context context) { this.list = list; this.context = context; } @NonNull @Override public XRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if (viewType == Constants.TYPE1) { View view = LayoutInflater.from(context).inflate(R.layout.news_item2_layout, parent, false); return new Type1ViewHolder(view); } else { View view2 = LayoutInflater.from(context).inflate(R.layout.news_item_layout, parent, false); return new Type2ViewHolder(view2); } } @Override public void onBindViewHolder(@NonNull XRecyclerView.ViewHolder holder, int position) { News.Data data = list.get(position); if (holder instanceof Type1ViewHolder) {//1张图片 ((Type1ViewHolder) holder).title.setText(data.topic); } else if (holder instanceof Type2ViewHolder) {//三张图片 ((Type2ViewHolder) holder).title.setText(data.topic); if (data.miniimg!=null&&data.miniimg.size()>0){ if (data.miniimg.size()==1){ Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv1); Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv2); Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv3); }else if (data.miniimg.size()==2){ Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv1); Glide.with(context).load(data.miniimg.get(1).src).into(((Type2ViewHolder) holder).iv2); Glide.with(context).load(data.miniimg.get(1).src).into(((Type2ViewHolder) holder).iv3); }else{ Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv1); Glide.with(context).load(data.miniimg.get(1).src).into(((Type2ViewHolder) holder).iv2); Glide.with(context).load(data.miniimg.get(2).src).into(((Type2ViewHolder) holder).iv3); } } } } @Override public int getItemViewType(int position) { return position % 2 == 0 ? Constants.TYPE1 : Constants.TYPE2; } @Override public int getItemCount() { return list.size(); } class Type1ViewHolder extends XRecyclerView.ViewHolder { private TextView title; public Type1ViewHolder(View itemView) { super(itemView); title = itemView.findViewById(R.id.title1); } } class Type2ViewHolder extends XRecyclerView.ViewHolder { private TextView title; private ImageView iv1, iv2, iv3; public Type2ViewHolder(View itemView) { super(itemView); iv1 = itemView.findViewById(R.id.img1); iv2 = itemView.findViewById(R.id.img2); iv3 = itemView.findViewById(R.id.img3); title = itemView.findViewById(R.id.title3); } } }
依赖
apply plugin: 'com.android.application' android { compileSdkVersion 27 defaultConfig { applicationId "com.example.kson.monthdemo" minSdkVersion 14 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.github.bumptech.glide:glide:4.7.1' annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1' implementation 'com.jcodecraeer:xrecyclerview:1.5.9' implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0' implementation 'com.squareup.okhttp3:okhttp:3.10.0' implementation 'com.google.code.gson:gson:2.8.5' } configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> def requested = details.requested if (requested.group == 'com.android.support') { if (!requested.name.startsWith("multidex")) { details.useVersion '27.1.1' } } } }