Android在UI部分为应用程序开发人员提供了极大的便利和灵活性,在此就不一一列举了,本文拟通过一个小例子窥见一斑。
很多用过新浪微博手机客户端Android版本的童鞋想必都对其主界面的效果印象深刻,见下左图:
从图中可以看到,主体的列表框是个很复杂的部分,既要能显示头像、微博内容,又要能在微博内容中显示表情、图片、@某人、URL,这些元素混杂在一起,对于某些平台的UI开发来讲,简直太难了。但在Android上来开发,确实很容易实现,右图就是本程序的运行结果,重点展现了列表框部分的仿照。当然,所用的图片都是来自于新浪的了。
下面,我们就一起来看一下这个效果的代码实现。
首先,需要定义数据模型,主要的数据抽象是Site、Blog、User,分别代表网站、博文、用户,数据模型如下图所示:
具体成员的含义就不解释了。如果你没用过新浪微博,建议去用一下,或者可以参考http://open.weibo.com/中的开发文档。
这几个类的代码如下:
- view plaincopy to clipboardprint?
- package com.wenbin.test.site;
- public class User{
- private String profileImageUrl="http://tp3.sinaimg.cn/1500460450/50/1289923764/0";
- private String screenName="测试";
- private boolean verified=false;
- public User(){
- }
- public String getProfileImageUrl(){
- return profileImageUrl;
- }
- public String getScreenName(){
- return screenName;
- }
- public void setProfileImageUrl(String profileImageUrl) {
- this.profileImageUrl = profileImageUrl;
- }
- public void setScreenName(String screenName) {
- this.screenName = screenName;
- }
- public void setVerified(boolean verified) {
- this.verified = verified;
- }
- public boolean isVerified(){
- return verified;
- }
- }
- view plaincopy to clipboardprint?
- package com.wenbin.test.site;
- import java.util.Date;
- public class Blog implements Comparable<Blog>{
- private Date createAt=new Date(System.currentTimeMillis());
- private Blog retweetedBlog;
- private String text="就算把我打的遍体鳞伤也见不得会[泪]?http://blog.csdn.net/caowenbin @移动云_曹文斌 。";
- private String smallPic="";
- private String source="IE9";
- private User user;
- private Site site;
- public Blog(){
- }
- public Blog(Site site){
- this.site=site;
- }
- public boolean isHaveRetweetedBlog(){
- return retweetedBlog!=null;
- }
- public Blog getRetweetedBlog(){
- return retweetedBlog;
- }
- public String getText(){
- return text;
- }
- public User getUser(){
- return user;
- }
- public String getSmallPic(){
- return smallPic;
- }
- public void setRetweetedBlog(Blog retweetedBlog) {
- this.retweetedBlog = retweetedBlog;
- }
- public void setText(String text) {
- this.text = text;
- }
- public String getInReplyUserScreenName(){
- if (retweetedBlog!=null && retweetedBlog.getUser()!=null)
- return retweetedBlog.getUser().getScreenName();
- else
- return "";
- }
- public String getInReplyBlogText(){
- if (retweetedBlog!=null)
- return retweetedBlog.getText();
- else
- return "";
- }
- public void setPic(String smallPic){
- this.smallPic=smallPic;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public int compareTo(Blog another) {
- int ret=0;
- if (this.createAt.before(another.createAt)){
- ret=-1;
- }
- else if (this.createAt.after(another.createAt)){
- ret=1;
- }
- else{
- ret=0;
- }
- return ret;
- }
- public void setSource(String source) {
- this.source = source;
- }
- public String getSource() {
- return source;
- }
- public void setSite(Site site) {
- this.site = site;
- }
- public Site getSite() {
- return site;
- }
- }
- view plaincopy to clipboardprint?
- package com.wenbin.test.site;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeSet;
- public abstract class Site{
- protected Set<Blog> blogs=new TreeSet<Blog>();
- protected String name;
- protected Map<String,String> faceMap=new HashMap<String,String>();
- public Site() {
- onConstruct();
- }
- protected abstract void onConstruct();
- public Map<String, String> getFaceMap() {
- return faceMap;
- }
- public Set<Blog> getBlogs(){
- return blogs;
- }
- public long getBlogsCount(){
- return blogs.size();
- }
- public void addBlog(Blog blog){
- blogs.add(blog);
- }
- public void removeBlog(Blog blog){
- blogs.remove(blog);
- }
- public Iterator<Blog> getBlogsIterator(){
- return blogs.iterator();
- }
- public void clearBlogs(){
- blogs.clear();
- }
- public String getName(){
- return name;
- }
- }
- view plaincopy to clipboardprint?
- package com.wenbin.test.site;
- public class SinaSite extends Site {
- protected void onConstruct(){
- name="新浪微博";
- initFaceMap();
- }
- private void initFaceMap(){
- faceMap.put("[呵呵]", "hehe");
- faceMap.put("[嘻嘻]", "xixi");
- faceMap.put("[哈哈]", "haha");
- faceMap.put("[爱你]", "aini");
- faceMap.put("[晕]", "yun");
- faceMap.put("[泪]", "lei");
- }
- }
先熟悉一下这些代码,下次就利用这些数据来制作基本的列表框。
【编辑推荐】