布局
----------------------------------------------------------MainActivity.xml----------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="bwie.com.rikao33.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
----------------------------------------------------Main2Activity.xml---------------------------------------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="bwie.com.rikao33.Main2Activity"> <TextView android:id="@+id/main2_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:text="界面二"/> </LinearLayout>
-----------------------------------------------------item.xml------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:id="@+id/ly" android:layout_toRightOf="@+id/img" android:layout_width="match_parent" android:layout_height="120dp" android:orientation="vertical"> <TextView android:id="@+id/mytext" android:layout_width="match_parent" android:textColor="#000" android:layout_height="60dp" android:textSize="22sp"/> <TextView android:id="@+id/shijian" android:layout_width="match_parent" android:textColor="#000" android:layout_height="60dp" android:textSize="16sp"/> </LinearLayout> </RelativeLayout>------------------------------------------------------------------------------------------------------------------------------------
权限
<uses-permission android:name="android.permission.INTERNET"/>-------------------------------------------------------------------------------------------------------------------------------
代码
-----------------------------------------------------------------IView-------------------------------------------------------------------------------------
public interface IView { void success(List<JavaBean.NewslistBean> news); void failed(Exception e); }-------------------------------------------------------------------CallBack --------------------------------------------------------------------------------
public interface CallBack { void onSuccess(Object o); void onFailed(Exception e); }----------------------------------------------OkhttpUtils -------------------------------------------------------------
package bwie.com.rikao33.okHttp; import android.os.Handler; import android.text.TextUtils; import java.io.IOException; import java.util.Map; import bwie.com.rikao33.Utils.GsonUtils; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; /** * Created by 高俊生 on 2017/11/30. */ public class OkhttpUtils { //单例模式 private static volatile OkhttpUtils instance; private Handler handler = new Handler(); //??? public OkhttpUtils() { } public static OkhttpUtils getInstance() { if (null == instance) { synchronized (OkhttpUtils.class) { if (instance == null) { instance = new OkhttpUtils(); } } } return instance; } //??? public void get(String url, Map<String, String> map, final Class cls, final CallBack callBack) { //判断是否有路径,若没有,直接返回 if (TextUtils.isEmpty(url)) { return; } StringBuffer sb = new StringBuffer(); sb.append(url); //???? if (url.contains("?")) { if (url.indexOf("?") == sb.length() - 1) { } else { sb.append("&"); } } else { sb.append("?"); } //??? for (Map.Entry<String, String> entry : map.entrySet()) { sb.append(entry.getKey()) .append("=") .append(entry.getValue()) .append("&"); } if (sb.indexOf("&") != -1) { sb.deleteCharAt(sb.lastIndexOf("&")); } OkHttpClient client = new OkHttpClient.Builder().build(); final Request request = new Request.Builder() .get() .url(sb.toString()) .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { callBack.onFailed(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { String ss = response.body().string(); final Object o = GsonUtils.getInstance().fromJson(ss, cls); handler.post(new Runnable() { @Override public void run() { callBack.onSuccess(o); } }); } }); } }
---------------------------------------------GsonUtils -----------------------------
package bwie.com.rikao33.Utils; import com.google.gson.Gson; /** * Created by 高俊生 on 2017/11/30. */ public class GsonUtils { private static Gson instance; public GsonUtils(){} public static Gson getInstance(){ if (instance==null){ instance = new Gson(); } return instance; } }
---------------------------------------------------------------Presenter ------------------------------------------------------------
package bwie.com.rikao33.presaenter; import java.util.HashMap; import bwie.com.rikao33.View.IView; import bwie.com.rikao33.bean.JavaBean; import bwie.com.rikao33.okHttp.CallBack; import bwie.com.rikao33.okHttp.OkhttpUtils; /** * Created by 高俊生 on 2017/11/30. */ public class Presenter { //接续数据,获取网络数据 private IView iv; //??? public void attachView(IView iv){ this.iv = iv; } public void getnews(){ HashMap<String ,String > map = new HashMap<String, String>(); map.put("key","4a82473783694ddebd495d47b16c906e"); map.put("num","10"); OkhttpUtils.getInstance().get("http://api.tianapi.com/meinv/", map, JavaBean.class, new CallBack() { @Override public void onSuccess(Object o) { JavaBean o1= (JavaBean)o; iv.success(o1.getNewslist()); } @Override public void onFailed(Exception e) { iv.failed(e); } }); } }
---------------------------------------------------------RecyclerviewAdpader ----------------------------------------------------------
package bwie.com.rikao33; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; import bwie.com.rikao33.bean.JavaBean; /** * Created by 高俊生 on 2017/11/30. */ public class RecyclerviewAdpader extends RecyclerView.Adapter<RecyclerviewAdpader.Viewholder> { private Context context; private List<JavaBean.NewslistBean> list; //自定义接口对象,点击事件 private OnRecyclerViewItemClickLintemet listener; //自定义一个接口点击事件 public interface OnRecyclerViewItemClickLintemet { void onItemClick(int position); } //定义接口方法 点击事件 public void setOnRecyclerViewItemClickLintemet(OnRecyclerViewItemClickLintemet listener) { this.listener = listener; } //有参构造器 public RecyclerviewAdpader(Context context, List<JavaBean.NewslistBean> list) { this.context = context; this.list = list; } @Override public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item, null); Viewholder holder = new Viewholder(view); return holder; } @Override public void onBindViewHolder(Viewholder holder, final int position) { Glide.with(context).load(list.get(position).getPicUrl()).into(holder.img); holder.mytext.setText(list.get(position).getTitle()); holder.shijian.setText(list.get(position).getCtime()); //条目单击事件 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listener.onItemClick(position); } }); } @Override public int getItemCount() { return list.size(); } public class Viewholder extends RecyclerView.ViewHolder { private final ImageView img; private final TextView mytext; private final TextView shijian; public Viewholder(View view) { super(view); img = (ImageView) itemView.findViewById(R.id.img); mytext = (TextView) itemView.findViewById(R.id.mytext); shijian = (TextView) itemView.findViewById(R.id.shijian); } } }
--------------------------------------------------MainActivity --------------------------------------------
package bwie.com.rikao33; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import org.greenrobot.eventbus.EventBus; import java.util.ArrayList; import java.util.List; import bwie.com.rikao33.View.IView; import bwie.com.rikao33.bean.JavaBean; import bwie.com.rikao33.presaenter.Presenter; public class MainActivity extends AppCompatActivity implements IView { private RecyclerView rv; private List<JavaBean.NewslistBean> list = new ArrayList<JavaBean.NewslistBean>(); private RecyclerviewAdpader recyclerviewAdpader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rv = (RecyclerView) findViewById(R.id.recyclerView); huoqu(); recyclerviewAdpader = new RecyclerviewAdpader(this, list); LinearLayoutManager manager = new LinearLayoutManager(this); rv.setLayoutManager(manager); //设置适配器 rv.setAdapter(recyclerviewAdpader); recyclerviewAdpader.setOnRecyclerViewItemClickLintemet(new RecyclerviewAdpader.OnRecyclerViewItemClickLintemet() { @Override public void onItemClick(final int position) { Intent intent = new Intent(MainActivity.this, Main2Activity.class); startActivity(intent); //???,粘性事件, EventBus.getDefault().postSticky(list.get(position).getTitle()); } }); } //获取Presenter获取网页的数据 private void huoqu() { Presenter presaenter = new Presenter(); presaenter.attachView(this); presaenter.getnews(); } @Override public void success(List<JavaBean.NewslistBean> news) { if (news != null) { list.addAll(news); recyclerviewAdpader.notifyDataSetChanged(); } } @Override public void failed(Exception e) { Log.d("错误:", e.getMessage()); } }
----------------------------------------------------------Main2Activity -----------------------------------------------------------
package bwie.com.rikao33; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; public class Main2Activity extends AppCompatActivity { private TextView ss; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); ss = (TextView)findViewById(R.id.main2_tv); } @Override protected void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Subscribe(threadMode = ThreadMode.MAIN,sticky = true) public void onMessageEvent(String event) { ss.setText(event); }; @Override protected void onPause() { super.onPause(); finish(); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }