Android Learning imitation Sina Weibo casually look at the column

Source: Internet
Author: User

Small part is still in the Android learning, this imitation Sina Weibo casually look at the column running effect as follows:

Process Analysis:

The completion of the project can be divided into three parts: layout design, custom adapters and add data for the adapter;

1. In the layout design, the small series defines a ListView control in the Mainactivity.xml file, and sets the layout List_cell of the data item separately.

The data entry code is as follows:

<?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 Main Page numbers are as follows:

<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. Custom adapters: When customizing the adapter, the small part is implemented by inheriting Baseadapter, the main code is as follows:

Private list<listcell> list;     Private context context;          Public Myadapter (context context, list<listcell> List) {        this. Context = context;          this. List = list;    }
 PublicView GetView (intarg0, 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 ()); returnarg1; }

The 2 parameters passed in the method are constructed here, the context is used to pass the contexts, and the list is used to receive the data source, where the entity classes in list<listcell> correspond to the entity classes of List_cell, which are no longer detailed

3. Perform the final configuration and testing in Mainactivity:

 PackageCn.edu.bzu.sinalook;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.ListView; Public classMainactivityextendsActivity {PrivateMyadapter Adapter; PrivateListView LV; PrivateList<listcell>list; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); LV=(ListView) Findviewbyid (r.id.lv); //adapter=new Myadapter (mainactivity.this,list);/* This must be placed below the list data, otherwise a null pointer exception is reported because the list at this time is empty * /List=NewArraylist<listcell>(); //test DataList.add (NewListcell (R.drawable.stage_0, "Little Red", "1 minutes Ago", "I like the stone of the sensible Kimi of the shy cute sen butterflies carefully take care of other people every day independently poetic age is really good!" ")); List.add (NewListcell (R.drawable.stage_1, "Little Orange", "19 minutes Ago", "100 pounds of stone I may not be able to lift, but if it is 100 catty name, I guarantee to carry up and run." ")); List.add (NewListcell (r.drawable.stage_2, "Xiao Huang", "21 minutes Ago", "in front of others to comment on my wrong good and bad with you what's the matter with you why do you bother me not to let you lack of dog food")); List.add (NewListcell (R.drawable.stage_3, "small green", "33 minutes Ago", "How many people do not want to 11.11 this holiday, and how many people in 11.11 have found their own God!" ")); List.add (NewListcell (R.drawable.stage_4, "xiaoqing", "43 minutes Ago", "Remember when I was a child, my grandfather writes very beautiful, wrote a lot of calligraphy." None of the teachers in the school are comparable. I can't even get a pen right now.)); List.add (NewListcell (r.drawable.stage_5, "Little Blue", "58 minutes Ago", "[Don't be so dashing as to your own bitter parents]")); List.add (NewListcell (R.drawable.stage_6, "small Purple", "1 hours ago", "gradually know, a lot of things can meet and not to ask, do not belong to their own, why spell the life to care." ")); List.add (NewListcell (r.drawable.stage_7, "Little Black", "1 hours ago", "once said to you to accompany you to the end of the day, but finally you and I have become the most familiar strangers!" ----MC Small ")); List.add (NewListcell (R.drawable.stage_4, "small white", "2 hours Ago", "in fact, the most like your personal is always with you, not happy when you are looking for, unhappy when you abandoned on the side!" ---MC small ")); Adapter=NewMyadapter (mainactivity. This, list);    Lv.setadapter (adapter); }}

By the way, a casual look at Sina Weibo is done.

Android Learning imitation Sina Weibo casually look at the column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.