当前位置: 移动技术网 > IT编程>移动开发>Android > Android模拟实现网易新闻客户端

Android模拟实现网易新闻客户端

2019年07月24日  | 移动技术网IT编程  | 我要评论

恶龙军团优等生1,本溪洋湖沟,天使出更国语

首先我们先看一下要模拟的界面

我们主要实现的就是listview解析json文件中的数据,ui布局很简单不做赘述。
这里我们需要一个服务器来实现数据的动态更新, 这里我们用到的是tomcat8.0。
首先我们把需要解析的json文件放置到tomcat的webapp文件下的root里面,方便我们解析。

首先我们创建一个jsonparse类用来解析json文件:

package cn.edu.bzu.myapplication.tools;

import com.google.gson.gson;
import com.google.gson.reflect.typetoken;

import java.lang.reflect.type;
import java.util.list;

import cn.edu.bzu.myapplication.entity.newsinfo;

/**
 * created by becauseshy on 2017/5/18.
 */

public class jsonparse {
 public static list<newsinfo> getnewinfo(string json){
  gson gson=new gson();
  type listtype=new typetoken<list<newsinfo>>(){

  }.gettype();
  list<newsinfo> newsinfos=gson.fromjson(json,listtype);


  return newsinfos;
 }
}

创建json文件的实体类:

package cn.edu.bzu.myapplication.entity;

/**
 * created by becauseshy on 2017/5/17.
 */

public class newsinfo {
 private string iconpath;
 private string title;
 private string description;
 private int type;
 private long comment;

 public string geticonpath() {
  return iconpath;
 }

 public void seticonpath(string iconpath) {
  this.iconpath = iconpath;
 }

 public string gettitle() {
  return title;
 }

 public void settitle(string title) {
  this.title = title;
 }

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

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"
 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" />
 </framelayout>
</linearlayout>

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">
 <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>

适配器代码:

package cn.edu.bzu.myapplication.adapter;

/**
 * created by becauseshy on 2017/5/17.
 */
import android.content.context;
import android.graphics.color;
import android.support.annotation.nonnull;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.arrayadapter;
import android.widget.textview;

import com.loopj.android.image.smartimageview;

import java.util.list;

import cn.edu.bzu.myapplication.r;
import cn.edu.bzu.myapplication.entity.newsinfo;


public class newadapter extends arrayadapter<newsinfo>{
private int resourceid;
 public newadapter(context context, int resource, list<newsinfo> objects) {
  super(context, resource, objects);
  resourceid=resource;
 }

 @override
 public view getview(int position, view convertview, viewgroup parent) {
  newsinfo fruit=getitem(position);
  view view;
  viewholder viewholder;
  if(convertview==null){
    view=layoutinflater.from(getcontext()).inflate(resourceid,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(fruit.geticonpath(),r.drawable.a,r.drawable.ic_launcher);
  viewholder.tv_title.settext(fruit.gettitle());
  viewholder.tv_description.settext(fruit.getdescription());
  int type=fruit.gettype();
  switch (type){

   case 1:
    viewholder.tv_type.settext("评论:"+fruit.getcomment());
    viewholder.tv_type.settextcolor(color.blue);
    break;
   case 2:
    viewholder.tv_type.settext("专题");
    viewholder.tv_type.settextcolor(color.black);
    break;
   case 3:
    viewholder.tv_type.settext("live");
    viewholder.tv_type.settextcolor(color.red);
    break;
  }

  return view;

 }
 class viewholder{
  smartimageview siv;
  textview tv_title;
  textview tv_description;
  textview tv_type;
 }
}

mainactivity实现代码:

package cn.edu.bzu.myapplication;

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.linearlayout;
import android.widget.listview;
import android.widget.toast;

import com.loopj.android.http.asynchttpclient;
import com.loopj.android.http.asynchttpresponsehandler;

import java.io.unsupportedencodingexception;
import java.util.arraylist;
import java.util.list;

import cn.edu.bzu.myapplication.tools.jsonparse;
import cn.edu.bzu.myapplication.adapter.newadapter;
import cn.edu.bzu.myapplication.entity.newsinfo;
import cn.edu.bzu.myapplication.model.fruit;

public class mainactivity extends appcompatactivity {
 private listview iv_news;
 private newadapter newadapter;
 private list<newsinfo> newinfos;
 private linearlayout loading;
 private jsonparse jsonparse;


 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  iv_news= (listview) findviewbyid(r.id.lv_news);
  newadapter =new newadapter(this,r.layout.news_item,newinfos);
  loading= (linearlayout) findviewbyid(r.id.loading);
  preparedata();


 }

 private void preparedata() {
  //fruitlist=new arraylist<>();
  //fruit apple=new fruit("apple",r.drawable.apple_pic);
  // fruitlist.add(apple);
  asynchttpclient asynchttpclient=new asynchttpclient();
  asynchttpclient.get(getstring(r.string.serverurl), new asynchttpresponsehandler() {

   @override
   public void onsuccess(int i, cz.msebera.android.httpclient.header[] headers, byte[] bytes) {
    try {
     string json=new string(bytes,"utf-8");
     newinfos=jsonparse.getnewinfo(json);
     if(newinfos==null){
      toast.maketext(mainactivity.this,"解析失败",toast.length_short).show();
     }
     else {
      loading.setvisibility(view.invisible);
      iv_news.setadapter(newadapter);


     }
    } catch (unsupportedencodingexception e) {
     e.printstacktrace();
    }


   }

   @override
   public void onfailure(int i, cz.msebera.android.httpclient.header[] headers, byte[] bytes, throwable throwable) {
    toast.maketext(mainactivity.this,"请求失败",toast.length_short).show();

   }
  });


 }

}

在values文件加下的string.xml文件中添加:

<string name="serverurl">http://172.16.26.58:8080/newinfo.xml</string>

最后一定不要忘了添加网络访问权限, 这很重要, 好多同学都犯了这个错误、

这样基本功能就实现了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网