Android实现新闻浏览功能

*要求:通过一个案例“新闻客户端”向大家演示AsyncHttpClient和
SmartImageView的综合使用。*
1:相关知识
市面上一些常见软件,例如手机QQ、天猫、京东商场等,都加载了大量网络上的图片。用Android自带的API实现这一功能十分麻烦而且耗时。为此,编程爱好者开发了一个开源项目——SmartImageView。
– 开源项目SmartImageView的出现主要是为了加速从网络上加载图片,它继承自ImageView类,支持根据URL地址加载图片、支持异步加载图片、支持图片缓存等。
2:设计布局文件
(1)主界面设计构造

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.newsshow.MainActivity">
`   <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/loading"
        android:visibility="invisible"
        android:layout_gravity="center"
        android:orientation="vertical"
        >
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="正在加载信息.......!" />
    </LinearLayout>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_news"></ListView>
</FrameLayout>

</LinearLayout>

如下图所示:
这里写图片描述
(2)ListView控件子布局 news_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="65dip">
    <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>

3:创建JSON文件,实现一个在线编辑器

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

4:创建实体类

package com.example.administrator.newsshow.entity;

/**
 * Created by Administrator on 2017/5/18.
 */

public class NewsInfo {
    private  String iconPath;
    private  String title;
    private  String description;
    private  int type;
    private  long comment;

    public String getTitle() {
        return title;
    }

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

    public String getIconPath() {
        return iconPath;
    }

    public void setIconPath(String iconPath) {
        this.iconPath = iconPath;
    }

    public String getDescription() {
        return description;
    }

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

    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;
    }
}

5:Tools包下创建工具类JsonParse负责解析JSON数据


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

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

}

6:创建适配器
adapter包下创建NewsAdapter类

package com.example.administrator.newsshow.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.administrator.newsshow.R;
import com.example.administrator.newsshow.entity.NewsInfo;
import com.loopj.android.image.SmartImageView;

import java.util.List;

/**
 * Created by Administrator on 2017/5/18.
 */

public class NewsAdapter  extends ArrayAdapter<NewsInfo>{
      public  NewsAdapter (Context context, List<NewsInfo>objects){
            super(context, R.layout.item,objects);
      }
    public View getView (int position, View convertView , ViewGroup parent){
         NewsInfo newsInfo =getItem(position);
         ViewHolder viewHolder;
        View view=null;
        if(convertView==null){
            view=View.inflate(getContext(),R.layout.item,null);
            viewHolder=new ViewHolder();
            viewHolder.siv=(SmartImageView)view.findViewById(R.id.siv_icon);
            viewHolder.tv_title=(TextView)view.findViewById(R.id.tv_title);
            viewHolder.tv_description=(TextView)view.findViewById(R.id.tv_description);
            viewHolder.tv_type=(TextView)view.findViewById(R.id.tv_type);
            view.setTag(viewHolder);

        }else {
            view =convertView;
            viewHolder=(ViewHolder)view.getTag();
        }
     viewHolder.siv.setImageUrl(newsInfo.getIconPath());
        viewHolder.tv_title.setText(newsInfo.getTitle());
        viewHolder.tv_description.setText(newsInfo.getDescription());
        viewHolder.tv_type.setText(newsInfo.getType()+"");
        return  view;
    }
    class  ViewHolder{
        SmartImageView siv;
    TextView tv_title;
        TextView tv_description;
        TextView tv_type;
    }
}

7:编写界面交互代码
界面逻辑代码的设计与实现:

package com.example.administrator.newsshow;

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.example.administrator.newsshow.Tools.JsonParse;
import com.example.administrator.newsshow.adapter.NewsAdapter;
import com.example.administrator.newsshow.entity.NewsInfo;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.image.SmartImageView;

import java.io.UnsupportedEncodingException;
import java.util.List;

import static com.example.administrator.newsshow.R.id.tv_title;

public class MainActivity extends AppCompatActivity {

     private ListView lv_news;
     private LinearLayout loading;
    private List<NewsInfo> newsInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_news= (ListView) findViewById(R.id.lv_news);
        loading= (LinearLayout) findViewById(R.id.loading);
        StartExecute();
    }
    private void  StartExecute(){
        System.out.println(">>>>>>>>>>>>>执行了");

        AsyncHttpClient asynchttpClient =new AsyncHttpClient();


        asynchttpClient.get("http://10.2.7.239:8080/NewsInfo.json",new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, org.apache.http.Header[] headers, byte[] bytes) {
                try {
                    String json = new String(bytes, "utf-8");
                    List<NewsInfo> newsInfos = JsonParse.getNewsInfo(json);

                    if (newsInfos == null) {
                        Toast.makeText(MainActivity.this, "解析失败!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "获取成功!", Toast.LENGTH_SHORT).show();
                        loading.setVisibility(View.INVISIBLE);
                        lv_news.setAdapter(new NewsAdapter(MainActivity.this, newsInfos));
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            public void onFailure(int i, org.apache.http.Header[] headers, byte[] bytes, Throwable throwable) {
                System.out.println(">>>>>>>>>获取失败!");
            }
            });
    }
}

8:运行结果示意图如下显示

这里写图片描述

  • 4
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值