android学习:模拟微博看看界面

来源:互联网 发布:mac 已经更新 版本没变 编辑:程序博客网 时间:2024/05/16 13:05
这几天学会了ListView组件,这个组件真的很有用。希望在今后的开发中能大量用到。

通过ListView的学习,模拟了新浪微博的随便看看页面。

要实现的效果:

代码如下:

[html] view plaincopyprint?
  1. <span style="font-family:Comic Sans MS;font-size:12px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/liner"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.     <LinearLayout   
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:padding="6dip"  
  11.         android:background="#CD4F39">  
  12.         <TextView   
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:text="新浪微博-随便看看"  
  16.             android:textSize="15sp"  
  17.               
  18.             android:textColor="@android:color/white"  
  19.             />  
  20.     </LinearLayout>  
  21.   
  22.     <com.bzu.edu.gxs.view.MyListView  
  23.         android:id="@+id/myListView1"  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content" >  
  26.     </com.bzu.edu.gxs.view.MyListView>  
  27. </LinearLayout></span>  
      

         layout_view.xml

         

    代码如下:

[html] view plaincopyprint?
  1. <span style="font-family:Comic Sans MS;font-size:12px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:padding="10dip"  
  6.     android:orientation="vertical" >  
  7.     <RelativeLayout   
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         >  
  11.         <ImageView   
  12.             android:id="@+id/head_img"  
  13.             android:src="@drawable/p1"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:layout_marginTop="5dip"  
  17.             android:layout_alignParentLeft="true"  
  18.             />  
  19.         <LinearLayout   
  20.             android:layout_width="match_parent"  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_toRightOf="@+id/head_img"  
  23.             android:layout_marginLeft="7dip"  
  24.             android:orientation="vertical">  
  25.             <RelativeLayout   
  26.                 android:layout_width="match_parent"  
  27.                 android:layout_height="wrap_content">  
  28.                 <TextView   
  29.                     android:id="@+id/name"  
  30.                     android:layout_width="wrap_content"  
  31.                     android:layout_height="wrap_content"  
  32.                     android:layout_alignParentLeft="true"  
  33.                     android:textColor="#242424"  
  34.                     android:textSize="14sp"  
  35.                     android:typeface="sans"  
  36.                     android:textStyle="bold"  
  37.                     android:text="@string/gxs"/>  
  38.                 <TextView   
  39.                     android:id="@+id/time"  
  40.                     android:layout_width="wrap_content"  
  41.                     android:layout_height="wrap_content"  
  42.                     android:layout_toRightOf="@id/name"  
  43.                     android:layout_alignBottom="@id/name"  
  44.                     android:layout_marginLeft="5dip"  
  45.                     android:textColor="#FFFFFF"  
  46.                     android:textSize="12sp"  
  47.                     android:text=""/>  
  48.                   
  49.                  <TextView   
  50.                     android:id="@+id/rq"  
  51.                     android:layout_width="wrap_content"  
  52.                     android:layout_height="wrap_content"  
  53.                     android:layout_alignParentRight="true"  
  54.                     android:layout_centerVertical="true"  
  55.                     android:textSize="13sp"  
  56.                     android:text="1分钟前"/>  
  57.             </RelativeLayout>  
  58.             <LinearLayout   
  59.                 android:layout_width="match_parent"  
  60.                 android:layout_height="wrap_content"  
  61.                 android:layout_marginTop="5dip">  
  62.                 <TextView   
  63.                     android:id="@+id/article"  
  64.                     android:layout_width="match_parent"  
  65.                     android:layout_height="wrap_content"  
  66.                     android:minHeight="50px"  
  67.                     android:textSize="12sp"  
  68.                     android:text="gxs"/>  
  69.             </LinearLayout>  
  70.         </LinearLayout>  
  71.     </RelativeLayout>  
  72.       
  73.   
  74. </LinearLayout>  
  75. </span>  

5.java代码

         MainActivity.java

