Android App Development-wizard CSDN Blog client's display blog details
In the last blog post to show you how to embed a meter ad and gain revenue, this blog is going to talk about how to display the blog in a ListView detailed information, this may be the children's shoes are more confused, because a blog may have a title, abstract, pictures, code and other elements, How do we display the content in one interface and display it the way we want it to, don't worry, we'll let you know.
re-organize a blog post may have the following elements:
- Title
- Summary
- Text content
- Image
- Coarse caption
- code block
in the UI chapter of the wizard has been introduced, the main control of the details of the blog is a listview, each element is a ListView item, each item has its own layout for the display of specific elements. as follows:
As for the UI, let's focus on the content adapter:
/blogclient/src/com/xiaowu/blogclient/adapter/blogdetailadapter.java
Package Com.xiaowu.blogclient.adapter;import Java.util.arraylist;import Java.util.list;import Android.content.context;import Android.graphics.bitmap;import Android.text.html;import Android.text.spannablestringbuilder;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup;import Android.webkit.webview;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.textview;import Com.nostra13.universalimageloader.core.displayimageoptions;import Com.nostra13.universalimageloader.core.imageloader;import Com.nostra13.universalimageloader.core.imageloaderconfiguration;import Com.nostra13.universalimageloader.core.assist.imagescaletype;import Com.nostra13.universalimageloader.core.display.fadeinbitmapdisplayer;import Com.xiaowu.blogclient.R;import Com.xiaowu.blogclient.model.blog;import Com.xiaowu.blogclient.util.constants;import Com.xiaowu.blogclient.util.fileutil;import com.xiaowu.blogclient.util.mytaghandler;/** * Blog Content Adapter * * @auThor wwj_748 * @date 2014/8/10 */public class Blogdetailadapter extends Baseadapter {private Viewholder holder;private Lay Outinflater layoutinflater;private Context context;private list<blog> list;private SpannableStringBuilder Htmlspannable;private Imageloader Imageloader = imageloader.getinstance ();p rivate displayimageoptions options;public Blogdetailadapter (Context context) {super (); this.context = Context;layoutinflater = Layoutinflater.from (context); List = new arraylist<blog> ();//Initialize Imageloaderimageloader.init (Imageloaderconfiguration.createdefault ( context);//imageloader configuration options = new Displayimageoptions.builder (). Showstubimage (R.drawable.images). Showimageforemptyuri (r.drawable.images). Showimageonfail (R.drawable.images). Cacheinmemory (). CacheOnDisc (). Imagescaletype (imagescaletype.exactly). Bitmapconfig (Bitmap.Config.RGB_565). Displayer (New Fadeinbitmapdisplayer ( ). Build ();} public void setlist (list<blog> list) {this.list = list;} public void Addlist (List<blog> list) {this.list.addAll (list);} public void Clearlist () {this.list.clear ();} Public list<blog> getList () {return List;} public void RemoveItem (int position) {if (list.size () > 0) {list.remove (position);}} @Overridepublic int GetCount () {return list.size ();} @Overridepublic Object getItem (int position) {return list.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Blog item = list.get (position); if (null = = Convertview) {holder = new Viewholder (); switch (Item.getstate ()) {case Constants.def_blog_item_type. title://Display Title Convertview = layoutinflater.inflate (R.layout.article_detail_title_item, null); holder.content = ( TextView) Convertview.findviewbyid (r.id.text); Break;case Constants.def_blog_item_type. SUMMARY://Digest Convertview = layoutinflater.inflate (R.layout.article_detail_summary_item, null); holder.content = ( TextView) Convertview.findviewbyid (r.id.text); Break;case ConstantS.def_blog_item_type. Content://Contents Convertview = layoutinflater.inflate (R.layout.article_detail_item, null); holder.content = (TextView) Convertview.findviewbyid (R.id.text); Break;case Constants.def_blog_item_type. IMG://Picture Convertview = layoutinflater.inflate (R.layout.article_detail_img_item, null); holder.image = (ImageView) Convertview.findviewbyid (R.id.imageview); Break;case Constants.def_blog_item_type. Bold_title://Bold Caption Convertview = Layoutinflater.inflate (R.layout.article_detail_bold_title_item, NULL); Holder.content = (TextView) Convertview.findviewbyid (r.id.text); Break;case Constants.def_blog_item_type. Code://Codes Convertview = Layoutinflater.inflate (R.layout.article_detail_code_item, null); Holder.code = (WebView) Convertview.findviewbyid (R.id.code_view);//Holder.code.getSettings (). Setusewideviewport (true);// Holder.code.getSettings (). Setjavascriptenabled (true);//Holder.code.getSettings (). Setsupportzoom (true);// Holder.code.getSettings (). Setbuiltinzoomcontrols (false);//Holder.codE.getsettings (). Setloadwithoverviewmode (true); break;} Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} System.out.println (Item.getcontent ()); if (null! = Item) {switch (Item.getstate ()) {case Constants.def_blog_item_ TYPE. IMG://Picture, asynchronous Load Imageloader.displayimage (Item.getcontent (), holder.image,options); Break;case Constants.def_blog_ Item_type. Code://codes, format display//Read code files and template files String code = item.getcontent ();//String code = fileutil.getfilecontent (context,//"Abouta Ctivity.java "); String template = fileutil.getfilecontent (context, "code.html");//generate result string html = Template.replace ("{{code}}", code) ; Holder.code.getSettings (). Setdefaulttextencodingname ("Utf-8"); Holder.code.getSettings (). Setsupportzoom (True); Holder.code.getSettings (). Setbuiltinzoomcontrols (true);//Holder.code.loadUrl ("file:///android_asset/code.html" ); Holder.code.loadDataWithBaseURL ("file:///android_asset/", HTML, "text/html", "Utf-8", null); Break;default: Holder.content.setText (html.fromhtml (item.geTcontent (), Null,new Mytaghandler ())); break;}} return Convertview;} @Overridepublic int Getviewtypecount () {return 6;} @Overridepublic int Getitemviewtype (int position) {switch (List.get (position). GetState ()) {case Constants.def_blog_ Item_type. Title:return 0;case Constants.def_blog_item_type. Summary:return 1;case Constants.def_blog_item_type. Content:return 2;case Constants.def_blog_item_type. Img:return 3;case Constants.def_blog_item_type. Bold_title:return 4;case Constants.def_blog_item_type. Code:return 5;} return 1;} @Overridepublic boolean isenabled (int position) {switch (List.get (position). GetState ()) {case Constants.def_blog_item _type. Img:return True;default:return false;}} Private class Viewholder {TextView id; TextView date; TextView title; TextView Content;imageview image; WebView Code;}}
here is a ListView optimization strategy, that is, the image is loaded asynchronously, the wizard here to use a good open source project Universalimageloader, we only need to correlate dependent projects, you can use it in the project to the network image asynchronous loading, Use your own to view the above code implementation.
/blogclient/src/com/xiaowu/blogclient/blogdetailactivity.java
Package Com.xiaowu.blogclient;import Me.maxwin.view.ixlistviewloadmore;import Me.maxwin.view.xlistview;import Android.app.activity;import Android.content.intent;import Android.os.asynctask;import Android.os.Bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.adapterview.onitemclicklistener;import Android.widget.adapterview;import Android.widget.ImageView ; Import Android.widget.progressbar;import Android.widget.toast;import Com.xiaowu.blogclient.adapter.blogdetailadapter;import Com.xiaowu.blogclient.util.constants;import Com.xiaowu.blogclient.util.jsouputil;import com.xiaowu.blogclient.util.httputil;/** * Blog Detail interface * * @author wwj_748 * @ Date 2014/8/10 */public class Blogdetailactivity extends Activity implements Ixlistviewloadmore {private Xlistview LISTVI ew List control private Blogdetailadapter blogdetailadapter; Content adapter private ProgressBar ProgressBar; Progress bar Private ImageView Reloadimageview; Reload the picture private ImageView BACKBTN; Fallback button private ImageView commentbtn; Comment button public static String URL; Blog address private String filename; File name @overrideprotected void OnCreate (Bundle savedinstancestate) {requestwindowfeature (window.feature_no_title); /Untitled Super.oncreate (Savedinstancestate); Setcontentview (R.layout.article_detail); init (); initcomponent ();// Executes an asynchronous load of new Maintask (). Execute (URL, constants.def_task_type. first);} Initialize private void init () {blogdetailadapter = new Blogdetailadapter (this); URL = getintent (). Getextras (). GetString (" Bloglink "); filename = url.substring (Url.lastindexof ("/") + 1); SYSTEM.OUT.PRINTLN ("FileName--->" + filename);} Initializes the component private void InitComponent () {ProgressBar = (ProgressBar) Findviewbyid (r.id.blogcontentpro); Reloadimageview = ( ImageView) Findviewbyid (r.id.reloadimage); Reloadimageview.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (view view) {reloadimageview.setvisibility (view.invisible);p rogressbar.setvisibility ( view.visible);}); BACKBTN = (ImageView) FIndviewbyid (R.ID.BACKBTN); Backbtn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {Finish ();}}); COMMENTBTN = (ImageView) Findviewbyid (r.id.comment); Commentbtn.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (view view) {Intent i = new Intent (); I.setclass (Blogdetailactivity.this, Blogcommentactivity.class); I.putextra ("filename", filename), startactivity (i); Overridependingtransition ( R.anim.push_left_in, r.anim.push_no);}); ListView = (Xlistview) Findviewbyid (R.id.listview); Listview.setadapter (Blogdetailadapter); Listview.setpullloadenable (this); Listview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, View view,int position, long ID) {//Get the status of click list item int state = Blogdetailadapter.get List (). Get (Position-1). GetState (); switch (state) {case Constants.def_blog_item_type. IMG://Click on the picture string url = Blogdetailadapter.getlist (). Get (Position-1). Getimglink (); Intent i = new Intent ();I.setclass (Blogdetailactivity.this, Imageactivity.class); I.putextra ("url", url); startactivity (i); Break;default: Break;}});} @Overridepublic void Finish () {super.finish ();} Private class Maintask extends Asynctask<string, Void, integer> {@Overrideprotected Integer doinbackground (String . params) {//HTTP request URL address, get HTML document String temp = Httputil.httpget (Params[0]), if (temp = = null) {if (Params[1].equals (C Onstants. Def_task_type. First) {return constants.def_result_code. First;} else {return Constants.def_result_code. ERROR;}} Add parsed Data blogdetailadapter.addlist (jsouputil.getcontent (URL, temp)); if (Params[1].equals (constants.def_task_ TYPE. First) {return constants.def_result_code. REFRESH;} Return Constants.def_result_code. LOAD;} @Overrideprotected void OnPostExecute (Integer result) {if (result = = Constants.def_result_code. First) {Toast.maketext (Getapplicationcontext (), "Poor network Signal", Toast.length_long). Show (); reloadimageview.setvisibility (view.visible);} else if (result = = Constants.def_result_CODE. ERROR) {Listview.stoploadmore ();} else if (result = = Constants.def_result_code. REFRESH) {blogdetailadapter.notifydatasetchanged ();} else {blogdetailadapter.notifydatasetchanged (); Listview.stoploadmore ();} Progressbar.setvisibility (view.invisible); Super.onpostexecute (result);}} Load more @overridepublic void Onloadmore () {if (! Jsouputil.contentlastpage) {new Maintask (). Execute (URL, constants.def_task_type. Nor_first);} else {//show "--the end0--" text at the bottom Listview.stoploadmore ("--The END--");}}}
If I study the source of the children's shoes will find that I use Asynctask for network request operation, children's shoes can also use the Thread+handler way to implement asynchronous requests, it is important to note that time-consuming operations and network operations can not be placed in the main thread, This is the specification for Android development. Here is also a technique, when we update the data of the ListView, we do not need to re-new a adapter, can like me, in the adapter class to provide Addlist method, add data to adapter, The Notifydatasetchanged () method can then be called at the appropriate location to update the data without duplication of data and inefficiency.
Android App Development-wizard CSDN Blog client's display blog details