1.导语
这几天学会了ListView组件,这个组件真的很有用。希望在今后的开发中能大量用到。
通过ListView的学习,模拟了新浪微博的随便看看页面。
程序效果如下:
2.源码下载
源码下载地址:
(1)GitHub地址:https://github.com/GXS1225/ListView.git
(2)CSDN地址:http://download.csdn.net/detail/gxseveryday/8671541
3.代码分析
此文件自定义一个adapter,继承自BaseAdapter,实现getView方法,在此方法中定义了每个item的显示样式。
知识点:
4.xml布局代码
步骤:
我们先添加两个xml文件:
代码如下:
<span style="font-family:Comic Sans MS;font-size:12px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/liner" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="6dip" android:background="#CD4F39"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新浪微博-随便看看" android:textSize="15sp" android:textColor="@android:color/white" /> </LinearLayout> <com.bzu.edu.gxs.view.MyListView android:id="@+id/myListView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </com.bzu.edu.gxs.view.MyListView> </LinearLayout></span>
<span style="font-family:Comic Sans MS;font-size:12px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dip" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/head_img" android:src="@drawable/p1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dip" android:layout_alignParentLeft="true" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/head_img" android:layout_marginLeft="7dip" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:textColor="#242424" android:textSize="14sp" android:typeface="sans" android:textStyle="bold" android:text="@string/gxs"/> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/name" android:layout_alignBottom="@id/name" android:layout_marginLeft="5dip" android:textColor="#FFFFFF" android:textSize="12sp" android:text=""/> <TextView android:id="@+id/rq" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:textSize="13sp" android:text="1分钟前"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dip"> <TextView android:id="@+id/article" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="50px" android:textSize="12sp" android:text="gxs"/> </LinearLayout> </LinearLayout> </RelativeLayout> </LinearLayout> </span>
<span style="font-family:Comic Sans MS;font-size:12px;">package com.bzu.gxs; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; import com.bzu.edu.gxs.adapter.ArticleAdapter; import com.bzu.edu.gxs.adapter.Messages; import com.bzu.edu.gxs.view.MyListView; import com.bzu.gxs.*; import android.R.integer; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.content.res.TypedArray; import android.view.Menu; import android.view.Window; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.LinearLayout; public class MainActivity extends Activity { private MyListView mylistview=null; private List<Messages> list_msg=null; private BaseAdapter adapter=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); init(); if(list_msg==null){ getData(); } adapter=new ArticleAdapter(this,list_msg); //设置监听器 mylistview.setOnRershListener(new OnRershListener()); mylistview.setAdapter(adapter); } private void getData() { // TODO Auto-generated method stub //获得xml的资源 String names[]; String article[]; TypedArray img; int i; names=getResources().getStringArray(R.array.name); article=getResources().getStringArray(R.array.article); img=getResources().obtainTypedArray(R.array.head_photo); list_msg=new ArrayList<Messages>(); for(i=0;i<names.length;i++){ Messages message=new Messages(); message.setImg(img.getDrawable(i)); message.setName(names[i]); message.setArticel(article[i]); message.setRq(String.valueOf(new Random().nextInt(120)+"分钟前")); Date date=new Date(); SimpleDateFormat simple=new SimpleDateFormat("MM-dd"); message.setTime(simple.format(date)); list_msg.add(message); } } private void init() { // TODO Auto-generated method stub mylistview=(MyListView)findViewById(R.id.myListView1); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } Handler handler=new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message arg0) { // TODO Auto-generated method stub if(arg0.what==0){ //结束刷新 mylistview.endOnRersh(); } return false; } }); //刷新接口实现类 class OnRershListener implements OnRersh{ //实现刷新接口的 方法,,在MyList中会回调该方法。 //模拟 @Override public void OnRershListener() { // TODO Auto-generated method stub new Thread(new Run()).start();//启动一个线程 } } //线程接口实现类 class Run implements Runnable{ @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(2000); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } handler.sendEmptyMessage(0); } } } </span>
<span style="font-family:Comic Sans MS;font-size:12px;">package com.bzu.edu.gxs.adapter; import java.util.List; import com.bzu.gxs.*; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class ArticleAdapter extends BaseAdapter { private List<Messages> list_mes=null; private Context context; public ArticleAdapter(Context context,List<Messages> list_mes) { // TODO Auto-generated constructor stub this.list_mes=list_mes; this.context=context; } @Override public int getCount() { // TODO Auto-generated method stub return list_mes.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list_mes.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ListItem listitem; if(convertView==null) { convertView=(LinearLayout)LayoutInflater.from(context).inflate(R.layout.layout_view, null); listitem=new ListItem(); listitem.img=(ImageView)convertView.findViewById(R.id.head_img); listitem.name=(TextView)convertView.findViewById(R.id.name); //显示当前日期 listitem.time=(TextView)convertView.findViewById(R.id.time); listitem.rq=(TextView)convertView.findViewById(R.id.rq); listitem.article=(TextView)convertView.findViewById(R.id.article); convertView.setTag(listitem); }else { listitem=(ListItem)convertView.getTag(); } listitem.img.setImageDrawable(list_mes.get(position).getImg()); listitem.name.setText(list_mes.get(position).getName()); listitem.time.setText(list_mes.get(position).getTime()); listitem.rq.setText(list_mes.get(position).getRq()); listitem.article.setText(list_mes.get(position).getArticel()); return convertView; } public class ListItem{ ImageView img; TextView name; TextView time; TextView rq; TextView article; } } </span>
<span style="font-family:Comic Sans MS;font-size:12px;">package com.bzu.edu.gxs.adapter; import android.graphics.drawable.Drawable; public class Messages { private Drawable img; private String name; private String time; private String rq; private String articel; public Drawable getImg() { return img; } public void setImg(Drawable img) { this.img = img; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getRq() { return rq; } public void setRq(String rq) { this.rq = rq; } public String getArticel() { return articel; } public void setArticel(String articel) { this.articel = articel; } }</span><span style="font-family:Comic Sans MS;"> </span>