如果寫一個微博的一個ListView,微博ListView

來源:互聯網
上載者:User

如果寫一個微博的一個ListView,微博ListView

先上:




這是個ListView:

layout是:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="wrap_content"android:tag="itemview" android:orientation="horizontal"android:background="@drawable/list_item_bg"><ImageView android:id="@+id/ivItemPortrait"android:layout_marginLeft="5dip" android:layout_marginTop="5dp"android:layout_width="50dp" android:layout_height="50dp"android:saveEnabled="true" android:src="@drawable/portrait" android:adjustViewBounds="true"/> //頭像的圖片<LinearLayout android:id="@+id/lyRightLayout"android:layout_height="wrap_content" android:layout_width="fill_parent"android:orientation="vertical" android:layout_margin="5dip"><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginRight="3dip"android:orientation="horizontal"><TextView android:text="" android:id="@+id/tvItemName"android:textColor="#000" android:textSize="15sp" android:paddingLeft="5dip" android:textStyle="bold"android:layout_width="wrap_content" android:layout_height="wrap_content" />  //這個是名字<RelativeLayout   //這個是名字右側的Layout,包括時間和定位資訊android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginRight="3dip"android:gravity="right" android:layout_weight="1"><TextView android:text="" android:id="@+id/tvItemDate"android:textColor="#666666" android:layout_marginLeft="3dip"android:textSize="12sp" android:layout_gravity="right"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_alignParentRight="true" /><ImageView android:id="@+id/ivItemPic"android:layout_marginTop="3dp" android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_toLeftOf="@id/tvItemDate"android:layout_alignTop="@id/tvItemDate" android:src="@drawable/pic"android:saveEnabled="false" android:visibility="gone" /><ImageView android:id="@+id/ivItemGps"android:layout_marginTop="3dp" android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_toLeftOf="@id/ivItemPic"android:layout_alignTop="@id/tvItemDate" android:src="@drawable/location_icon"android:saveEnabled="false" android:visibility="gone" /></RelativeLayout></LinearLayout><TextView android:text="" android:id="@+id/tvItemContent"   //這個是內容的資訊android:textColor="#414951"android:layout_marginTop="8dip" android:layout_width="fill_parent"android:layout_height="wrap_content" android:paddingLeft="5dip" android:paddingRight="5dip"android:lineSpacingExtra="4px"/><RelativeLayout android:layout_width="fill_parent"android:layout_height="wrap_content" android:gravity="center_vertical"><ImageView android:id="@+id/tweet_upload_pic1"android:scaleType="centerInside" android:layout_width="@integer/preview_pic_size"android:layout_height="@integer/preview_pic_size"android:layout_marginLeft="5dp" android:layout_marginRight="5dp"android:visibility="gone" android:src="@drawable/preview_pic_loading"android:layout_marginTop="5dp" android:layout_marginBottom="5dp" />   //這個發送方的圖</RelativeLayout><LinearLayout android:id="@+id/subLayout"android:layout_width="fill_parent" android:layout_height="wrap_content"android:orientation="vertical" android:layout_marginBottom="4dp"android:background="@drawable/popup" android:visibility="gone"><TextView android:text="" android:id="@+id/tvItemSubContent"android:textColor="#666666"android:layout_marginTop="3dip" android:layout_marginBottom="3dip"android:layout_width="fill_parent" android:layout_height="wrap_content" android:lineSpacingExtra="4px"/><RelativeLayout android:layout_width="fill_parent"android:layout_height="wrap_content" android:gravity="center_vertical" android:paddingBottom="10dp"><ImageView android:id="@+id/tweet_upload_pic2"android:scaleType="centerInside" android:layout_width="@integer/preview_pic_size"android:layout_height="@integer/preview_pic_size"android:layout_marginLeft="5dp" android:layout_marginRight="5dp"android:visibility="gone" android:src="@drawable/preview_pic_loading"android:layout_marginTop="5dp" android:layout_marginBottom="5dp" />  <pre name="code" class="html"> //這個是轉寄的圖
</RelativeLayout></LinearLayout><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content" android:orientation="horizontal"><TextView android:id="@+id/tweet_form" android:textSize="12.0sp" android:textColor="#ff7d7d7d"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_weight="1" android:paddingLeft="5dip"/>//這個是最下方的小表徵圖<LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content" android:orientation="horizontal"android:gravity="right|center_vertical" ><ImageView android:id="@+id/tweet_redirect_pic" android:layout_width="wrap_content"android:layout_height="wrap_content" android:paddingLeft="10.0dip"android:visibility="gone" android:src="@drawable/redirect_icon" /><TextView android:id="@+id/tweet_redirect"android:textSize="12.0sp" android:textColor="#a4b7d6" android:layout_width="wrap_content"android:layout_height="wrap_content" android:visibility="gone" android:paddingLeft="2dip"/><ImageView android:id="@+id/tweet_comment_pic" android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="10.0dip"android:visibility="gone" android:src="@drawable/comment_icon" /><TextView android:id="@+id/tweet_comment" android:textSize="12.0sp"android:textColor="#a4b7d6" android:layout_width="wrap_content"android:visibility="gone" android:paddingLeft="2dip"android:layout_height="wrap_content" android:paddingRight="5dp"/></LinearLayout></LinearLayout></LinearLayout></LinearLayout>

