public class Fragment01 extends Fragment {
private static final String TAG = "Fragment01----";
private MyHandler myHandler = new MyHandler();
private final static int SUCCESS = 0;
private final static int ERROR = 1;
private ListView listView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment01_layout, container, false);
listView = view.findViewById(R.id.fragment01_listview);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//4.获取网络数据
new Thread() {
@Override
public void run() {
try {
URL u = new URL(HttpConfig.ONE_URL);
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
connection.setConnectTimeout(5000);
if (connection.getResponseCode() == 200) {
InputStream inputStream = connection.getInputStream();
//5.封装工具类
String json = CommenUtils.inputStream2String(inputStream);
//6.使用Handler,发送数据
Message message = myHandler.obtainMessage();
message.what = SUCCESS;
message.obj = json;
myHandler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SUCCESS:
//成功
String json = (String) msg.obj;
Log.d(TAG, "handleMessage: " + json);
//导包Gson
Gson gson = new Gson();
//如果json就是一个纯属组的解析方式
Type listType = new TypeToken
LinkedList
//8.书写Adapter进行显示
List
MyAdapter myAdapter = new MyAdapter(getActivity(), item);
listView.setAdapter(myAdapter);
break;
}
}
}
}
public class Fragment02 extends Fragment {
private static final String TAG = "Fragment02";
private TextView name, address;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment02_layout, container, false);
name = view.findViewById(R.id.fragment02_name);
address = view.findViewById(R.id.fragment02_add);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//1.请求网络
/**
* 记得要添加HttpClient的依赖库 百度
*/
new MyTask().execute(HttpConfig.TWO_URL);
}
class MyTask extends AsyncTask
@Override
protected String doInBackground(String... strings) {
String url = strings[0];
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
//执行
try {
HttpResponse response = client.execute(get);
//状态码
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
//将流转成String
String json = CommenUtils.inputStream2String(inputStream);
Log.d(TAG, "run: " + json);
//发送
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
UserBean userBean = gson.fromJson(s, UserBean.class);
//赋值
name.setText(userBean.getData().getName());
address.setText(userBean.getData().getAddr());
}
}
}
public class MyAdapter extends BaseAdapter {
private Context context;
private List
public MyAdapter(Context context, List
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder myViewHolder=null;
if (convertView==null){
convertView=View.inflate(context, R.layout.fragment01_item,null);
CircleImageView circleImageView=convertView.findViewById(R.id.item_pic);
TextView textView=convertView.findViewById(R.id.item_title);
myViewHolder=new MyViewHolder(circleImageView,textView);
convertView.setTag(myViewHolder);
}
else {
myViewHolder= (MyViewHolder) convertView.getTag();
}
//赋值
myViewHolder.getTv().setText(list.get(position).getTitle());
String thumbnail = list.get(position).getThumbnail();
Glide.with(context).load(thumbnail).into(myViewHolder.getPic());
return convertView;
}
class MyViewHolder {
private CircleImageView pic;
private TextView tv;
public MyViewHolder(CircleImageView pic, TextView tv) {
this.pic = pic;
this.tv = tv;
}
public CircleImageView getPic() {
return pic;
}
public void setPic(CircleImageView pic) {
this.pic = pic;
}
public TextView getTv() {
return tv;
}
public void setTv(TextView tv) {
this.tv = tv;
}
}
}
package com.daydayup.zhoukao1.utils;
import java.io.IOException;
import java.io.InputStream;
/**
* 公共的工具类
*/
public class CommenUtils {
//将输入流转成字符串
public static String inputStream2String(InputStream inputStream) throws IOException {
int len = 0;
byte[] butter = new byte[1024];
StringBuffer stringBuffer = new StringBuffer();
while ((len = inputStream.read(butter)) != -1) {
String s = new String(butter, 0, len,"UTF-8");
stringBuffer.append(s);
}
return stringBuffer.toString();
}
}
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".acitivities.MainActivity">
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
android:layout_height="0.75dp"
android:background="#999999" />
android:layout_height="50dp"
android:orientation="horizontal">
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="页面一"
android:textSize="30sp" />
android:layout_height="match_parent">
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="页面二"
android:textSize="30sp" />
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="标题"
android:textSize="20sp" />
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="页面二"
android:textColor="#000"
android:textSize="30sp" />
android:layout_height="0.75dp"
android:background="#999999" />
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:layout_height="wrap_content" />