1) activity_main:
<?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" tools:context=".MainActivity" android:orientation="horizontal"> <WebView android:id="@+id/wb" android:layout_width="match_parent" android:layout_height="match_parent"> </WebView> <FrameLayout android:layout_height="match_parent" android:layout_width="match_parent"> <LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" android:visibility="invisible" android:gravity="center" android:id="@+id/loading"> <ProgressBar android:layout_height="wrap_content" android:layout_width="wrap_content"/> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="正在加载信息..."/> </LinearLayout> <ListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/lv_news"/> </FrameLayout> </LinearLayout>
2) news_item:
<?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"> <RelativeLayout 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"> </RelativeLayout> <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_description" 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_type" 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>
3) MainActivity:
package com.example.chap9; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ListView; public class MainActivity extends AppCompatActivity { private WebView wb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wb= (WebView) findViewById(R.id.wb); //让webview控件支持js脚本 wb.getSettings().setJavaScriptEnabled(true); //当需要从一个网页跳转到另外一个网页时, // 目标网页仍然在wbview控件中显示,而不是打开浏览器 wb.setWebViewClient(new WebViewClient()); wb.loadUrl("http://www.baidu.com"); } }
4) JsonParse:
package com.example.chap9; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.List; //解析json数据的工具类 public class JsonParse { public static List<NewsInfo> getJson(String json){ Gson gson=new Gson(); Type listType=new TypeToken<List<NewsInfo>>(){}.getType();//泛型 List<NewsInfo> list=gson.fromJson(json,listType); return null; } }
5)NesInfo:
package com.example.chap9; public class NewsInfo { private String icon; private String title; private String content; private String type; private String comment; public NewsInfo(String icon, String title, String content, String type, String comment) { this.icon = icon; this.title = title; this.content = content; this.type = type; this.comment = 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 String getType() { return type; } public void setType(String type) { this.type = type; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } }
6) AndroidManifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.chap9"> <!-- 申请权限--> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Chap9"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>