Android Sina Weibo client (vi)--home interface of the ListView

Source: Internet
Author: User

Source: Miroslav | http://fangjie.sinaapp.com/?p=184 Reprint please indicate the source

Final Effect Demo: http://fangjie.sinaapp.com/?page_id=54
The project code has been put into Github:https://github.com/jayfang1993/sinaweibo

First, the adapter of the ListView.

Because the list of micro-blog item is not a rule, for example, some microblogging has forwarded sub-blog, some do not, some have pictures, some have no pictures, so it is not fixed. Baseadapter is used here to design a Weiboadapter.java for Weibo item.

Package Com.fangjie.weibo.ui;import Java.util.date;import Java.util.list;import com.fangjie.weibo.r;import Com.fangjie.weibo.bean.weibo;import Android.content.context;import Android.graphics.bitmap;import Android.support.v4.util.lrucache;import Android.text.html;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 Weiboadapter extends Baseadapter {private Context context;private List<weibo > Weibos;public weiboadapter (Context context,list<weibo> weibos) {System.out.println (Weibos.get (1). Conten t); This.context=context;this.weibos=weibos;} public int GetCount () {return weibos.size ();} Public Object getItem (int position) {return null;} public long getitemid (int position) {return 0;}   Public View GetView (int position, View Convertview, ViewGroup parent) {//position represents location//custom item layout through View Association, populated    if (Convertview = = null)     {Convertview = view.inflate (context, r.layout.wb_item, null);        } System.out.println (position);        Final Weibo Weibo =weibos.get (position); Gets the component to be displayed, note that the Findviewbyid call object is the object that is populated with the item's layout above view TextView Tv_name = (TextView) Convertview.findviewbyid (r.id.txt          _wb_item_uname);          TextView tv_content = (TextView) Convertview.findviewbyid (r.id.txt_wb_item_content);          TextView tv_time = (TextView) Convertview.findviewbyid (r.id.txt_wb_item_time);          TextView Tv_from = (TextView) Convertview.findviewbyid (R.id.txt_wb_item_from);          TextView tv_comment = (TextView) Convertview.findviewbyid (r.id.txt_wb_item_comment);          TextView tv_repost = (TextView) Convertview.findviewbyid (r.id.txt_wb_item_redirect);        LinearLayout zlayout= (linearlayout) Convertview.findviewbyid (r.id.lyt_wb_item_sublayout);         TextView tv_zcontent= (TextView) Convertview.findviewbyid (r.id.txt_wb_item_subcontent); Final ImageView iv_userhead= (ImageView) CONvertview.findviewbyid (R.id.img_wb_item_head);        ImageView iv_isv= (ImageView) Convertview.findviewbyid (R.ID.IMG_WB_ITEM_V);        ImageView iv_content_pic= (ImageView) Convertview.findviewbyid (r.id.img_wb_item_content_pic);        ImageView iv_zcontent_pic= (ImageView) Convertview.findviewbyid (r.id.img_wb_item_content_subpic);        Component Add Content Tv_content.settext (Weibo.getcontent ());        Tv_name.settext (Weibo.getuser (). GetName ());        Tv_from.settext ("From:" +html.fromhtml (Weibo.getfrom ()));        Tv_repost.settext (Weibo.getreposts_count () + "");        Tv_comment.settext (Weibo.getcomments_count () + "");        Tv_time.settext (DealTime (Weibo.gettime ()));          LoadBitmap (Weibo.getuser (). Getprofile_image_url (), iv_userhead,80,80);                if (!weibo.getbmiddle_pic (). Equals ("")) {LoadBitmap (Weibo.getbmiddle_pic (), iv_content_pic,0,0);        Iv_content_pic.setvisibility (view.visible); } else {iv_content_pic.setvisibility (View.gone);        } if (Weibo.getuser (). ISISV ()) iv_isv.setvisibility (view.visible);        else iv_isv.setvisibility (View.gone);        if (Weibo.getweibo ()!=null) {zlayout.setvisibility (view.visible);            Tv_zcontent.settext ("@" +weibo.getweibo (). GetUser (). GetName () + ":" +weibo.getweibo (). getcontent ()); if (!weibo.getweibo (). Getbmiddle_pic (). Equals ("")) {LoadBitmap (Weibo.getweibo (). Getbmiddle_pic ()                    , iv_zcontent_pic,0,0);            Iv_zcontent_pic.setvisibility (view.visible);        }} else Zlayout.setvisibility (View.gone);  return convertview; }public void AddItem (Weibo Weibo) {weibos.add (Weibo);}}

The layout file for Weibo item Wb_item.xml

<?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=" wrap_content "android:background=" @drawable/ List_item "> <imageview android:id=" @+id/img_wb_item_head "android:layout_width=" 48DP "android:layout_height= "48DP" android:src= "@drawable/user_head" android:layout_marginleft= "3DP" android:layout_margintop= "5DP"/> <! --Right frame--<linearlayout android:layout_width= "match_parent" android:orientation= "vertical" android:layout_ height= "Wrap_content" android:layout_margin= "5DP" > <!--user name, SINA certified Part---<linearlayout Android:layout_ Width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <!--User name---& Lt TextView android:id= "@+id/txt_wb_item_uname" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:textcolor=" #424d54 "android:textsize=" 15DP "/&Gt <!--SINA Certification--<imageview android:id= "@+id/img_wb_item_v" android:layout_width= "Wrap_content" android:layout _height= "Wrap_content" android:visibility= "Gone" android:src= "@drawable/V"/> <relativelayout android:layout_ Width= "Fill_parent" android:layout_height= "wrap_content" android:gravity= "right" > <!--release time-&LT;TEXTVI  EW android:id= "@+id/txt_wb_item_time" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "1 hours ago" android:textcolor= "#efa608" android:textsize= "12DP"/> </RelativeLayout> </linearla Yout> <!--Weibo body Content--<linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_  Content "android:orientation=" vertical "> <!--Weibo body Content--<textview android:id=" @+id/txt_wb_item_content " Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:text= "  Micro-bo text content micro-bo text content micro-bo text content micro-bo text content microblogging text content micro bo text content "android:textcolor=" #6b717b "Android:textsize= "13DP"/> <imageview android:id= "@+id/img_wb_item_content_pic" android:layout_width= "Wrap_  Content "android:layout_height=" wrap_content "android:src=" @drawable/user_head "android:layout_margintop=" 3DP " android:visibility= "Gone"/> </LinearLayout> <!--forwarded sub-Weibo content--<linearlayout android:layout_width= " Match_parent "android:layout_height=" wrap_content "android:id=" @+id/lyt_wb_item_sublayout "android:orientation=" Vertical "android:layout_margintop=" 3DP "android:visibility=" Gone "android:background=" @drawable/popup "> <!- -Weibo text Content--<textview android:id= "@+id/txt_wb_item_subcontent" android:layout_width= "Fill_parent" Android: layout_height= "Wrap_content" android:text= "micro-bo text content micro-bo text content micro-bo text content micro-bo text content microblogging text content micro-bo text content" Android:textcolor= "# 6b717b "android:textsize=" 13DP "/> <imageview android:id=" @+id/img_wb_item_content_subpic "Android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:src= "@drawable/user_head "android:layout_margintop=" 3DP "android:visibility=" Gone "/> </LinearLayout> <!--Weibo SOURCE section--<linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:o rientation= "Horizontal" android:layout_margintop= "3DP" android:layout_marginbottom= "3DP" > <!--User name--&lt ; TextView android:id= "@+id/txt_wb_item_from" android:layout_width= "wrap_content" android:layout_height= "Wrap_  Content "android:text=" from: Touch Android "android:textcolor=" #9ba0aa "android:textsize=" 12DP "/> <linearlayout Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" Android  : gravity= "Right" > <textview android:id= "@+id/txt_wb_item_redirect" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:drawableleft= "@drawable/redirect_icon" android:text= "Android: Textcolor= "#9ba0aa" android:textsize= "13DP"/> <textview ANdroid:id= "@+id/txt_wb_item_comment" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_marginleft= "5DP" android:drawableleft= "@drawable/comment_icon" android:text= "Android: Textcolor= "#9ba0aa" android:textsize= "13DP"/> </LinearLayout> </LinearLayout> </linearlayout& Gt;</linearlayout>

Weiboadapter's role is to bind list<weibo> Weibos data to each view's control. Note: The load loadbitmap for the picture Imagview control is asynchronous loading, which is described in the next article.

Two. ListView details--load more at the bottom

First you need to write a layout file for this thing Load_more.xml, the layout is very simple, is a button.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout  xmlns:android= "http://schemas.android.com/apk/ Res/android "  android:orientation=" vertical "  android:layout_width=" fill_parent "  android:layout_ height= "Wrap_content" >  <button   android:id= "@+id/loadmorebutton" android:layout_width= "Fill_   Parent "   android:layout_height=" wrap_content "   android:text=" load More "   android:onclick=" Loadmore "/> </LinearLayout>


Then add a bottom view to the ListView in Homeactivity.java, Listview.addfooterview (view view);

Set list bottom View-load more view Loadmoreview = Getlayoutinflater (). Inflate (R.layout.load_more, null);  loadmorebutton= (Button) Loadmoreview.findviewbyid (R.id.loadmorebutton);          Weibolist.addfooterview (Loadmoreview);


Three. Refresh button for ListView

ProgressBar will appear when you click the Refresh button on the main screen, which is not very difficult. First write a progress layout, before the start of the refresh task ProgressBar display, after the end of the task View.gone OK, detailed see source code homeactivity.

Four. Note: When I write the ListView, the simulator test is completely OK, but when debugging on the real machine, the ListView will have a shadow on the junction with the TitleBar and tabhost above. Add that to the sentence.

Listview.setfadingedgelength (0);

Probably not very intuitive, and finally attached Homeactivity.java all the code, this is the home interface code

Package Com.fangjie.weibo.ui;import Java.util.hashmap;import Java.util.list;import java.util.map;import Com.fangjie.weibo.r;import Com.fangjie.weibo.bean.task;import Com.fangjie.weibo.bean.weibo;import Com.fangjie.weibo.logic.mainservice;import Com.fangjie.weibo.util.sharepreferencesutil;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.linearlayout;import Android.widget.listview;import Android.widget.textview;import Android.widget.toast;public class Homeactivity extends Activity implements iweiboacitivity {private ListView weibolist;private list<weibo> weibos;private weiboadapter adapter;private TextView tv_title;private button btn_refresh;private button btn_update;private linearlayout progress;private button loadmorebutton;protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.home); init ();} public void inIt () {weibolist= (ListView) Findviewbyid (R.id.lv_weibos); tv_title= (TextView) Findviewbyid (r.id.txt_wb_title); btn_ Refresh= (Button) Findviewbyid (R.id.btn_refresh), btn_update= (Button) Findviewbyid (r.id.btn_writer);p rogress= ( LinearLayout) Findviewbyid (r.id.layout_progress);//Set list bottom View-load more view Loadmoreview = Getlayoutinflater (). Inflate (  R.layout.load_more, NULL);          loadmorebutton= (Button) Loadmoreview.findviewbyid (R.id.loadmorebutton);           Weibolist.addfooterview (Loadmoreview); Weibolist.setfadingedgelength (0); final String Token=sharepreferencesutil.getloginuser (homeactivity.this). GetToken (); Tv_title.settext (Sharepreferencesutil.getloginuser (homeactivity.this). GetUserName ()); Map<string,object> params=new hashmap<string,object> ();p arams.put ("token", token); Task Task=new Task (Task.get_weibos, params);p rogress.setvisibility (view.visible); Mainservice.newtask (Task); Mainservice.addactivty (Homeactivity.this); Loadmorebutton.setonclicklistener (new Onclicklistener () {public void ONclick (View v) {map<string,object> params=new hashmap<string,object> ();p arams.put ("token", token); Params.put ("max_id", Weibos.get (Weibos.size ()-1). Getwid ()); Loadmorebutton.settext ("Loading, please wait ..."); Task Task=new Task (Task.loadmore, params); Mainservice.newtask (Task); Mainservice.addactivty (Homeactivity.this);}}); Btn_refresh.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {map<string,object> params= New Hashmap<string,object> ();p arams.put ("token", token);p rogress.setvisibility (view.visible); Task Task=new Task (Task.get_weibos, params); Mainservice.newtask (Task); Mainservice.addactivty (Homeactivity.this);}}); Btn_update.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {toast.maketext (Homeactivity.this, " Pro, the program ape has not written well ... ", Toast.length_long). Show ();});} @SuppressWarnings ("unchecked") public void Refresh (int TaskID, Object ... objects) {switch (TaskID) {case Task.get_weibos : weibos= (list<weibo>) objects[0];adapter=new Weiboadapter (HomeActivity.this,weibos); Weibolist.setadapter (adapter); Break;case task.loadmore:weibos= (list<weibo>) objects[ 0];for (int i=1;i<weibos.size (); i++) Adapter.additem (Weibos.get (i)); adapter.notifydatasetchanged (); After the data set changes, notify adapter Loadmorebutton.settext ("load More");} Progress.setvisibility (View.gone); Mainservice.removeactivty (Homeactivity.this);}}


Maybe everyone in the homeactivity only see new tasks, but these tasks do not know what to do, you can look at the previous blog post, because there is a logical processing of the Mainservice processing class. For Task.get_weibos and Task.loadmore, the code is added again. There is also a part of the code for these two tasks attached to Mainservice.java.

Refresh the Weibo case task.get_weibos:{string token= (String) Task.getparams (). GET ("token"); Weiboutil weiboutil=new weiboutil (); List<weibo> weibos=weiboutil.getweibolist (token,0); msg.obj=weibos;break;} Loads more case task.loadmore:{string token= (String) task.getparams (). Get ("token"), long max_id= (long) task.getparams (). Get ("max_id"); Weiboutil weiboutil=new weiboutil (); List<weibo> weibos=weiboutil.getweibolist (token,max_id); msg.obj=weibos;break;}


  welcome attention to my personal site: http://fangjie.sinaapp.com/&NBSP;

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.