如果写一个微博的一个ListView
来源:互联网 发布:南极旅游 知乎 编辑:程序博客网 时间:2024/06/03 18:27
先上效果图:
这是个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
0 0
- 如果写一个微博的一个ListView
- 写一个普通的ListView
- 写一个自己的listview
- 如果写一个android支持的html文件
- 使用kotlin写一个ListView
- 写一个上下拉动到底时有弹性的listview
- 一个奇怪的listview
- 一个ListView的使用
- android如果用ListView做一个表格形式
- android如果用ListView做一个表格形式
- 如果写一个点击view带动画的下滑展开显示隐藏内容的控件
- 写一个sql语句,如果查询的某个值结果为空时给出提示
- 如果Google面试让你用python写一个树的遍历程序
- 打log的时候如果少写一个%d,cgi会core掉
- 如果再写一个ipcamera项目,我哪些地方能做的更好?
- ios如果写一个提示带动画的View,可以来引导用户行为
- 如果写一个android桌面滑动切换屏幕的控件(一)
- 如果写一个android桌面滑动切换屏幕的控件(二)
- Java Swing
- C语言 01
- 什么?你还不会写JQuery 插件
- 人工智能动作
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发人员工具 总结
- 如果写一个微博的一个ListView
- poj 3273(二分+贪心)
- hdu 4901 DP
- 安装emacs出现到依赖问题及解决方案
- Great Start
- C语言02
- [WebGL入门]八,着色器的说明和基础
- rhel5.8安装ganglia-3.2.0
- Java Timer 定时器的使用