[html] view plaincopyprint?
  1. <span style="font-family:Comic Sans MS;font-size:12px;">package com.bzu.gxs;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.ArrayList;  
  5. import java.util.Date;  
  6. import java.util.List;  
  7. import java.util.Random;  
  8. import com.bzu.edu.gxs.adapter.ArticleAdapter;  
  9. import com.bzu.edu.gxs.adapter.Messages;  
  10. import com.bzu.edu.gxs.view.MyListView;  
  11. import com.bzu.gxs.*;  
  12. import android.R.integer;  
  13. import android.os.Bundle;  
  14. import android.os.Handler;  
  15. import android.os.Message;  
  16.   
  17.   
  18. import android.app.Activity;  
  19. import android.content.res.TypedArray;  
  20. import android.view.Menu;  
  21. import android.view.Window;  
  22. import android.widget.ArrayAdapter;  
  23. import android.widget.BaseAdapter;  
  24. import android.widget.LinearLayout;  
  25.   
  26. public class MainActivity extends Activity {  
  27.       
  28.     private MyListView mylistview=null;  
  29.     private List<Messages> list_msg=null;  
  30.     private BaseAdapter adapter=null;  
  31.     @Override  
  32.     protected void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  35.         setContentView(R.layout.activity_main);  
  36.           
  37.         init();  
  38.         if(list_msg==null){  
  39.             getData();  
  40.         }  
  41.               
  42.         adapter=new ArticleAdapter(this,list_msg);  
  43.         //设置监听器  
  44.         mylistview.setOnRershListener(new OnRershListener());  
  45.         mylistview.setAdapter(adapter);  
  46.     }  
  47.           
  48.     private void getData() {  
  49.         // TODO Auto-generated method stub  
  50.         //获得xml的资源  
  51.         String names[];  
  52.         String article[];  
  53.         TypedArray img;  
  54.         int i;        
  55.         names=getResources().getStringArray(R.array.name);  
  56.         article=getResources().getStringArray(R.array.article);  
  57.         img=getResources().obtainTypedArray(R.array.head_photo);  
  58.           
  59.         list_msg=new ArrayList<Messages>();  
  60.         for(i=0;i<names.length;i++){  
  61.             Messages message=new Messages();  
  62.             message.setImg(img.getDrawable(i));  
  63.             message.setName(names[i]);  
  64.             message.setArticel(article[i]);  
  65.             message.setRq(String.valueOf(new Random().nextInt(120)+"分钟前"));  
  66.             Date date=new Date();  
  67.             SimpleDateFormat simple=new SimpleDateFormat("MM-dd");  
  68.             message.setTime(simple.format(date));  
  69.             list_msg.add(message);  
  70.         }  
  71.     }  
  72.     private void init() {  
  73.         // TODO Auto-generated method stub  
  74.         mylistview=(MyListView)findViewById(R.id.myListView1);  
  75.     }  
  76.   
  77.     @Override  
  78.     public boolean onCreateOptionsMenu(Menu menu) {  
  79.       
  80.         getMenuInflater().inflate(R.menu.main, menu);  
  81.         return true;  
  82.     }  
  83.       
  84.   
  85.     Handler handler=new Handler(new Handler.Callback() {  
  86.         @Override  
  87.         public boolean handleMessage(Message arg0) {  
  88.             // TODO Auto-generated method stub  
  89.             if(arg0.what==0){                 
  90.                 //结束刷新  
  91.                 mylistview.endOnRersh();  
  92.             }  
  93.             return false;  
  94.         }  
  95.     });  
  96.       
  97.     //刷新接口实现类  
  98.     class OnRershListener implements OnRersh{  
  99.         //实现刷新接口的 方法,,在MyList中会回调该方法。  
  100.         //模拟  
  101.         @Override  
  102.         public void OnRershListener() {  
  103.             // TODO Auto-generated method stub  
  104.             new Thread(new Run()).start();//启动一个线程  
  105.         }  
  106.     }  
  107.   
  108.     //线程接口实现类  
  109.     class Run implements Runnable{  
  110.         @Override  
  111.         public void run() {  
  112.             // TODO Auto-generated method stub  
  113.             try {  
  114.                 Thread.sleep(2000);  
  115.             } catch (Exception e) {  
  116.                 // TODO: handle exception  
  117.                 e.printStackTrace();  
  118.             }  
  119.             handler.sendEmptyMessage(0);  
  120.         }  
  121.     }  
  122.   
  123. }  
  124.   
  125. </span>  

            ArticleAdapter.java

