//MyApp类
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
//DataBean类
public class DataBean {
/**
* error : false
* results : [{"_id":"5b63cd4e9d21225e0d3f58c9","createdAt":"2018-08-03T11:34:38.672Z","desc":"2018-08-03","publishedAt":"2018-08-03T00:00:00.0Z","source":"api","type":"福利","url":"https://ww1.sinaimg.cn/large/0065oQSqgy1ftwcw4f4a5j30sg10j1g9.jpg","used":true,"who":"lijinshan"},{"_id":"5b6151509d21225206860f08","createdAt":"2018-08-01T14:21:04.556Z","desc":"2018-08-01","publishedAt":"2018-08-01T00:00:00.0Z","source":"api","type":"福利","url":"https://ww1.sinaimg.cn/large/0065oQSqly1ftu6gl83ewj30k80tites.jpg","used":true,"who":"lijinshan"},{"_id":"5b60356a9d212247776a2e0e","createdAt":"2018-07-31T18:09:46.825Z","desc":"2018-07-31","publishedAt":"2018-07-31T00:00:00.0Z","source":"api","type":"福利","url":"http://ww1.sinaimg.cn/large/0065oQSqgy1ftt7g8ntdyj30j60op7dq.jpg","used":true,"who":"lijinshan"},{"_id":"5b5e93499d21220fc64181a9","createdAt":"2018-07-30T12:25:45.937Z","desc":"2018-07-30","publishedAt":"2018-07-30T00:00:00.0Z","source":"web","type":"福利","url":"https://ww1.sinaimg.cn/large/0065oQSqgy1ftrrvwjqikj30go0rtn2i.jpg","used":true,"who":"lijinshanmx"},{"_id":"5b50107f421aa917a31c0565","createdAt":"2018-07-19T12:15:59.226Z","desc":"2018-07-19","publishedAt":"2018-07-19T00:00:00.0Z","source":"web","type":"福利","url":"https://ww1.sinaimg.cn/large/0065oQSqly1ftf1snjrjuj30se10r1kx.jpg","used":true,"who":"lijinshanmx"},{"_id":"5b4eaae4421aa93aa99bee16","createdAt":"2018-07-18T11:14:55.648Z","desc":"2018-07-18","publishedAt":"2018-07-18T00:00:00.0Z","source":"web","type":"福利","url":"https://ww1.sinaimg.cn/large/0065oQSqly1ftdtot8zd3j30ju0pt137.jpg","used":true,"who":"lijinshanmx"},{"_id":"5b481d01421aa90bba87b9ae","createdAt":"2018-07-13T11:31:13.266Z","desc":"2018-07-13","publishedAt":"2018-07-13T00:00:00.0Z","source":"web","type":"福利","url":"http://ww1.sinaimg.cn/large/0073sXn7ly1ft82s05kpaj30j50pjq9v.jpg","used":true,"who":"lijinshanmx"},{"_id":"5b456f5d421aa92fc4eebe48","createdAt":"2018-07-11T10:45:49.246Z","desc":"2018-07-11","publishedAt":"2018-07-11T00:00:00.0Z","source":"web","type":"福利","url":"https://ww1.sinaimg.cn/large/0065oQSqly1ft5q7ys128j30sg10gnk5.jpg","used":true,"who":"lijinshanmx"},{"_id":"5b441f06421aa92fccb520a2","createdAt":"2018-07-10T10:50:46.379Z","desc":"2018-07-10","publishedAt":"2018-07-10T00:00:00.0Z","source":"web","type":"福利","url":"https://ww1.sinaimg.cn/large/0065oQSqgy1ft4kqrmb9bj30sg10fdzq.jpg","used":true,"who":"lijinshanmx"},{"_id":"5b42d1aa421aa92d1cba2918","createdAt":"2018-07-09T11:08:26.162Z","desc":"2018-07-09","publishedAt":"2018-07-09T00:00:00.0Z","source":"web","type":"福利","url":"http://ww1.sinaimg.cn/large/0065oQSqly1ft3fna1ef9j30s210skgd.jpg","used":true,"who":"lijinshanmx"}]
*/
private boolean error;
private List<ResultsBean> results;
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public List<ResultsBean> getResults() {
return results;
}
public void setResults(List<ResultsBean> results) {
this.results = results;
}
public static class ResultsBean {
/**
* _id : 5b63cd4e9d21225e0d3f58c9
* createdAt : 2018-08-03T11:34:38.672Z
* desc : 2018-08-03
* publishedAt : 2018-08-03T00:00:00.0Z
* source : api
* type : 福利
* url : https://ww1.sinaimg.cn/large/0065oQSqgy1ftwcw4f4a5j30sg10j1g9.jpg
* used : true
* who : lijinshan
*/
private String _id;
private String createdAt;
private String desc;
private String publishedAt;
private String source;
private String type;
private String url;
private boolean used;
private String who;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isUsed() {
return used;
}
public void setUsed(boolean used) {
this.used = used;
}
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
}
}
//接口类DataModel
public interface DataModel {
void showData(String page, OnNetListener onNetListener);
}
//DataModellmp类
public class DataModelImp implements DataModel{
@Override
public void showData(String page, final OnNetListener onNetListener) {
OkhttpUtils.getInstance().doGet(Api.DATA_URL , new OnNetListener() {
@Override
public void OnSuccess(String result) {
onNetListener.OnSuccess(result);
}
@Override
public void OnFilder(Exception e) {
onNetListener.OnFilder(e);
}
});
}
}
//DataPresenter接口类
public interface DataPresenter {
void showData(String page);
}
// DataPresenterImpl类
public class DataPresenterImp implements DataPresenter {
private DataView dataView;
private final DataModelImp dataModelImp;
/**
* 绑定View
* @param dataView
*/
public void attachView(DataView dataView){
this.dataView = dataView;
}
/**
* 解绑View
*/
public void detachView(){
dataView = null;
}
/**
* 使用前先检查当前View是否可用
* @return
*/
public boolean checkViewAvailable(){
return dataView != null;
}
public DataPresenterImp(DataView dataView) {
this.dataView = dataView;
dataModelImp = new DataModelImp();
}
@Override
public void showData(String page) {
dataModelImp.showData(page, new OnNetListener() {
@Override
public void OnSuccess(String result) {
DataBean dataBeans = new Gson().fromJson(result, DataBean.class);
List<DataBean.ResultsBean> listData = dataBeans.getResults();
dataView.showData(listData);
}
@Override
public void OnFilder(Exception e) {
}
});
}
public void cloneView(){
if(dataView != null){
dataView = null;
}
}
}
//工具类 OkhttpUtils
public class OkhttpUtils {
private static OkhttpUtils instance;
private final OkHttpClient okHttpClient;
private Handler handler;
private OkhttpUtils() {
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15,TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.build();
handler = new Handler(Looper.getMainLooper());
}
public static OkhttpUtils getInstance(){
if(instance == null){
instance = new OkhttpUtils();
}
return instance;
}
public void doGet(String url, final OnNetListener onNetListener){
//创建Request
final Request request = new Request.Builder()
.url(url)
.build();
//发送请求
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.OnFilder(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.OnSuccess(string);
}
});
}
});
}
}
//接口类DataView
public interface DataView {
void showData(List<DataBean.ResultsBean> list);
}
//接口类 Api
public interface Api {
String DATA_URL = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1";
}
//创建Main2Activity类
public class Main2Activity extends AppCompatActivity implements DataView{
private List<DataBean.ResultsBean> list;
@BindView(R.id.recy)
RecyclerView recy;
private LinearLayoutManager linearLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ButterKnife.bind(this);
DataPresenterImp dataPresenterImp = new DataPresenterImp(this);
dataPresenterImp.showData(Api.DATA_URL);
}
@OnClick(R.id.recy)
public void onViewClicked() {
}
@Override
public void showData(final List<DataBean.ResultsBean> list) {
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
MyAdapter myAdapter = new MyAdapter(list);
recy.setAdapter(myAdapter);
recy.setLayoutManager(linearLayoutManager);
myAdapter.setOnItemzClickpotion(new OnItemzClickpotion() {
@Override
public void setPotion(int potion) {
DataBean.ResultsBean resultsBean = list.get(potion);
EventBus.getDefault().post(resultsBean);
finish();
}
});
}
}
//MainActivty类
public class MainActivity extends AppCompatActivity {
@BindView(R.id.img_view)
SimpleDraweeView imgView;
@BindView(R.id.textView)
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
EventBus.getDefault().register(this);
}
@OnClick(R.id.img_view)
public void onViewClicked() {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
@Subscribe(threadMode = ThreadMode.MainThread)
public void onEventMainThread(DataBean.ResultsBean resultsBean) {
imgView.setImageURI(resultsBean.getUrl());
textView.setText(resultsBean.getWho());
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
//创建适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<DataBean.ResultsBean> list;
public MyAdapter(List<DataBean.ResultsBean> list) {
this.list = list;
}
@NonNull
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, final int position) {
String[] split = list.get(position).getUrl().split("\\|");
holder.simpleDraweeView.setImageURI(Uri.parse(split[0]));
holder.tuijian_title.setText(list.get(position).getWho());
holder.simpleDraweeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OnItemzClickpotion.setPotion(position);
}
});
}
OnItemzClickpotion OnItemzClickpotion;
public void setOnItemzClickpotion(OnItemzClickpotion onItemzClickpotion) {
this.OnItemzClickpotion = onItemzClickpotion;
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private SimpleDraweeView simpleDraweeView;
private TextView tuijian_title;
public MyViewHolder(View itemView) {
super(itemView);
simpleDraweeView = itemView.findViewById(R.id.imageView);
tuijian_title=itemView.findViewById(R.id.textView);
}
}
}
//接口类 OnitemzClikpotion
public interface OnItemzClickpotion {
void setPotion(int potion);
}
//接口类OnNetListener
public interface OnNetListener {
void OnSuccess(String result);
void OnFilder(Exception e);
}
//布局文件
//activity_main
<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"
android:orientation="vertical"
tools:context="bie.com.moni.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_marginTop="30dp"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center_horizontal"/>
<com.facebook.drawee.view.SimpleDraweeView
app:roundAsCircle="true"
android:id="@+id/img_view"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
//activity_main2
<android.support.constraint.ConstraintLayout 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="bie.com.moni.Main2Activity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recy"></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
//item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.facebook.drawee.view.SimpleDraweeView
app:roundAsCircle="true"
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="34dp"
android:layout_toEndOf="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:text="TextView" />
</RelativeLayout>
//依赖
implementation 'com.facebook.fresco:fresco:1.9.0'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'de.greenrobot:eventbus:3.0.0-beta1'
compile 'org.greenrobot:greendao-generator:3.0.0'
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
implementation 'com.squareup.okio:okio:1.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
compile 'com.android.support:recyclerview-v7:26+'
//因版本问题所以我的必须加 (因情况而定)
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'
}
}
}
}