Android學習之模仿新浪微博隨便看看欄目

來源:互聯網
上載者:User

標籤:

小編目前仍在Android學習中,本次模仿新浪微博隨便看看欄目運行效果如下:

過程分析:

完成該項目大概可以分為三部分:  布局設計,自訂配接器 和 為適配器添加資料;

1.布局設計中,小編在mainactivity.xml檔案中定義了一個ListView控制項,並單獨設定了一個資料項目的布局list_cell,

資料項目代碼如下:

<?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:orientation="horizontal"     android:background="#EEEEEE">    <ImageView        android:id="@+id/ivIcon"        android:layout_width="40dp"        android:layout_height="40dp"        android:layout_margin="3dp"        android:contentDescription="@string/iv_des"/>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:layout_marginLeft="10dp">        <TextView            android:id="@+id/tvName"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentLeft="true"            android:layout_alignParentTop="true"            android:layout_marginTop="7dp"            android:text="@string/tv_name"            android:textAppearance="?android:attr/textAppearanceLarge" />               <TextView            android:id="@+id/tvTime"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_above="@+id/tvContent"            android:layout_alignParentRight="true"            android:layout_marginRight="20dp"            android:text="@string/tv_time"            android:textAppearance="?android:attr/textAppearanceSmall" />                 <TextView            android:id="@+id/tvContent"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_alignParentLeft="true"            android:layout_marginTop="10dp"            android:layout_below="@id/tvName"                        android:text="" />    </RelativeLayout></LinearLayout>

 

mainactivity.xml主要頁碼如下:

<TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/tv_title" />    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:layout_marginTop="6dp">    </ListView>

2.自訂的適配器:在自訂配接器時,小編是通過繼承BaseAdapter來實現的,主要代碼如下:

private List<ListCell> list;    private Context context;        public MyAdapter(Context context, List<ListCell> list) {        this.context = context;        this.list = list;    }
public View getView(int arg0, View arg1, ViewGroup arg2) {                if (arg1 == null) {            arg1 = LayoutInflater.from(context).inflate(R.layout.list_cell,null);        }                ListCell lc = list.get(arg0);                ImageView iconId = (ImageView) arg1.findViewById(R.id.ivIcon);        TextView tvName = (TextView) arg1.findViewById(R.id.tvName);        TextView tvTime = (TextView) arg1.findViewById(R.id.tvTime);        TextView tvContent = (TextView) arg1.findViewById(R.id.tvContent);                iconId.setBackgroundResource(lc.getIconId());        tvName.setText(lc.getTvName());        tvTime.setText(lc.getTvTime());        tvContent.setText(lc.getTvContent());                return arg1;    }

在此處構造方法中傳遞的2個參數,Context用來傳遞上下文,List則用來接收資料來源,其中list<ListCell>中的實體類對應的是list_cell的實體類,此處不再詳述

3.在MainActivity中進行最後的配置與測試:

package cn.edu.bzu.sinalook;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;public class MainActivity extends Activity {    private MyAdapter adapter;    private ListView lv;    private List<ListCell> list;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                lv = (ListView) findViewById(R.id.lv);        //        adapter=new MyAdapter(MainActivity.this,list);/*此處必須放置在list資料之下,否則會報null 指標異常,原因是此時的list為空白*/        list = new ArrayList<ListCell>();        //測試資料        list.add(new ListCell(R.drawable.stage_0, "小紅", "1分鐘前", "我喜歡石頭的懂事 kimi的害羞可愛 森蝶的細心照顧他人 天天的獨當一面 詩齡的天馬行空 童年真好!"));        list.add(new ListCell(R.drawable.stage_1, "小橙", "19分鐘前", "一百斤的石頭我可能提不起來,但是 如果是一百斤人名幣,我保證拎起來就跑。"));        list.add(new ListCell(R.drawable.stage_2, "小黃", "21分鐘前", "在別人面前評論我的是是非非好與不好跟你有何關係你為何纏我不放莫非你缺狗糧"));        list.add(new ListCell(R.drawable.stage_3, "小綠", "33分鐘前", "有多少人不想過11.11這個節日,又有多少人是在11.11找到了自己的神!"));        list.add(new ListCell(R.drawable.stage_4, "小青", "43分鐘前", "記得小時候,我爺爺寫字非常漂亮,寫了好多本毛筆字。學校裡的老師沒有一個比得上。現在連筆都拿不穩了"));        list.add(new ListCell(R.drawable.stage_5, "小藍", "58分鐘前", "[別瀟洒了自己苦了父母 ]"));        list.add(new ListCell(R.drawable.stage_6, "小紫", "1小時前", "    漸漸知道了,很多東西可遇而不可求,不屬於自己的,何必拼了命去在乎。"));        list.add(new ListCell(R.drawable.stage_7, "小黑", "1小時前", "    曾幾何時對你說過要陪你到海枯石爛,可是到了最後你我還是成為了最熟悉的陌生人! ----MC小凡"));        list.add(new ListCell(R.drawable.stage_4, "小白", "2小時前", "    其實最喜歡你的內個人是一直陪伴你的人,不是開心的時候就找你,不開心的時候就把你拋棄在一邊!---MC小凡"));                adapter=new MyAdapter(MainActivity.this,list);                lv.setAdapter(adapter);    }}

到了這裡,一個模仿新浪微博隨便看看的項目就完成了~~~

Android學習之模仿新浪微博隨便看看欄目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.