[html] view plaincopyprint?
  1. <span style="font-family:Comic Sans MS;font-size:12px;">package com.bzu.edu.gxs.adapter;  
  2.   
  3. import java.util.List;  
  4. import com.bzu.gxs.*;  
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.ImageView;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.TextView;  
  13.   
  14. public class ArticleAdapter extends BaseAdapter {  
  15.     private List<Messages> list_mes=null;  
  16.     private Context context;  
  17.       
  18.     public ArticleAdapter(Context context,List<Messages> list_mes) {  
  19.         // TODO Auto-generated constructor stub  
  20.         this.list_mes=list_mes;  
  21.         this.context=context;  
  22.     }  
  23.   
  24.     @Override  
  25.     public int getCount() {  
  26.         // TODO Auto-generated method stub  
  27.         return list_mes.size();  
  28.     }  
  29.   
  30.     @Override  
  31.     public Object getItem(int position) {  
  32.         // TODO Auto-generated method stub  
  33.         return list_mes.get(position);  
  34.     }  
  35.   
  36.     @Override  
  37.     public long getItemId(int position) {  
  38.         // TODO Auto-generated method stub  
  39.         return position;  
  40.     }  
  41.   
  42.     @Override  
  43.     public View getView(int position, View convertView, ViewGroup parent) {  
  44.         // TODO Auto-generated method stub  
  45.         ListItem listitem;  
  46.         if(convertView==null)  
  47.         {  
  48.             convertView=(LinearLayout)LayoutInflater.from(context).inflate(R.layout.layout_view, null);  
  49.               
  50.             listitem=new ListItem();  
  51.             listitem.img=(ImageView)convertView.findViewById(R.id.head_img);  
  52.             listitem.name=(TextView)convertView.findViewById(R.id.name);  
  53.             //显示当前日期  
  54.             listitem.time=(TextView)convertView.findViewById(R.id.time);  
  55.             listitem.rq=(TextView)convertView.findViewById(R.id.rq);  
  56.             listitem.article=(TextView)convertView.findViewById(R.id.article);  
  57.             convertView.setTag(listitem);  
  58.         }else {  
  59.             listitem=(ListItem)convertView.getTag();  
  60.         }  
  61.           
  62.         listitem.img.setImageDrawable(list_mes.get(position).getImg());  
  63.         listitem.name.setText(list_mes.get(position).getName());  
  64.         listitem.time.setText(list_mes.get(position).getTime());  
  65.         listitem.rq.setText(list_mes.get(position).getRq());  
  66.         listitem.article.setText(list_mes.get(position).getArticel());  
  67.         return convertView;  
  68.     }  
  69.       
  70.     public class ListItem{  
  71.         ImageView img;  
  72.         TextView name;  
  73.         TextView time;  
  74.         TextView rq;  
  75.         TextView article;  
  76.     }  
  77. }  
  78. </span>  

          Messages.java

[html] view plaincopyprint?
  1. <span style="font-family:Comic Sans MS;font-size:12px;">package com.bzu.edu.gxs.adapter;  
  2.   
  3. import android.graphics.drawable.Drawable;  
  4.   
  5. public class Messages {  
  6.     private Drawable img;  
  7.     private String name;  
  8.     private String time;  
  9.     private String rq;  
  10.     private String articel;  
  11.       
  12.     public Drawable getImg() {  
  13.         return img;  
  14.     }  
  15.     public void setImg(Drawable img) {  
  16.         this.img = img;  
  17.     }  
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.     public String getTime() {  
  25.         return time;  
  26.     }  
  27.     public void setTime(String time) {  
  28.         this.time = time;  
  29.     }  
  30.     public String getRq() {  
  31.         return rq;  
  32.     }  
  33.     public void setRq(String rq) {  
  34.         this.rq = rq;  
  35.     }  
  36.     public String getArticel() {  
  37.         return articel;  
  38.     }  
  39.     public void setArticel(String articel) {  
  40.         this.articel = articel;  
  41.     }  
  42.       
  43.   
  44. }</span><span style="font-family:Comic Sans MS;">  
  45. </span> 

0 0
原创粉丝点击