邏輯是:

1.檢查本地是否有緩衝,如果有直接轉化為List,作為ListView的資料

2.如果沒有資料,就從伺服器把資料download下來存在本地,並進行解析

3.寫adapter去載入這些資料

下面來說說如何載入列表中的小圖片

在記憶體裡new一個:

public static Map<String, Bitmap> mPortraitCacheMap = new HashMap<String, Bitmap>();

if (mBlog.portrait != null) {new LoadPortraitTask().execute(mBlog.portrait, mCacheDir,this.parent, mBlog);}Bitmap bm = Utils.mPortraitCacheMap.get(mBlog.portrait);if (bm != null && !bm.isRecycled()) {mPortrait.setVisibility(View.INVISIBLE);mPortrait.setImageBitmap(bm);mPortrait.setVisibility(View.VISIBLE);return;}

private class LoadPortraitTask extends AsyncTask<Object, Void, Object[]> {protected void onPreExecute() {}protected Object[] doInBackground(Object... args) {String portraitUrl = (String) args[0];//String cacheDir = TextUtils.isEmpty(mSdFileDir)? mCacheDir : mSdFileDir;ListView listView = (ListView) args[2];MBlog mBlog = (MBlog) args[3];Object[] objs = new Object[2];objs[0] = mBlog.portrait;Bitmap round = null;//將圖片下載下來儲存在sd卡上objs[1] = Utils.getPortraitBitmap(portraitUrl, mCacheDir, listView.getContext(),mBlog, true, Constants.PORTRAIT_DIR_SUFFIX);return objs;}protected void onProgressUpdate(Void... unused) {}protected void onPostExecute(Object[] ret) {String url = (String) ret[0];Bitmap b = (Bitmap) ret[1];if (!mBlog.portrait.equals(url))return;if (b != null && !b.isRecycled()) {mPortrait.setVisibility(View.INVISIBLE);mPortrait.setImageBitmap(b);mPortrait.setVisibility(View.VISIBLE);} else {mPortrait.setVisibility(View.INVISIBLE);mPortrait.setImageBitmap(getDefaultPortrait());mPortrait.setVisibility(View.VISIBLE);}}}

public static Bitmap getPortraitBitmap(String portraitUrl,String cacheDir, Context ctx, MBlog mBlog, boolean bRound, String suffix) {synchronized (lock) {final String sdDir = Utils.getSDPath();final String saveDir = TextUtils.isEmpty(sdDir)? cacheDir : sdDir + suffix;String file = null;Context context = ctx;//如果圖片已經存在本地,那就直接載入就好了if (isInCacheMap(portraitUrl, suffix)) {Bitmap bm = mPortraitCacheMap.get(portraitUrl);if (bm == null || bm.isRecycled()) {mPortraitCacheMap.remove(portraitUrl);file = Utils.checkCache(saveDir, portraitUrl);return loadFromFile(context, file, mBlog, portraitUrl,bRound, suffix);}} else{file = Utils.checkCache(saveDir, portraitUrl);if (file == null) {Bitmap bm = loadFromNet(context, portraitUrl, saveDir, mBlog, bRound, suffix);if (bm == null || bm.isRecycled())return loadFromNet(context, portraitUrl, saveDir, mBlog, bRound, suffix);return bm;}else {return loadFromFile(context, file, mBlog, portraitUrl,bRound, suffix);}}}return null;}

我們可以這樣來下載圖片:

public byte[] getPictureByte(String url) throws HttpException {InputStream inputStream;HttpGet request = new HttpGet(url);HttpClient client = getHttpClient();request.setHeader("User-Agent", Constants.USER_AGENT);try {HttpResponse response = client.execute(request);StatusLine status = response.getStatusLine();if (status.getStatusCode() != Constants.HTTP_STATUS_OK) {throw new HttpException(String.format("Invalid response from server: %s", status.toString()));}// Pull content stream from responseHttpEntity entity = response.getEntity();inputStream = entity.getContent();} catch (IOException e) {if (client != null) {client.getConnectionManager().shutdown();}throw new HttpException("Problem communicating with API", e);}try {ByteArrayOutputStream content = new ByteArrayOutputStream();byte[] imageByte;int readBytes = 0;while ((readBytes = inputStream.read(sBuffer)) != -1) {content.write(sBuffer, 0, readBytes);content.flush();}imageByte = content.toByteArray();content.close();if (client != null) {client.getConnectionManager().shutdown();}return imageByte;} catch (IOException e) {if (client != null) {client.getConnectionManager().shutdown();}throw new HttpException(UNKNOWN_ERROR, e);}}


代碼下載:http://download.csdn.net/detail/baidu_nod/7705375


android怎寫一個如下樣式activity的布局檔案 螢幕上方為一個listview,底部

把ListView放到一個布局當中 把那兩個Button放到 一個布局當中 設定包含listview的布局的一個屬性android:layout_weight="1" 這樣就OK了、
 
android,listView的一行我想放兩個textView,第一個佔盡量多的位置,怎寫?

1.使用tablelayout,定義成兩列,即每行顯示兩個textview,每列的寬度可以自訂.
2.使用LinearLayout,即LinearLayout套LinearLayout ,外層採用垂直布局,每一行也需要一個LinearLayout ,定義成水平布局.
3.使用gridview也能搞定.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.