1.设置条目点击事件
2.在第二页面接收值
完整代码:
-----------------MainActivity-----------------
package com.example.earl.lianxilianxi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ArrayList<ResultBeans.ResultBean.DataBean> list = new ArrayList<>();
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fresco.initialize(this);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rcv);
//设置布局管理器
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public void zhanshi(View view) {
//创建OKHttpClient对象
OkHttpUtils okHttpUitls = new OkHttpUtils();
okHttpUitls.get(Constants.URL);
okHttpUitls.setOnOKHttpGetListener(new OkHttpUtils.OKHttpGetListener() {
@Override
public void error(String error) {
Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();
}
@Override
public void success(String json) {
// try {
// //实例化JSONObject对象
// JSONObject jsonObject = new JSONObject(json);
// Log.d("data2", jsonObject.toString());
// //得到对象result
// JSONObject result = jsonObject.getJSONObject("result");
// //得到数组list
// JSONArray jsonArray = result.getJSONArray("data");
// //遍历数组
// for (int i = 0; i < jsonArray.length(); i++) {
// JSONObject object = jsonArray.getJSONObject(i);
// String thumbnail_pic_s = object.optString("thumbnail_pic_s");
// String title = object.optString("title");
// Bean bean = new Bean(thumbnail_pic_s, title);
// list.add(bean);
// }
//
// } catch (JSONException e) {
// e.printStackTrace();
// }
Gson gson = new Gson();
ResultBeans resultBeans = gson.fromJson(json, ResultBeans.class);
List<ResultBeans.ResultBean.DataBean> data = resultBeans.getResult().getData();
list.addAll(data);
//设置适配器
MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);
recyclerView.setAdapter(myAdapter);
Toast.makeText(MainActivity.this,"+++", Toast.LENGTH_SHORT).show();
}
});
}
}
------------------Main2Activity-------------------
package com.example.earl.lianxilianxi;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ImageView img = findViewById(R.id.big_img);
TextView tv=findViewById(R.id.title_tv);
Intent intent = getIntent();
String thumbnail_pic_s02 = intent.getStringExtra("thumbnail_pic_s02");
String title = intent.getStringExtra("title");
Log.d("title",title+"------------------------");
Glide.with(Main2Activity.this).load(thumbnail_pic_s02).into(img);
tv.setText(title.toString());
}
}
-----------------MyAdapter---------------
package com.example.earl.lianxilianxi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
Context context;
ArrayList<ResultBeans.ResultBean.DataBean> list;
public MyAdapter(Context context, ArrayList<ResultBeans.ResultBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.lv_item, null);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder,final int position) {
holder.textView.setText(list.get(position).getTitle());
// Glide.with(context).load(list.get(position).getImg()).into(holder.imageView);
Uri imguri = Uri.parse(list.get(position).getThumbnail_pic_s());
holder.imageView.setImageURI(imguri);
Log.d("ni",list.get(position).getThumbnail_pic_s());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, Main2Activity.class);
intent.putExtra("thumbnail_pic_s02",list.get(position).getThumbnail_pic_s02());
intent.putExtra("title",list.get(position).getTitle());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private SimpleDraweeView imageView;
private TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
this.imageView = itemView.findViewById(R.id.img);
this.textView = itemView.findViewById(R.id.tv);
}
public SimpleDraweeView getImageView() {
return imageView;
}
public void setImageView(SimpleDraweeView imageView) {
this.imageView = imageView;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
}
------------------Constants----------------
package com.example.earl.lianxilianxi;
public class Constants {
// private static String key = "2f41498b35e69877fc56dc96776e5d1f";
public static String URL ="http://v.juhe.cn/toutiao/index?type=top&key=2f41498b35e69877fc56dc96776e5d1f";
}
------------------OkHttpUtils------------------
package com.example.earl.lianxilianxi;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpUtils {
private OKHttpGetListener onOKHttpGetListener;
private MyHandler myHandler = new MyHandler();
// /get
public void get(String url){
OkHttpClient client = new OkHttpClient();
//创建请求对象
Request request = new Request.Builder().url(url).build();
//创建Call请求队列
//请求都是放到一个队列里面的
Call call = client.newCall(request);
Log.d("TAG1", "get() returned: " + call+"------------");
//开始请求
call.enqueue(new Callback() {
//失败,成功的方法都是在子线程里面,不能直接更新UI
@Override
public void onFailure(Call call, IOException e) {
Message message = myHandler.obtainMessage();
message.obj = "请求失败";
message.what = 0;
myHandler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message message = myHandler.obtainMessage();
String json = response.body().string();
message.obj = json;
message.what = 1;
myHandler.sendMessage(message);
}
});
}
//使用接口回到,将数据返回
public interface OKHttpGetListener{
void error(String error);
void success(String json);
}
//给外部调用的方法
public void setOnOKHttpGetListener(OKHttpGetListener onOKHttpGetListener){
this.onOKHttpGetListener = onOKHttpGetListener;
}
//使用Handler,将数据在主线程返回
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
int w = msg.what;
Log.d("TAG2", "handleMessage() returned: " +msg );
if (w ==0){
String error = (String) msg.obj;
onOKHttpGetListener.error(error);
}
if (w==1){
String json = (String) msg.obj;
onOKHttpGetListener.success(json);
}
}
}
}
-------------------Bean-------------------(使用原生解析式会用)
package com.example.earl.lianxilianxi;
public class Bean {
private String img;
private String title;
public Bean(String img, String title) {
this.img = img;
this.title = title;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
-------------------ResultBeans---------------------
自己用快捷键生成 Alt+S
------------------activity_main.xml---------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="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="com.example.earl.lianxilianxi.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="展示数据"
android:onClick="zhanshi"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rcv"
android:layout_width="match_parent"
android:layout_height="match_parent"
fresco:roundAsCircle="true"></android.support.v7.widget.RecyclerView>
</LinearLayout>
-------------------lv_item.xml----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/img"
android:layout_width="80dp"
android:layout_height="80dp"
fresco:roundAsCircle="true"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
------------------activity_main2.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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.earl.lianxilianxi.Main2Activity"> <ImageView android:layout_width="match_parent" android:layout_height="350dp" android:id="@+id/big_img"/> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:id="@+id/title_tv"/> </LinearLayout>