新浪微部落格戶端開發詳解(三)
上一篇博文中詳細講解了auth2.0的認證過程,本篇博文會詳細講解獲取微博資料並顯示出來的效果。
首先看效果圖:
微博資料獲取之後展示效果如圖所示。
(一)
首先我們從新浪微博的開放平臺說起。
身份認證之後,開發者會有三十組介面可以呼叫。
上面所示的介面是三十組介面中的一組介面,可以說是一類介面,這些介面是獲取微博資料的,讀取介面。意思就是說獲取到微博資料。
我在這裡詳細講解一個介面,剩下的介面舉一反三就可以的,道理是一樣的。
點選第二個介面連結,進入到該介面的詳細說明的網頁,詳細展示如下:
2/statuses/friends timeline
statuses/friends_timeline
獲取當前登入使用者及其所關注使用者的最新微博
//這裡是該介面的作用的說明,該介面實現什麼樣的功能
URL
//這裡是該介面的具體的url,向伺服器請求就是向伺服器的該介面請求,實現獲取當前登入使用者及其所關注使用者的最新微博的功能
支援格式
JSON
//這裡說明的是請求的引數的格式,使用的是JSON格式資料向伺服器發
//送引數,這裡說明一下,新浪微博的開放介面統一使用JSON格式數
//據,伺服器返回的資料也是JSON格式的,需要對JSON資料進行解
//析,不過JSON格式資料非常簡單
HTTP請求方式
GET
//這裡指明瞭向伺服器請求的方式,使用GET方式進行請求
是否需要登入
是
//這裡指明瞭該介面是需要使用者登入之後才可以獲取該介面提供的資料,
//如果這裡是“否”,說明該介面可以在使用者沒有登入的情況下實現該接
//口提供的功能。例如:分享介面就不用使用者登入就可以實現。
關於登入授權,參見 如何登入授權
訪問授權限制
訪問級別:普通介面
頻次限制:是
關於頻次限制,參見
//這裡說明該介面的訪問級別,一般的開發者身份認證成功之後會有三十
//組介面可以使用,不過是普通介面,還有一些高階介面一般開發者還不
//能使用。什麼情況下可以使用呢?可以到我的應用->介面管理->申請權 //限裡面可以申請高階介面。
//另外需要說明的是頻次限制,這個是該介面的訪問頻率的限制次數,這
//樣的限制很明顯就是為了安全嘛。不過不用擔心,這個是開發階段才有
//這樣的限制。
請求引數
//請求引數明什麼說的了,人家寫的很詳細
必選 | 型別及範圍 | 說明 | |
---|---|---|---|
source | false | string | 採用OAuth授權方式不需要此引數,其他授權方式為必填引數,數值為應用的AppKey。 |
access_token | false | string | 採用OAuth授權方式為必填引數,其他授權方式不需要此引數,OAuth授權後獲得。 |
since_id | false | int64 | 若指定此引數,則返回ID比since_id大的微博(即比since_id時間晚的微博),預設為0。 |
max_id | false | int64 | 若指定此引數,則返回ID小於或等於max_id的微博,預設為0。 |
count | false | int | 單頁返回的記錄條數,最大不超過100,預設為20。 |
page | false | int | 返回結果的頁碼,預設為1。 |
base_app | false | int | 是否只獲取當前應用的資料。0為否(所有資料),1為是(僅當前應用),預設為0。 |
feature | false | int | 過濾型別ID,0:全部、1:原創、2:圖片、3:視訊、4:音樂,預設為0。 |
trim_user | false | int | 返回值中user欄位開關,0:返回完整user欄位、1:user欄位僅返回user_id,預設為0。 |
注意事項
無
呼叫樣例及除錯工具
返回結果
//返回結果使用的是JSON格式的資料
JSON示例
關於錯誤返回值與錯誤程式碼,參見 錯誤程式碼說明
返回欄位說明
返回值欄位 | 欄位型別 | 欄位說明 |
---|---|---|
created_at | string | 微博建立時間 |
id | int64 | 微博ID |
mid | int64 | 微博MID |
idstr | string | 字串型的微博ID |
text | string | 微博資訊內容 |
source | string | 微博來源 |
favorited | boolean | 是否已收藏,true:是,false:否 |
truncated | boolean | 是否被截斷,true:是,false:否 |
in_reply_to_status_id | string | (暫未支援)回覆ID |
in_reply_to_user_id | string | (暫未支援)回覆人UID |
in_reply_to_screen_name | string | (暫未支援)回覆人暱稱 |
thumbnail_pic | string | 縮圖片地址,沒有時不返回此欄位 |
bmiddle_pic | string | 中等尺寸圖片地址,沒有時不返回此欄位 |
original_pic | string | 原始圖片地址,沒有時不返回此欄位 |
geo | object | 地理資訊欄位 詳細 |
user | object | 微博作者的使用者資訊欄位 詳細 |
retweeted_status | object | 被轉發的原微博資訊欄位,當該微博為轉發微博時返回 詳細 |
reposts_count | int | 轉發數 |
comments_count | int | 評論數 |
attitudes_count | int | 表態數 |
mlevel | int | 暫未支援 |
visible | object | 微博的可見性及指定可見分組資訊。該object中type取值,0:普通微博,1:私密微博,3:指定分組微博,4:密友微博;list_id為分組的組號 |
pic_ids | object | 微博配圖ID。多圖時返回多圖ID,用來拼接圖片url。用返回欄位thumbnail_pic的地址配上該返回欄位的圖片ID,即可得到多個圖片url。 |
ad | object array | 微博流內的推廣微博ID |
以上便是對於一個介面的詳細說明了。看懂了一個介面,搞明白之後,其他的介面跟這個異曲同工,舉一反三,就很容易看懂,沒什麼問題。真正帶開發只用看引數和返回值就可以了。
(二)
下面我貼出該介面的實現類。
public class HomeAcitivity extends Activity
{
private ListView listview ;
private ProgressBar progressbar;
private List<Status> statuses;//每條微博看作一個物件Status,獲取的好友微博使用List儲存
private Intent intent;
private int postionIndex;
private Oauth2AccessToken mAccessToken;//這個類非常關鍵,三十組介面的呼叫均需要使用該類中所儲存的Token和code值、expire值等
private WeiboManager weibomanager;//這個是自動義的微博管理類,使用該類獲取或者操作微博。就是說一切對於微博的操作均在微博管理類中實現,稍後會貼出該類的程式碼
@SuppressLint("HandlerLeak")
private Handler handler = new Handler()
//這是android中的訊息傳遞機制,因為訪問網路資料在最新的android sdk中是不允許在主執行緒中訪問網路資源,必須另外開啟執行緒,當另外的執行緒獲取到資源之後,利用handler把資料返回到主執行緒,下面的message就是封裝了利用handler傳遞到主執行緒的資料。
{
@SuppressWarnings({ "unchecked", "static-access" })
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 0x111:
progressbar.setVisibility(View.GONE);
listview.setVisibility(View.VISIBLE);
statuses = (List<Status>)msg.obj;//獲取成功之後,利用message封裝資料,利用handler把資料傳遞到主執行緒
msg.obj = statuses;
listview.setAdapter(new WeiboListAdapter(HomeAcitivity.this,statuses,R.id.publicStatus));
handler.sendMessage(msg);}}.start();//初始狀態下,載入新微博
listview.setOnItemLongClickListener(new OnItemLongClickListener()
//對每條微博設定一個監聽器,長按可以彈出一個選單項,選單項中包括重新整理微博、檢視單條微博、退出等。
{ @Override
public boolean onItemLongClick(AdapterView<?> arg0, View v, int postion, long id){
boolean bl = listview.showContextMenu();
postionIndex = postion;
return true;
}
});
listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
//這個是初始化選單項,利用R。menu.menu實現,需要在res資原始檔夾下的建立一個固定名字為
//menu的資料夾,該資料夾下有一個固定名字的menu.xml實現,如果各位看官不知道這個怎麼回事,請另外搜尋如何定義選單項
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo info) {
MenuInflater inflator = getMenuInflater();
inflator.inflate(R.menu.menu,menu);
menu.setHeaderTitle("選單");
}
});
mAccessToken = AccessTokenKeeper.readAccessToken(HomeAcitivity.this);//這個是獲取Token的工具類,在sinasdk-core.jar中已經實現好了,可以直接使用
}
@Override
public boolean onContextItemSelected(MenuItem item)//這個方法就是對定義的選單中的每一項可以實現的功能
{
switch (item.getItemId())
{
case R.id.refresh://重新整理
<span style="white-space:pre"> </span>new Thread()
{
@SuppressWarnings("static-access")
@Override
public void run()
{
List<Status> statuses1 = weibomanager.getHomeTimeline();
Message msg = new Message();
msg.obj = statuses1;
msg.what = 0x110;
handler.sendMessage(msg);
}
}.start();
break;
case R.id.detail://檢視微博
WeiboListAdapter weibolistadapter = new WeiboListAdapter(HomeAcitivity.this,statuses,R.id.detail);
Status status = weibolistadapter.getItem(postionIndex);
if(status != null)
{
Bundle bundle = new Bundle();
bundle.putSerializable("status", status);
Intent intent = new Intent(HomeAcitivity.this,StatusDetailActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}else { }
break;
case R.id.exit://退出
AlertDialog.Builder alertdialog = new AlertDialog.Builder(HomeAcitivity.this);
alertdialog.setTitle("退出微博");
alertdialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{
AccessTokenKeeper.clear(HomeAcitivity.this);
Log.d("clear==",mAccessToken.toString());
AccountAPI accountapi = new AccountAPI(mAccessToken);
requestListener requestLis = new requestListener();
accountapi.endSession(requestLis);
String json = null;
json = requestLis.get();
Message msg2 = new Message();
msg2.obj = json + "null";
msg2.what = 0x120;
handler.sendMessage(msg2);
finish();
}
});
alertdialog.setNegativeButton(android.R.string.cancel,new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
alertdialog.show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
}
上面的程式碼是activity實現類,根據我的註釋應該還是很容易理解的,不過裡面用到了weiboManager類和WeiboListAdapter介面卡,在此詳細說明WeiboManager類和WeiboListAdapter類.
(三)
WeiboManager類比較簡單,使用的是已經實現好的工具類。先看程式碼:
public class WeiboManager implements Constants
{
private static String json;
private static List<Status> statuses = null;
private static Oauth2AccessToken mAccessToken;
private static StatusesAPI statusesapi;
public WeiboManager(){}//預設構造器
public WeiboManager(Context context)//需要Context引數的構造器,這裡使用Context最好
{
mAccessToken = AccessTokenKeeper.readAccessToken(context);
statusesapi= new StatusesAPI(mAccessToken);
//微博介面api,這個工具類是在sinasdk.jar包中已經封裝好了,可以直接使用,這個jar包的下載地址我會在
//博文最後給出jar包的下載地址,供大家使用。當然這個jar包的工具類遠不止這一個
//AccountAPI CommentsAPI CommonAPI FavoritesAPI FriendshipsAPI PlaceAPI RegisterAPI等等這個api介面均在
//該jar包中定義好,開發者只需給定引數呼叫介面即可。
}
@SuppressWarnings("unchecked")
public static List<Status> getHomeTimeline()
{
/**
* StatusesAPI是微博api介面,裡面定義的方法有很多,
* publicTimeline 返回最新的公共微博
* friendsTimeline 獲取當前登入使用者及其所關注使用者的最新微博
* friendsTimelineIds 獲取當前登入使用者及其所關注使用者的最新微博的ID
* userTimeline 獲取某個使用者最新發表的微博列表(與下面這個方法引數不同)
* userTimeline 獲取某個使用者最新發表的微博列表
* repostTimeline 獲取指定微博的轉發微博列表
* 等等 這些方法均是在StatusesAPI工具類中已經實現好的,我在這裡直接使用homeTimeline獲取好友微博列表
* 的方法。由於並不是做很大的應用,這裡只是學習測試,所以這裡的引數也是給定的。
*
* homeTimeline方法中的引數說明如下:
* 獲取當前登入使用者及其所關注使用者的最新微博
* public void homeTimeline( long since_id, long max_id, int count, int page,
* boolean base_app, FEATURE feature, boolean trim_user, RequestListener listener)
* @param since_id 若指定此引數,則返回ID比since_id大的微博(即比since_id時間晚的微博),預設為0。
* @param max_id 若指定此引數,則返回ID小於或等於max_id的微博,預設為0。
* @param count 單頁返回的記錄條數,預設為50。
* @param page 返回結果的頁碼,預設為1。
* @param base_app 是否只獲取當前應用的資料。false為否(所有資料),true為是(僅當前應用),預設為false。
* @param feature 過濾型別ID,0:全部、1:原創、2:圖片、3:視訊、4:音樂,預設為0。
* @param trim_user 返回值中user欄位開關,false:返回完整user欄位、true:user欄位僅返回user_id,預設為false。
* @param listener
* 對於RequestListener監聽器我在詳細說明一下,如果大家的記性好的話,這個監聽器大家是用到過的。
* 在進行auth2.0授權認證的時候,獲取Token值的時候,這個Token值得獲取就是通過監聽器回撥得到的,原理還這裡用
* 到的是一樣的。
* 當呼叫該方法的時候,會把該方法中的new RequestListener()匿名監聽器物件作為引數傳遞,當伺服器返回響應
* 會自動的呼叫該監聽器的方法,如果成功,呼叫onComplete方法,伺服器返回的資料就是response引數的值
* 如果伺服器由於某種原因錯誤,則會呼叫其他的方法。
**/
statusesapi.homeTimeline(0L,0L,10,1,false,FEATURE.ALL,false,new RequestListener(){
@Override
public void onComplete(String response)
{
if (response == null)
{
return;
}
this.json = response;//伺服器返回的資料是JSON格式的字串資料,
statuses = JSONAndObject.convert(Status.class, json, "statuses");//這裡把json字串資料轉化為微博物件
}
@Override
public void onComplete4binary(ByteArrayOutputStream responseOS)
{
// TODO Auto-generated method stub
}
@Override
public void onIOException(IOException e)
{
// TODO Auto-generated method stub
}
@Override
public void onError(WeiboException e)
{
// TODO Auto-generated method stub
}
});
return statuses;
}
}
/**
*
*該工具類就是把json字串轉化為微博物件,是一個工具類,可以直接使用的。
**/
public class JSONAndObject
{
// 使用該方法時需要先建立一個Object,傳入第1個引數
public static Object convertSingleObject(Object obj, String json)
{
if (obj == null || json == null)
return obj;
try
{
// 只使用public型別欄位
Field[] fields = obj.getClass().getFields();
if (fields != null)
{
JSONObject jsonObject = new JSONObject(json);
for (Field field : fields)
{
try
{
// 如果json中沒有相應的key,會丟擲異常,繼續掃描下一個key
Object objValue = jsonObject.get(field.getName());
// 字串型別
if (field.getType() == String.class)
{
field.set(obj, String.valueOf(objValue));
}
// long型別
else if (field.getType() == long.class)
{
field.set(obj,Long.valueOf(String.valueOf(objValue)));
}
// int型別
else if (field.getType() == int.class)
{
field.set(obj,Integer.valueOf(String.valueOf(objValue)));
}
// boolean型別
else if (field.getType() == boolean.class)
{
field.set(obj, Boolean.getBoolean(String.valueOf(objValue)));
}
// Object型別(WeiboObject型別)
else
{
Object fieldObject = field.getType().newInstance();
if (fieldObject instanceof WeiboObject)
{
convertSingleObject(fieldObject,String.valueOf(objValue));
field.set(obj, fieldObject);
}
}
}
catch (Exception e)
{
// TODO: handle exception
}
}
}
}
catch (Exception e)
{
}
return obj;
}
// 將json字串轉換為List
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List convert(Class c, String json, String propertyName)
{
List objs = null;
if (c == null || json == null)
return objs;
try
{
// 只使用public型別欄位
Field[] fields = c.getFields();
if (fields != null)
{
String jsonStr = json;
if (propertyName != null)
{
JSONObject jsonObject = new JSONObject(json);
jsonStr = jsonObject.get(propertyName).toString();
}
JSONArray jsonArray = new JSONArray(jsonStr);
objs = new ArrayList();
for (int i = 0; i < jsonArray.length(); i++)
{
Object obj = c.newInstance();
objs.add(obj);
convertSingleObject(obj, jsonArray.getString(i));
}
}
}
catch (Exception e)
{
Log.d("convert==", e.getMessage());
}
return objs;
}
public static String convertSingleObjectToJson(Object obj)
{
String json = null;
if (obj == null)
{
return json;
}
Field[] fields = obj.getClass().getFields();
json = "{";
for (int i = 0; i < fields.length; i++)
{
try
{
Field field = fields[i];
if (field.getType() == String.class)
{
String temp = ((field.get(obj) == null) ? "" : String
.valueOf(field.get(obj)));
temp = temp.replaceAll("\"", "\\\\\"");
json += "\"" + field.getName() + "\":\"" + temp + "\"";
}
// long型別
else if (field.getType() == long.class)
{
json += "\"" + field.getName() + "\":" + field.getLong(obj);
}
// int型別
else if (field.getType() == int.class)
{
json += "\"" + field.getName() + "\":" + field.getInt(obj);
}
// boolean型別
else if (field.getType() == boolean.class)
{
json += "\"" + field.getName() + "\":"
+ field.getBoolean(obj);
}
// Object型別(WeiboObject型別)
else
{
Object fieldObject = field.get(obj);
if (fieldObject instanceof WeiboObject)
{
json += "\"" + field.getName() + "\":"
+ convertSingleObjectToJson(fieldObject);
}
else
{
continue;
}
}
if (i < fields.length - 1)
{
json += ",";
}
}
catch (Exception e)
{
// TODO: handle exception
}
}
json += "}";
return json;
}
public static String convertObjectToJson(Object obj, String propertyName)
{
String json = null;
if (obj == null)
return json;
if (obj instanceof List)
{
List list = (List) obj;
if (propertyName != null)
{
// 包含一個屬性的物件,這個屬性是物件陣列
json = "{\"" + propertyName + "\":[";
}
else
{
// 物件陣列
json = "[";
}
for (int i = 0; i < list.size(); i++)
{
Object item = list.get(i);
json += convertSingleObjectToJson(item);
if (i < list.size() - 1)
json += ",";
}
if (propertyName != null)
{
json += "]}";
}
else
{
json = "]";
}
}
return json;
}
}
(四)
下面給出WeiboListAdapter類的實現程式碼,大家應該對BaseAdapter很熟悉了。
public class WeiboListAdapter extends BaseAdapter implements Serializable
{
Activity activity;
protected LayoutInflater layoutInflater;
protected int faceType;
protected List<Status> statuses;
public static View view = null;
@Override
public int getCount()
{
return statuses.size();
}
@Override
public Status getItem(int position)
{
return statuses.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
//這裡使用的是圖片下載機制,使用非同步方式下載
public static Drawable loadImage(String url,ImageView imgeview)
{
AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
Drawable drawable = asyncImageLoader.loadDrawable(url,imgeview , new ImageCallback()
{
@Override
public void imageLoaded(Drawable imageDrawable, ImageView imageView,String imageUrl)
{
imageView.setImageDrawable(imageDrawable);
}
});
return drawable;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.weibosdkdemo_list_item, null);
}
View weiboListItem = convertView.findViewById(R.id.linearlayout_weibo_list_item);
if (position < statuses.size())
{
weiboListItem.setVisibility(View.VISIBLE);
Status status = statuses.get(position);
TextView statusText = (TextView) convertView.findViewById(R.id.textview_text);//微博文字內容
TextView name = (TextView) convertView.findViewById(R.id.textview_name);//微博作者
TextView createdAt = (TextView) convertView.findViewById(R.id.textview_created_at);//微博釋出時間
ImageView profileImage = (ImageView)convertView.findViewById(R.id.imageview_profile_image);//微博作者頭像
profileImage.setImageResource(R.drawable.portrait);//設定微博作者頭像(採用預設頭像)
ImageView picture = (ImageView) convertView.findViewById(R.id.imageview_picture);//微博帶影象標誌
ImageView statusImage = (ImageView) convertView.findViewById(R.id.imageview_status_image);//微博內容圖片
ImageView verified = (ImageView) convertView.findViewById(R.id.imageview_verified);//認證使用者圖示
verified.setVisibility(View.GONE);
if (status.user != null)//如果微博作者不空
{
Tools.userVerified(verified, status.user.verified_type);
}
LinearLayout insideContent = (LinearLayout) convertView
.findViewById(R.id.linearlayout_inside_content);//微博內容的線性佈局
ImageView retweetdetailImage = (ImageView) convertView
.findViewById(R.id.imageview_retweetdetail_image);//轉發微博的圖片
TextView retweetdetailText = (TextView) convertView
.findViewById(R.id.textview_retweetdetail_text);//轉發微博的文字內容
TextView source = (TextView) convertView.findViewById(R.id.textview_source);//顯示微博來源
// 當前微博有影象,設定微博圖片可見
//如果微博中存在圖片(包含轉發微博中的圖片),則設定圖片標記可見
//如果微博中存在圖片(不是轉發)則設定圖片可見
//如果轉發微博中存在圖片 則設定轉發微博圖片可見
//初始化
picture.setVisibility(View.INVISIBLE);
statusImage.setVisibility(View.GONE);
retweetdetailImage.setVisibility(View.GONE);
if (WeiboManager.hasPicture(status))
{
if ( (status.thumbnail_pic != null) && (!"".equals(status.thumbnail_pic) ))//如果微博有圖片
{
statusImage.setVisibility(View.VISIBLE);
picture.setVisibility(View.VISIBLE);
}
if(status.retweeted_status != null)
{
if ( (status.retweeted_status.thumbnail_pic != null)
&& (!"".equals(status.retweeted_status.thumbnail_pic)))
{
retweetdetailImage.setVisibility(View.VISIBLE);//如果轉發微博內容有圖片
picture.setVisibility(View.VISIBLE);
}
}
}else//否則設定微博圖片不可見
{
picture.setVisibility(View.INVISIBLE);
statusImage.setVisibility(View.GONE);
retweetdetailImage.setVisibility(View.GONE);
}
// 裝載影象 頭像設定
Drawable profilePortrait=null;
if (status.user != null)
{
profilePortrait = loadImage(status.user.profile_image_url,profileImage);//如果微博使用者存在
}
if(profilePortrait == null)
{
profileImage.setImageResource(R.drawable.portrait);
}else
{
// profileImage.setImageDrawable(profilePortrait);
}
profilePortrait=null;//再次初始化
//裝載微博圖片,預設情況下為GONE
Drawable profileStatusImage = null;
if (status != null && status.thumbnail_pic!="")
{
profileStatusImage = loadImage(status.thumbnail_pic,statusImage);
}
profileStatusImage = null;
statusText.setText(Tools.changeTextToFace(activity,Html.fromHtml(Tools.atBlue(status.text))));//設定微博文字內容,同時對字元進行處理
if (status.user != null)
name.setText(status.user.name);//設定微博作者
createdAt.setText(Tools.getTimeStr(status.getCreatedAt(),new Date()));//設定微博釋出時間
source.setText("來自 " + status.getTextSource());//設定微博來源
if (status.retweeted_status != null && status.retweeted_status.user != null)//如果是轉發微博,並且轉發微博的作者存在
{
insideContent.setVisibility(View.VISIBLE);//設定轉發微博內容的線性佈局可見
retweetdetailText.setText(Html.fromHtml(Tools.atBlue("@"
+ status.retweeted_status.user.name + ":"
+ status.retweeted_status.text)));//設定轉發微博的文字內容,同時對字元進行處理
Drawable profileStatusThumbnailPic = null;
if (status != null && status.retweeted_status.thumbnail_pic != "")
{
profileStatusThumbnailPic = loadImage(status.retweeted_status.thumbnail_pic,retweetdetailImage);
}
profileStatusThumbnailPic = null;
}else//否則設定微博內容的線性佈局不可見
{
insideContent.setVisibility(View.GONE);
}
}
else//如果獲取position位置 超過了微博List的長度,設定不可見
{
weiboListItem.setVisibility(View.GONE);
}
view = convertView;
return convertView;
}
public WeiboListAdapter(Activity activity)//構造器
{
this.activity = activity;
}
public WeiboListAdapter(Activity activity, List<Status> statuses, int faceType)
{
this.activity = activity;
this.faceType = faceType;
layoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.statuses = new ArrayList<Status>();
if (statuses != null)
this.statuses.addAll(statuses);
}
}
WeiboListAdapter類中的佈局檔案如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/linearlayout_weibo_list_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:orientation="horizontal" >
<!-- 左側的佈局 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- 頭像 -->
<ImageView
android:id="@+id/imageview_profile_image"
android:layout_width="61dp"
android:layout_height="61dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:src="@drawable/portrait" />
<!-- 認證影象 -->
<ImageView
android:id="@+id/imageview_verified"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_gravity="right|bottom"
android:src="@drawable/v_blue"
android:visibility="gone" />
</FrameLayout>
<!-- 右側佈局 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<!-- 顯示使用者名稱 -->
<TextView
android:id="@+id/textview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#F00"
android:textSize="14sp" />
<LinearLayout
android:id="@+id/linearlayout_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_toRightOf="@id/textview_name"
android:gravity="right"
android:orientation="horizontal" >
<!-- 如果微博帶影象會顯示一個標記 -->
<ImageView
android:id="@+id/imageview_picture"
android:layout_width="16dp"
android:layout_height="12dp"
android:layout_marginTop="3dp"
android:src="@drawable/pic"
android:visibility="invisible" />
<!-- 顯示微博釋出時間 -->
<TextView
android:id="@+id/textview_created_at"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:textColor="#F18D00"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textview_name"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<!-- 顯示微博內容 -->
<TextView
android:id="@+id/textview_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:linksClickable="false"
android:textColor="#000"
android:textSize="14sp" />
<LinearLayout
android:id="@+id/linearlayout_inside_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/content_border"
android:orientation="vertical"
android:visibility="gone" >
<!-- 如果是轉發微博,顯示原微博內容 -->
<TextView
android:id="@+id/textview_retweetdetail_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:linksClickable="false"
android:textColor="#000"
android:textSize="