接上一篇,我们已经有了自定义的ListView了,在其中具备了Header和Footer,现在这个ListView已经可以在多个类似的界面中使用了,而且Header和Footer都可以使用更复杂的View来实现。
接下来,是为ListView添加数据的时候了,根据《Android控件组合应用一》一文中对数据结构的描述,我们在MainActivity中添加一个方法initSite()用于构造测试数据。该类的代码如下:
- package com.wenbin.test;
- import com.wenbin.test.site.Blog;
- import com.wenbin.test.site.SinaSite;
- import com.wenbin.test.site.Site;
- import com.wenbin.test.site.User;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.TextView;
- public class MainActivity extends Activity implements OnItemClickListener {
- private TextView usernameTextview;
- private Site site;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initSite();
- usernameTextview=(TextView)findViewById(R.id.TextViewUsername);
- BlogListView sinaListView=(BlogListView)findViewById(R.id.sinaList);
- sinaListView.setOnItemClickListener(this);
- sinaListView.init(site);
- findViewById(R.id.BtnWrite).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- //TODO:
- }
- });
- findViewById(R.id.BtnRefresh).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- //TODO:
- }
- });
- updateUserNameTextView();
- }
- private void updateUserNameTextView() {
- usernameTextview.setText(R.string.unAuthUser);
- }
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- //TODO:
- }
- private void initSite(){
- site=new SinaSite();
- Blog blog=new Blog(site);
- blog.setPic("http://ww3.sinaimg.cn/thumbnail/6b8527b3jw6dbydoikpzuj.jpg");
- User user=new User();
- user.setVerified(true);
- blog.setUser(user);
- site.addBlog(blog);
- blog=new Blog(site);
- user=new User();
- user.setVerified(true);
- blog.setUser(user);
- Blog retBlog=new Blog();
- user=new User();
- user.setProfileImageUrl("");
- user.setScreenName("reply");
- retBlog.setUser(user);
- retBlog.setText("[爱你]求喜欢");
- retBlog.setPic("http://tp4.sinaimg.cn/1658122963/50/1282754213");
- blog.setRetweetedBlog(retBlog);
- site.addBlog(blog);
- for(int i=0;i<5;i++){
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- user=new User();
- blog=new Blog(site);
- blog.setUser(user);
- site.addBlog(blog);
- }
- }
- }
然后,我们为每个列表项来写一个布局文件blogview.xml,布局如下:
- <?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:orientation="vertical"
- android:padding="10dp" android:descendantFocusability="blocksDescendants"
- android:background="@color/white">
- <RelativeLayout android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <WebView android:id="@+id/profileImage"
- android:layout_width="48dp"
- android:layout_height="48dp"
- android:scrollbars="none"
- android:layout_alignParentLeft="true"
- android:background="@drawable/portrait">
- </WebView>
- <ImageView android:id="@+id/vImage"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignRight="@+id/profileImage"
- android:layout_alignBottom="@+id/profileImage"
- android:src="@drawable/v">
- </ImageView>
- <RelativeLayout android:layout_alignParentRight="true"
- android:layout_toRightOf="@+id/profileImage"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_marginLeft="10dp">
- <TextView android:id="@+id/userName" android:layout_height="wrap_content" android:layout_alignParentTop="true"
- android:textColor="@color/red"
- android:textSize="15sp"
- android:paddingBottom="5dp" android:layout_width="fill_parent">
- </TextView>
- <TextView android:id="@+id/blogText" android:layout_height="wrap_content"
- android:layout_below="@+id/userName"
- android:textColor="@color/black"
- android:textSize="15sp" android:layout_width="wrap_content">
- </TextView>
- <WebView android:id="@+id/smallImage"
- android:layout_width="68dp"
- android:layout_height="48dp"
- android:scrollbars="none"
- android:layout_below="@+id/blogText"
- android:background="@drawable/image">
- </WebView>
- <RelativeLayout android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/smallImage"
- android:background="@drawable/popup"
- android:paddingTop="10dp"
- android:paddingBottom="5dp"
- android:paddingLeft="5dp"
- android:paddingRight="5dp"
- android:id="@+id/reBlog">
- <TextView android:id="@+id/reBlogText" android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/reUserName"
- android:textColor="@color/black"
- android:textSize="15sp">
- </TextView>
- <WebView android:id="@+id/reImage"
- android:layout_width="68dp"
- android:layout_height="48dp"
- android:scrollbars="none"
- android:layout_below="@+id/reBlogText"
- android:background="@drawable/image">
- </WebView>
- </RelativeLayout>
- <TextView android:id="@+id/sourceText" android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/userName"
- android:layout_below="@+id/reBlog"
- android:textColor="@color/blue"
- android:textSize="10sp"
- android:paddingBottom="5dp">
- </TextView>
- </RelativeLayout>
- </RelativeLayout>
- </LinearLayout>
至此,可以通过派生ListAdapter来实现我们的Adapter了,代码如下:
- package com.wenbin.test;
- import java.util.Iterator;
- import java.util.Set;
- import com.wenbin.test.site.Blog;
- import android.content.Context;
- import android.database.DataSetObserver;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.webkit.WebView;
- import android.widget.Adapter;
- import android.widget.ImageView;
- import android.widget.ListAdapter;
- import android.widget.TextView;
- public class BlogAdapter implements ListAdapter {
- private static final String AT = "@";
- private static final String COLON=": ";
- private Set<Blog> blogs;
- private Context context;
- public BlogAdapter(Set<Blog> blogs,Context context){
- this.blogs=blogs;
- this.context=context;
- }
- @Override
- public boolean areAllItemsEnabled() {
- return true;
- }
- @Override
- public boolean isEnabled(int arg0) {
- return true;
- }
- @Override
- public int getCount() {
- return blogs.size();
- }
- @Override
- public Blog getItem(int position) {
- if (position<0 || position>=getCount())
- return null;
- if (blogs!=null){
- Iterator<Blog> iterator=blogs.iterator();
- int i=0;
- while(iterator.hasNext()){
- if (i==position){
- return iterator.next();
- }
- else{
- iterator.next();
- i++;
- }
- }
- }
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @Override
- public int getItemViewType(int position) {
- return Adapter.IGNORE_ITEM_VIEW_TYPE;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view=null;
- if (convertView==null){
- LayoutInflater li=LayoutInflater.from(context);
- view=li.inflate(R.layout.blogview, null);
- }
- else{
- view=convertView;
- }
- Blog blog=getItem(position);
- if (blog!=null){
- updateBlogView(view, blog);
- if (blog.isHaveRetweetedBlog()&& blog.getInReplyBlogText().length()>0){
- updateRetweeteBlogView(view, blog);
- }
- else{
- view.findViewById(R.id.reBlog).setVisibility(View.GONE);
- view.findViewById(R.id.reImage).setVisibility(View.GONE);
- }
- }
- view.setTag(blog);
- return view;
- }
- /**
- * @param view
- * @param blog
- */
- private void updateBlogView(View view, Blog blog) {
- TextView userName=(TextView)view.findViewById(R.id.userName);
- TextView blogText=(TextView)view.findViewById(R.id.blogText);
- WebView profileImage=(WebView)view.findViewById(R.id.profileImage);
- ImageView vImage=(ImageView)view.findViewById(R.id.vImage);
- WebView smallImage=(WebView)view.findViewById(R.id.smallImage);
- TextView sourceText=(TextView)view.findViewById(R.id.sourceText);
- userName.setText(blog.getUser().getScreenName());
- sourceText.setText(context.getString(R.string.from)+blog.getSource());
- blogText.setText(blog.getText());
- profileImage.loadUrl(blog.getUser().getProfileImageUrl());
- if (!blog.getUser().isVerified())
- vImage.setVisibility(View.INVISIBLE);
- if (blog.getSmallPic().length()>0){
- smallImage.loadUrl(blog.getSmallPic());
- }
- else{
- smallImage.setVisibility(View.GONE);
- }
- }
- /**
- * @param view
- * @param blog
- */
- private void updateRetweeteBlogView(View view, Blog blog) {
- TextView reBlogText=(TextView)view.findViewById(R.id.reBlogText);
- WebView reImage=(WebView)view.findViewById(R.id.reImage);
- if (blog.getInReplyBlogText().length()>0){
- if (blog.getInReplyUserScreenName().length()>0){
- reBlogText.setText(AT+blog.getInReplyUserScreenName()+COLON+blog.getInReplyBlogText());
- }
- else{
- reBlogText.setText(blog.getInReplyBlogText());
- }
- }
- else{
- reBlogText.setVisibility(View.GONE);
- }
- if (blog.getRetweetedBlog().getSmallPic().length()>0){
- reImage.loadUrl(blog.getRetweetedBlog().getSmallPic());
- }
- else{
- reImage.setVisibility(View.GONE);
- }
- }
- @Override
- public int getViewTypeCount() {
- return 1;
- }
- @Override
- public boolean hasStableIds() {
- return false;
- }
- @Override
- public boolean isEmpty() {
- return false;
- }
- @Override
- public void registerDataSetObserver(DataSetObserver observer) {
- }
- @Override
- public void unregisterDataSetObserver(DataSetObserver observer) {
- }
- }
运行,界面如下图所示:
初具雏形,还差的细节包括:表情符未显示,@昵称未高亮显示,URL未展现成链接,图片的外围有白框,图片下载过程中无默认背景。好吧,欲知后事如何,且看下篇分解。
——欢迎转载,转载请注明出处 http://blog.csdn.net/caowenbin ——