Android Application Development-detailed blog posts displayed on the blog client of xiaowu CSDN

Source: Internet
Author: User

Android Application Development-detailed blog posts displayed on the blog client of xiaowu CSDN
Android Application Development-detailed blog posts displayed on the blog client of xiaowu CSDN
In the previous blog, we will introduce how to embed rice advertisements and get profits. This blog will explain how to display the details of a blog post in a ListView, which may be confusing for kids shoes, because a blog may contain titles, summaries, images, code, and other elements, how can we display the content on an interface and display it in a designated way? Don't worry, we will tell you the following.
A new blog post may have the following elements:TitleSummaryText ContentImageRough titleCode block
As described in the UI article, the main control of the blog details is a ListView. Each element is an item in the ListView, and each item has its own layout for displaying specific elements. As follows:




I won't talk about the UI. Let's take a look at 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 author */public class BlogDetailAdapter extends BaseAdapter {private ViewHolder holder; private LayoutInflater layoutInflater; private Context context; private List
 
  
List; private SpannableStringBuilder htmlSpannable; private ImageLoader imageLoader = ImageLoader. getInstance (); private DisplayImageOptions options; public BlogDetailAdapter (Context context) {super (); this. context = context; layoutInflater = LayoutInflater. from (context); list = new ArrayList
  
   
(); // 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 ). displayers (new FadeInBitmapDisplayer (300 )). build ();} public void setList (List
   
    
List) {this. list = list;} public void addList (List
    
     
List) {this. list. addAll (list);} public void clearList () {this. list. clear ();} public List
     
      
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 (posi Tion); if (null = convertView) {holder = new ViewHolder (); switch (item. getState () {case Constants. DEF_BLOG_ITEM_TYPE.TITLE: // display the 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: // summary convertView = layoutInflater. inflate (R. layout. article_detail_summary_ite M, null); holder. content = (TextView) convertView. findViewById (R. id. text); break; case Constants. DEF_BLOG_ITEM_TYPE.CONTENT: // content 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: // image convertView = layoutInflater. inflate (R. layout. article_detail_img_item, null); holder. im Age = (ImageView) convertView. findViewById (R. id. imageView); break; case Constants. DEF_BLOG_ITEM_TYPE.BOLD_TITLE: // bold title 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: // code convertView = layoutInflater. inflate (R. layout. article_detail_code_item, null); holde R. code = (WebView) convertView. findViewById (R. id. code_view); // holder. code. getSettings (). setUseWideViewPort (true); // holder. code. getSettings (). setJavaScriptEnabled (true); // holder. code. getSettings (). setsuppzoom zoom (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: // image, asynchronously loaded imageLoader. displayImage (item. getContent (), holder. image, options); break; case Constants. DEF_BLOG_ITEM_TYPE.CODE: // code, format display // read the code file and template file String code = item. getContent (); // String code = FileUtil. getFileContent (context, // "AboutActivity. java "); String template = FileUtil. getFileContent (context, "code.html"); // generate String html = template. replace ("{code}", code); holder. code. getSettings (). setdefatextextencodingname ("UTF-8"); holder. code. getSettings (). setsuppzoom zoom (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 an optimization policy for ListView, that is, asynchronous loading of images. Here, Wu uses the excellent open-source project universalimageloader. We only need to associate it with the dependent project, you can use it to asynchronously load network images in the project. You can view the code above for 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 details page ** @ author wwj_748 * @ date 2014/8/10 */public class BlogDetailActivity extends Activity implements IXListViewLoadMore {private XListView listView; // list control private BlogDetailAdapter blogDetailAdapter; // content adapter private ProgressBar progressBar; // progress bar private ImageView reLoadImageView; // reload the image private ImageView backBtn; // the return button private ImageView commentBtn; // The comment button public static String url; // The blog address private String filename; // file name @ Overrideprotected void onCreate (Bundle savedInstanceState) {requestWindowFeature (Window. FEATURE_NO_TITLE); // No title super. onCreate (savedInstanceState); setContentView (R. layout. article_detail); init (); initComponent (); // execute asynchronous loading of new maintask(.exe cute (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);} // initialize the 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); progressBar. 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) {// gets the status int state = blogDetailAdapter of the click list. getList (). get (position-1 ). getState (); switch (state) {case Constants. DEF_BLOG_ITEM_TYPE.IMG: // click the image 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
 
  
{@ Overrideprotected Integer doInBackground (String... params) {// obtain the html document String temp = HttpUtil through the http request url. httpGet (params [0]); if (temp = null) {if (params [1]. equals (Constants. DEF_TASK_TYPE.FIRST) {return Constants. DEF_RESULT_CODE.FIRST;} else {return Constants. DEF_RESULT_CODE.ERROR ;}// Add the 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 ();} el Se if (result = Constants. DEF_RESULT_CODE.REFRESH) {blogDetailAdapter. notifyDataSetChanged ();} else {blogDetailAdapter. yydatasetchanged (); listView. stopLoadMore ();} progressBar. setVisibility (View. INVISIBLE); super. onPostExecute (result) ;}}// load more @ Overridepublic void onLoadMore () {if (! JsoupUtil. contentLastPage) {new maintask(cmd.exe cute (url, Constants. DEF_TASK_TYPE.NOR_FIRST);} else {// "-- THE END0 --" text listView is displayed at THE bottom. stopLoadMore ("-- the end --");}}}
 


After studying the source code provided by my students, I can use AsyncTask to perform network request operations. Also, students can use Thread + Handler to Implement Asynchronous requests, note that time-consuming operations and network operations cannot be placed in the main thread, which is the specification of Android development. Here we also provide a technique. when updating the ListView data, we do not need to re-create an adapter. Like me, we can provide the addList method in the adapter class, add the data to the adapter, and then call the yydatasetchanged () method at the appropriate location to update the data without data duplication or inefficiency.

Related Article

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.