新闻客户端

1:创建一个名为“新闻客户端”的应用程序,设计用户交互界面如图所示:
新闻客户端_第1张图片

布局文件activity_main.xml代码如下:


<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=".MainActivity"
    android:orientation="vertical">

    <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:gravity="center"
            android:orientation="vertical"
            android:visibility="invisible">
            <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"
            android:layout_marginTop="5dp"/>
    FrameLayout>
LinearLayout>

上述代码创建了新闻客户端的主界面,这个界面主要包含了提示用户数据正在加载中的ProgressBar,TextView以及用于展示新闻信息的ListView.
2:ListView的Item布局文件news_item.xml如下所示:


<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:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">com.loopj.android.image.SmartImageView>
    <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:maxLines="1"
        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>

图形化界面如图所示:
新闻客户端_第2张图片
上述布局文件使用到了自定义控件SmartImageView和三个分别用于展示新闻标题,新闻内容以及新闻评论数的TextView.
3:编写界面交互代码

public class Main2Activity extends AppCompatActivity {
    private ListView listView;
    private LinearLayout loading;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        listView=(ListView)findViewById(R.id.lv_news);
        loading=(LinearLayout)findViewById(R.id.loading);
        fillData();
    }
    private void fillData(){

        AsyncHttpClient asyncHttpClient=new AsyncHttpClient();
        asyncHttpClient.get("http://10.61.13.164:8080/NewsInfo.json", new AsyncHttpResponseHandler() {
            @Override
                    public void onSuccess(int i,org.apache.http.Header[] headers, byte[] bytes) {
                        String json;
                        try {
                    json = new String(bytes,"utf-8");

                List newsInfos= JsonParse.getNewsInfo(json);
                     if (newsInfos==null){
                         Toast.makeText(Main2Activity.this,"解析失败",Toast.LENGTH_LONG).show();
                     }else{
                         loading.setVisibility(View.INVISIBLE);
                         listView.setAdapter(new MyAdapter(Main2Activity.this,newsInfos));
                     }
                } catch (Exception e) {
                            e.printStackTrace();
                        }
            }

            @Override
            public void onFailure(int i,org.apache.http.Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }
}

4:创建NewsInfo类:

public class NewsInfo {
    private String icon;
    private String title;
    private String content;
    private int type;
    private long comment;

    public long getComment() {
        return comment;
    }

    public void setComment(long comment) {
        this.comment = comment;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String description) {
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }
}

5:创建工具类解析xml文件

public class JsonParse {
    public static List getNewsInfo(String json)
    {
        Gson gson=new Gson();
        Type listType=new TypeToken>(){

        }.getType();
        List newsInfos=gson.fromJson(json,listType);
        return newsInfos;
    }
}

6:配置服务器:

[
  {
    "icon": "http://10.61.13.164:8080/images/a.PNG",
    "title": "科技温暖世界",
    "content": "进入一个更有爱的领域",
    "type": "1",
    "comment": "69"
  },
  {
    "icon": "http://10.3.24.180:8080/images/b.PNG",
    "title": "《神武》",
    "content": "新美术资源盘点,视觉新体验",
    "type": "2",
    "comment": "35"
  },
  {
    "icon": "http://10.3.24.180:8080/images/c.PNG",
    "title": "南北车正式公布合并",
    "content": "南北车将于今日正式公布合并",
    "type": "3",
    "comment": "2"
  },
  {
    "icon": "http://10.3.24.180:8080/images/d.PNG",
    "title": "萌呆了!汪星人抱玩偶酣睡",
    "content": "汪星人抱玩偶酣睡,萌翻网友",
    "type": "1",
    "comment": "25"
  },
  {
    "icon": "http://10.3.24.180:8080/images/e.PNG",
    "title": "风力发电进校园",
    "content": "风力发电普进校园",
    "type": "2",
    "comment": "26"
  },
  {
    "icon": "http://10.3.24.180:8080/images/f.PNG",
    "title": "地球一小时",
    "content": "地球熄灯一小时",
    "type": "1",
    "comment": "23"
  },
  {
    "icon": "http://10.3.24.180:8080/images/g.PNG",
    "title": "最美公路",
    "content": "最美公路,难以想象",
    "type": "1",
    "comment": "23"
  }
]

7:添加权限:
本案例需要访问网络因此需要在AndroidMainfest.xml里面配置相应的权限:

<uses-permission android:name="android.permission.INTERNET" />

8:运行程序查看新闻:
新闻客户端_第3张图片

你可能感兴趣的