Lesson38
- 理论
- 代码部分
(1)AndroidManifest.xml
网络权限
(2)activity_main.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"
android:orientation="vertical"
tools:context="computer.class1.appnews.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载信息..."/>
</LinearLayout>
<ListView
android:id="@+id/lv_news"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</FrameLayout>
</LinearLayout>
(3)list_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="65dp">
<com.loopj.android.image.SmartImageView
android:id="@+id/siv_icon"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/siv_icon"
android:ellipsize="end"
android:maxLength="20"
android:singleLine="true"
android:text="我是标题"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/siv_icon"
android:ellipsize="end"
android:maxLength="16"
android:singleLine="true"
android:text="我是描述"
android:textColor="#99000000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:text="我是评论"
android:textColor="#99000000"
android:textSize="12sp" />
</RelativeLayout>
(4)Utils
package lesson10.computer.lesson38;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
/**
* Created by dell-pc on 2020/12/4.
*/
public class Utils {
public static List<NewsInfo> jaonParser(String json){
Gson gson = new Gson();
Type type = new TypeToken<List<NewsInfo>>() {
}.getType();
List<NewsInfo> lists = gson.fromJson(json, type);//从哪里解析
return lists;
}
}
(5)NewsInfo
package lesson10.computer.lesson38;
/**
* Created by dell-pc on 2020/12/4.
*/
public class NewsInfo {
private String icon; //图片路径
private String title; //新闻标题
private String content; //新闻描述
private int type; //新闻类型 1:普通新闻 2:专题 3:Live
public NewsInfo(String icon, String title, String content, int type, long comment) {
this.icon = icon;
this.title = title;
this.content = content;
this.type = type;
this.comment = comment;
}
private long comment; //新闻评论数
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public long getComment() {
return comment;
}
public void setComment(long comment) {
this.comment = comment;
}
}
(6)MainActivity
package lesson10.computer.lesson38;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.image.SmartImageView;
import org.apache.http.Header;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<NewsInfo> newsInfos;
private ListView lvNews;
private LinearLayout loading;
//private List<>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
fillData();
}
private void initView() {
lvNews = (ListView) findViewById(R.id.lv_news);
loading = (LinearLayout) findViewById(R.id.loading);
}
private void fillData() {
AsyncHttpClient client = new AsyncHttpClient();
//第一个参数网址,第二个参数回调,在回调里更新画面
client.get(getString(R.string.serverur1), new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, Header[] headers, byte[] bytes) {
String json = null;
try {
json = new String(bytes,"utf-8");
newsInfos = Utils.jaonParser(json);//进行解析
//给ListView绑定数据
//创建Adapter
loading.setVisibility(View.INVISIBLE);
lvNews.setAdapter(new Myadapter());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "获取数据失败!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
}
});
}
//创建自定义的适配器
private class Myadapter extends BaseAdapter{
@Override
public int getCount() {
return newsInfos != null ? newsInfos.size() : 0;//个数
}
@Override
public Object getItem(int position) {
return newsInfos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//动态加载Layout
View view = View.inflate(MainActivity.this, R.layout.list_item, null);
SmartImageView sivIcon = (SmartImageView) view.findViewById(R.id.siv_icon);
TextView title = (TextView) view.findViewById(R.id.tv_title);
TextView content = (TextView) view.findViewById(R.id.tv_content);
TextView comment = (TextView) view.findViewById(R.id.tv_comment);
NewsInfo news = newsInfos.get(position);
sivIcon.setImageUrl(news.getIcon(),R.mipmap.ic_launcher,R.mipmap.ic_launcher);
title.setText(news.getTitle());
content.setText(news.getContent());
//新闻类型[
int type = news.getType();
//1:普通新闻 2:专题 3:LIVE
switch (type){
case 1:
comment.setText("评论"+news.getComment());
break;
case 2:
comment.setText("专题");
comment.setTextColor(Color.RED);
break;
case 3:
comment.setText("LIVE");
comment.setTextColor(Color.BLUE);
break;
}
return view;
}
}
}