实现ListView的条目下自动隐藏显示的布局

这个想法是我在看了sina微博的塞班客户端的微博显示效果而想移植到Android平台上,因为它的体验很好,而我们做的效果就是要方便,要用户有很好的体验,但是可惜的是在sina官方的Android客户端没有实现这种效果!

废话少说先贴图,看效果:

1。没有点击ListView之前


2。点击之后弹出四个Button



 
 3。当点收起的时候又回到1的状态

当然这个只是刚做出来的效果,界面还没有具体的美化,所以看起来还勉强,呵呵!

追求完美,还需要很多的工作,废话少说,代码+讲解如下:

 

我之前从来没有做过这种的效果,出发点也不知道,跟大多数的人一样,沉思!

但是之后baidu+google,一遍不行再来,反复的找,但是也没找到解决的办法,目前还没有做这种的效果

莫得办法,自谋出路!ok

 

第一步:找高人博客(就看关于ListView的介绍)

我们看的不是别人怎么写出来的代码,看的是思想,人家是怎么考虑的,我看过的如下:

http://blog.csdn.net/flowingflying/archive/2011/03/28/6283942.aspx

http://blog.csdn.net/flowingflying/archive/2011/03/29/6286767.aspx

http://blog.csdn.net/flowingflying/archive/2011/03/31/6292017.aspx

这位对ListView的研究很深啊!上面三篇都是,逐步深入,对于初学者绝对是推荐

看了你就爱上他吧!总之一句话!我的神啊!哈哈哈

好了看到这儿,我想你的第一步就是因该马上收藏哥的这篇博客,赶紧看他的吧!他才是哥的灵感,然后再看我的blog

我想这才会理解,我为什么会这么做!ok!just do it!

 

下面是我看了后的感觉:吸取到的精华就是:

1.MVC模式:初学者不知者无罪,但是要知道M V C 三个代表什么,那部分代码因该是那部分的实现。

2.设计自己的ListView的关键是什么:我告诉你,是ListView和数据的桥梁Adapter,不管是什么BaseAdapter,SimpleAdapter一样,莫得区别了。

3.你需要知道Adapter的每个函数是怎么工作的,譬如说在调用getView之前你知道它会干什么吗?调用getCount?为什么会调用getCount呢?我说为什么不呢?因为偶去测试了,它就去先去调用了getCount,不会又神来告诉你了!需要你自己去do!不如你去试试吧,把它的返回值设为0,你放心你的getView不灵光了!呵呵

4.在Adapter中最重要的是什么?想想吧!告诉你是getView,然后它是怎么工作的?调用的机制是什么?怎么调用的?

它是一行行绘制,还是一下子搞完?ok?do it!

 

好了,还有许多就不一一列举,希望你看了这些也能获得这些知识,一个字悟道吧!就是这么出来的!不知道的可以交流,呵呵

 

第二步:编码

1.布局文件list.xml

 

List.xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >   
  7.     <LinearLayout   
  8.         android:id="@+id/mainlayout"  
  9.         android:orientation="horizontal"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content">   
  12.         <LinearLayout   
  13.             android:id="@+id/layout1"  
  14.             android:orientation="horizontal"  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content">   
  17.             <ImageView   
  18.                 android:id="@+id/wbIcon"  
  19.                 android:src="@drawable/sina"  
  20.                 android:layout_width="50dip"  
  21.                 android:layout_height="50dip"  
  22.                 android:layout_marginLeft="5dip"  
  23.                 android:layout_marginTop="5dip"  
  24.                 android:layout_marginRight="5dip"  
  25.                 android:adjustViewBounds="true">   
  26.             </ImageView>   
  27.         </LinearLayout>   
  28.         <LinearLayout   
  29.             android:id="@+id/layout2"  
  30.             android:orientation="vertical"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_toRightOf="@+id/layout1"  
  34.             android:layout_alignParentRight="true">   
  35.             <RelativeLayout   
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:layout_alignParentRight="true">   
  39.                 <TextView   
  40.                     android:id="@+id/wbUser"  
  41.                     android:text="username"  
  42.                     android:textColor="#FF0000"  
  43.                     android:textSize="15dip"  
  44.                     android:layout_width="wrap_content"  
  45.                     android:layout_height="wrap_content"  
  46.                     android:paddingLeft="5dip">   
  47.                 </TextView>   
  48.                 <TextView   
  49.                     android:id="@+id/wbTime"  
  50.                     android:text="updatetime"  
  51.                     android:textColor="#FF666666"  
  52.                     android:textSize="14dip"  
  53.                     android:layout_width="wrap_content"  
  54.                     android:layout_height="wrap_content"  
  55.                     android:paddingRight="5dip"  
  56.                     android:layout_alignBottom="@+id/wbUser"  
  57.                     android:layout_alignParentRight="true">   
  58.                 </TextView>   
  59.                 <TextView    
  60.                     android:id="@+id/wbText"    
  61.                     android:layout_height="wrap_content"    
  62.                     android:layout_width="fill_parent"  
  63.                     android:textSize="14dip"  
  64.                     android:text="the comment fill with it iii/niiiiiiiiiiiiiijkkkkkkkkkkkkkkkkkkkkkkkkk"    
  65.                     android:textColor="#424952"  
  66.                     android:layout_below="@+id/wbUser"  
  67.                     android:layout_alignParentLeft="true">   
  68.                 </TextView>   
  69.                 <LinearLayout   
  70.                     android:orientation="vertical"  
  71.                     android:layout_below="@+id/wbText"  
  72.                     android:layout_width="fill_parent"  
  73.                     android:layout_height="wrap_content">   
  74.                     <TextView   
  75.                         android:id="@+id/wbCommentText"    
  76.                         android:layout_height="wrap_content"  
  77.                         android:layout_width="fill_parent"  
  78.                         android:textSize="14dip"  
  79.                         android:textColor="#424952"  
  80.                         android:background="#CDC9A5"  
  81.                         android:text="uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu">   
  82.                     </TextView>   
  83.                     <ImageView   
  84.                         android:id="@+id/wbImage"  
  85.                         android:src="@drawable/sina"  
  86.                         android:adjustViewBounds="true"  
  87.                         android:layout_gravity="center_horizontal"  
  88.                         android:layout_height="wrap_content"  
  89.                         android:layout_width="wrap_content"  
  90.                         android:layout_marginTop="3dip">   
  91.                     </ImageView>   
  92.                 </LinearLayout>   
  93.             </RelativeLayout>   
  94.         </LinearLayout>   
  95.     </LinearLayout>   
  96. </RelativeLayout>  

 

 

当然我这个布局做的不好,效率比较低,由于时间仓促没有修改,希望大家自己完成,代码优化是很重要的,原则就一个,尽量有少的layout实现该有的效果!

不知道发现没有:如果直接想的话,我们可能直接会说:那四个键直接写在List中嘛!当然我也试过,但是效果很差,而且一旦list条数过多就出现你都想吐的感觉!my god!怎么会这样!但是事实就是这样!不过你也可以尝试,只要去尝试总会成功的!相信自己,只不过我想到了一个更好的方法去做!效果更好而已!

 

我曾尝试过三个个方案

第一:折中方案,不在每个List的下面显示,而是都显示在最下面,但是可能会覆盖下面的菜单栏,故而舍弃,不过比上面简单的多,贴上xml

list.xml

Main.xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >   
  7.     <!--    
  8.     <LinearLayout   
  9.         android:id="@+id/buttonlayout"  
  10.         android:orientation="horizontal"  
  11.         android:paddingLeft="12dip"  
  12.         android:background="#EED8AE"  
  13.         android:layout_alignParentBottom="true"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         style="@android:style/ButtonBar">   
  17.             <Button   
  18.                 android:id="@+id/wbTransmit"  
  19.                 android:layout_width="wrap_content"  
  20.                 android:layout_height="wrap_content"  
  21.                 android:layout_weight="1"  
  22.                 android:layout_alignParentBottom="true"  
  23.                 android:visibility="gone"  
  24.                 android:text="转发">   
  25.             </Button>   
  26.             <Button   
  27.                 android:id="@+id/wbComment"  
  28.                 android:layout_width="wrap_content"  
  29.                 android:layout_height="wrap_content"  
  30.                 android:layout_weight="1"  
  31.                 android:layout_alignParentBottom="true"  
  32.                 android:visibility="gone"  
  33.                 android:text="评论">   
  34.             </Button>   
  35.             <Button   
  36.                 android:id="@+id/wbRetract"  
  37.                 android:layout_width="wrap_content"  
  38.                 android:layout_height="wrap_content"  
  39.                 android:layout_weight="1"  
  40.                 android:layout_alignParentBottom="true"  
  41.                 android:visibility="gone"  
  42.                 android:text="收起">   
  43.             </Button>   
  44.             <Button   
  45.                 android:id="@+id/wbWatchComment"  
  46.                 android:layout_width="wrap_content"  
  47.                 android:layout_height="wrap_content"  
  48.                 android:layout_weight="1"  
  49.                 android:layout_alignParentBottom="true"  
  50.                 android:visibility="gone"  
  51.                 android:text="看评论">   
  52.             </Button>   
  53.     </LinearLayout>   
  54.      -->   
  55.     <ListView   
  56.         android:id="@id/android:list"  
  57.         android:divider="#EEB422"  
  58.         android:dividerHeight="3dip"  
  59.         android:fadeScrollbars="true"  
  60.         android:fastScrollEnabled="true"  
  61.         android:background="@layout/list_corner"  
  62.         android:layout_width="fill_parent"  
  63.         android:layout_height="fill_parent">   
  64.     </ListView>   
  65. </RelativeLayout>  

 

 注:在代码中的注释部分即是,他能够做出这种效果,但是在其中有许多的细节需要注意的,最外层需要RelativeLayout,其次list需要在Button之上,不能覆盖list,但是我做出来之后出现了一件诡异的事,list条目乱跳,很纠结。

 

方案二:直接将button放在list.xml中,作为list的一部分,并且隐藏,当点击list的时候显示list.xml如下:

list.xml

 

List.xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">   
  6.     <LinearLayout   
  7.         android:id="@+id/layout01"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content">   
  10.         <RelativeLayout   
  11.             android:id="@+id/relativelayout"  
  12.             android:orientation="vertical"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:paddingLeft="12dip"  
  16.             android:paddingBottom="4dip"  
  17.             >       
  18.             <ImageView   
  19.                 android:id="@+id/itemImage"  
  20.                 android:layout_margin="5dip"  
  21.                 android:src="@drawable/icon"  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content"/>   
  24.             <LinearLayout   
  25.                 android:id="@+id/layout2"  
  26.                 android:orientation="vertical"  
  27.                 android:layout_toRightOf="@+id/itemImage"  
  28.                 android:layout_width="wrap_content"  
  29.                 android:layout_height="wrap_content">   
  30.                 <TextView     
  31.                     android:id="@+id/itemTitle"  
  32.                     android:layout_width="fill_parent"    
  33.                     android:layout_height="wrap_content"    
  34.                     android:text="1111111111"  
  35.                     android:textSize="20dip"/>   
  36.                 <TextView   
  37.                     android:id="@+id/itemText"  
  38.                     android:layout_width="fill_parent"    
  39.                     android:layout_height="wrap_content"  
  40.                     android:text="2222222222"    
  41.                     android:textSize="17dip"/>   
  42.             </LinearLayout>   
  43.         </RelativeLayout>   
  44.     </LinearLayout>   
  45. <!--        
  46.     <RelativeLayout   
  47.         android:layout_below="@+id/layout01"  
  48.         android:orientation="vertical"  
  49.         android:paddingLeft="12dip"  
  50.         android:background="#EED8AE"  
  51.         android:layout_width="wrap_content"  
  52.         android:layout_height="wrap_content">   
  53.             <Button   
  54.                 android:id="@+id/wbTransmit"  
  55.                 android:focusable="false"  
  56.                 android:focusableInTouchMode="false"  
  57.                 android:clickable="false"  
  58.                 android:layout_marginLeft="5dip"  
  59.                 android:layout_width="wrap_content"  
  60.                 android:layout_height="wrap_content"  
  61.                 android:visibility="gone"  
  62.                 android:text="转发">   
  63.             </Button>   
  64.             <Button   
  65.                 android:id="@+id/wbComment"  
  66.                 android:focusable="false"  
  67.                 android:focusableInTouchMode="false"  
  68.                 android:clickable="false"  
  69.                 android:layout_width="wrap_content"  
  70.                 android:layout_height="wrap_content"  
  71.                 android:layout_marginLeft="28dip"  
  72.                 android:layout_toRightOf="@+id/wbTransmit"  
  73.                 android:layout_alignTop="@+id/wbTransmit"  
  74.                 android:visibility="gone"  
  75.                 android:text="评论">   
  76.             </Button>   
  77.             <Button   
  78.                 android:id="@+id/wbRetract"  
  79.                 android:focusable="false"  
  80.                 android:focusableInTouchMode="false"  
  81.                 android:clickable="false"  
  82.                 android:layout_width="wrap_content"  
  83.                 android:layout_height="wrap_content"  
  84.                 android:layout_marginLeft="28dip"  
  85.                 android:layout_toRightOf="@+id/wbComment"  
  86.                 android:layout_alignTop="@+id/wbTransmit"  
  87.                 android:visibility="gone"  
  88.                 android:text="收起">   
  89.             </Button>   
  90.             <Button   
  91.                 android:id="@+id/wbWatchComment"  
  92.                 android:focusable="false"  
  93.                 android:focusableInTouchMode="false"  
  94.                 android:clickable="false"  
  95.                 android:layout_width="wrap_content"  
  96.                 android:layout_height="wrap_content"  
  97.                 android:layout_alignParentRight="true"  
  98.                 android:layout_alignTop="@+id/wbTransmit"  
  99.                 android:visibility="gone"  
  100.                 android:text="看评论">   
  101.             </Button>   
  102.   
  103.     </RelativeLayout>   
  104. -->     
  105. </LinearLayout>  

 注:android:focusableInTouchMode="false" android:focusable="false" android:clickable="false"这三个是关键,一开始它是不显示的,

当点击list的时候显示,需要在点击事件中去做。但是结果是,点击后效果是出来了,但是每次四个button都出现在最后,而不是

list的下面,最后我分析,是由于getview,我们需要重写这个方法。其关键就是需要理解getView的工作机制。

 

第三步:重写BaseAdapter函数

这个是最关键的东西,代码如下:

BaseAdapter.java

Mybaseadapter代码 复制代码  收藏代码
  1. package com.woclub.utils;   
  2.   
  3. import java.util.List;   
  4.   
  5. import android.content.Context;   
  6. import android.graphics.Color;   
  7. import android.graphics.drawable.Drawable;   
  8. import android.text.Spannable;   
  9. import android.text.SpannableStringBuilder;   
  10. import android.text.style.ForegroundColorSpan;   
  11. import android.view.Gravity;   
  12. import android.view.LayoutInflater;   
  13. import android.view.View;   
  14. import android.view.ViewGroup;   
  15. import android.view.View.OnClickListener;   
  16. import android.widget.BaseAdapter;   
  17. import android.widget.Button;   
  18. import android.widget.ImageView;   
  19. import android.widget.LinearLayout;   
  20. import android.widget.TextView;   
  21.   
  22. import com.woclub.beans.WeiboHolder;   
  23. import com.woclub.beans.WeiboInfo;   
  24. import com.woclub.utils.AsyncImageLoader.ImageCallback;   
  25. import com.woclub.weibo.R;   
  26.   
  27.   
  28. public class MyBaseAdapter extends BaseAdapter{   
  29.   
  30.     private List<WeiboInfo> wbList;   
  31.     private LayoutInflater mInflater;   
  32.     private AsyncImageLoader asyncImageLoader;   
  33.     private Context mContext;   
  34.     private WeiboHolder holder = null;   
  35.     private int SelectListItem = 0;   
  36.        
  37.     public MyBaseAdapter(Context context, List<WeiboInfo> mData)   
  38.     {   
  39.         this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
  40.         this.wbList = mData;   
  41.         mContext = context;   
  42.     }   
  43.        
  44.   
  45. //  private AsyncImageLoader asyncImageLoader;   
  46.     @Override   
  47.     public int getCount() {   
  48.         // TODO Auto-generated method stub   
  49.         return wbList.size();   
  50.     }   
  51.   
  52.     @Override   
  53.     public Object getItem(int position) {   
  54.         // TODO Auto-generated method stub   
  55.         return wbList.get(position);   
  56.     }   
  57.   
  58.     @Override   
  59.     public long getItemId(int position) {   
  60.         // TODO Auto-generated method stub   
  61.         return position;   
  62.     }   
  63.   
  64.     @Override   
  65.     public View getView(int position, View convertView, ViewGroup parent) {   
  66.         // TODO Auto-generated method stub   
  67.         if (position < 0 || wbList.size() <= 0)   
  68.             return null;   
  69.         View row = convertView;   
  70.         LinearLayout layout = new LinearLayout(mContext);   
  71.         layout.setOrientation(LinearLayout.VERTICAL);   
  72.            
  73.         if(convertView == null)   
  74.         {   
  75.             //一行行的加载list   
  76.             layout.addView(addListView(position, row));   
  77.         }   
  78.         else   
  79.         {   
  80.             holder = (WeiboHolder)row.getTag();   
  81.             //这是增加四个Button控件   
  82.             layout.addView(addListView(position, row));   
  83.             int ID = this.getSelectListItem();   
  84.             //如果选择的行是当前正重新刷新(即当前载入的行)时,我们在下面加入四个button   
  85.             if(ID==position){   
  86.                 layout.addView(addButtonView());   
  87.             }   
  88.         }   
  89.         return layout;   
  90.     }   
  91.   
  92.   
  93.     /**   
  94.      * 设置text中显示的格式   
  95.      * @param wbText TextView   
  96.      * @param string 开始的字符   
  97.      * @param string2 结束字符   
  98.      */   
  99.     private void textHighlight(TextView textView,String start,String end){   
  100.         Spannable sp = (Spannable) textView.getText();   
  101.         String text = textView.getText().toString();   
  102.         int n = 0;   
  103.         int s = -1;   
  104.         int e = -1;   
  105.         while (n < text.length()) {   
  106.             s = text.indexOf(start, n);   
  107.             if (s != -1) {   
  108.                 e = text.indexOf(end, s + start.length());   
  109.                 if (e != -1) {   
  110.                     e = e + end.length();   
  111.                 } else {   
  112.                     e = text.length();   
  113.                 }   
  114.                 n = e;   
  115.                 sp.setSpan(new ForegroundColorSpan(Color.BLUE), s, e,   
  116.                         Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
  117.                 s = e = -1;   
  118.             } else {   
  119.                 n = text.length();   
  120.             }   
  121.         }   
  122.         }   
  123.        
  124.     public void setSelectListItem(int position)   
  125.     {   
  126.         this.SelectListItem = position;   
  127.     }   
  128.     public int getSelectListItem()   
  129.     {   
  130.         return this.SelectListItem;   
  131.     }   
  132.        
  133.     /**   
  134.      * 这个函数的作用是加载整个List列表   
  135.      * 是一行行的加载整个list   
  136.      * @param position行list的位置   
  137.      * @return   
  138.      */   
  139.     private View addListView(int position, View row)   
  140.     {   
  141.         asyncImageLoader = new AsyncImageLoader();   
  142.         row = mInflater.inflate(R.layout.list, null);   
  143.         //先取的控件   
  144.         holder = new WeiboHolder();   
  145.         //下面是获取xml中的实例化控件对象   
  146.         holder.wbIcon = (ImageView)row.findViewById(R.id.wbIcon);   
  147.         holder.wbUser = (TextView)row.findViewById(R.id.wbUser);   
  148.         holder.wbTime = (TextView)row.findViewById(R.id.wbTime);   
  149.         holder.wbText = (TextView)row.findViewById(R.id.wbText);   
  150.         holder.wbCommentText = (TextView)row.findViewById(R.id.wbCommentText);   
  151.         holder.wbImage = (ImageView)row.findViewById(R.id.wbImage);   
  152.         //Sets the tag associated with this view.这个tags是包含了view里面的控件,但不一定是唯一的   
  153.         //即相当于这个view重新获得了一个完整的实例化控件集合(实例由上面四步完成)   
  154.         WeiboInfo wb = wbList.get(position);   
  155.         if(wb != null)   
  156.         {   
  157.             row.setTag(wb.getId());   
  158.             holder.wbUser.setText(wb.getUserName());   
  159.             holder.wbTime.setText(wb.getTime());   
  160.             // vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE);   
  161.             // to force it to use Spannable storage so styles can be attached.   
  162.             // Or we could specify that in the XML.   
  163.             holder.wbText.setText(wb.getText(), TextView.BufferType.SPANNABLE);   
  164.             //设置微博内容的显示格式   
  165.             textHighlight(holder.wbText,"#","#");   
  166.             textHighlight(holder.wbText,"@",":");   
  167.             textHighlight(holder.wbText,"http://"," ");   
  168.                
  169.             holder.wbCommentText.setText(wb.getCommentText(),TextView.BufferType.SPANNABLE);   
  170.             textHighlight(holder.wbCommentText,"#","#");   
  171.             textHighlight(holder.wbCommentText,"@",":");   
  172.             //textHighlight2(holder.wbCommentText,new char[]{'#'},new char[]{'#'});   
  173.             //textHighlight2(holder.wbCommentText,new char[]{'@'},new char[]{':',' '});   
  174.             //载入头像   
  175.             Drawable cachedIcon = asyncImageLoader.loadDrawable(wb.getUserIcon(), holder.wbIcon, new ImageCallback() {   
  176.                    
  177.                 @Override   
  178.                 public void imageLoaded(Drawable imageDrawable, ImageView imageView,   
  179.                         String imageUrl) {   
  180.                     // TODO Auto-generated method stub   
  181.                     imageView.setImageDrawable(imageDrawable);   
  182.                 }   
  183.             });   
  184.             if(cachedIcon == null)   
  185.             {   
  186.                 holder.wbIcon.setImageResource(R.drawable.sina);   
  187.             }   
  188.             else    
  189.             {   
  190.                 holder.wbIcon.setImageDrawable(cachedIcon);   
  191.             }   
  192.                
  193.             //载入图片   
  194.             if(wb.getImage() != null)   
  195.             {   
  196.                 Drawable cachedImage = asyncImageLoader.loadDrawable(wb.getImage(), holder.wbImage, new ImageCallback() {   
  197.                        
  198.                     @Override   
  199.                     public void imageLoaded(Drawable imageDrawable, ImageView imageView,   
  200.                             String imageUrl) {   
  201.                         // TODO Auto-generated method stub   
  202.                         imageView.setImageDrawable(imageDrawable);   
  203.                     }   
  204.                 });   
  205.                 if(cachedImage == null)   
  206.                 {   
  207.                     holder.wbImage.setImageResource(R.drawable.sina);   
  208.                 }   
  209.                 else   
  210.                 {   
  211.                     holder.wbImage.setImageDrawable(cachedImage);   
  212.                 }   
  213.             }   
  214.                
  215.         }   
  216.         row.setTag(holder);   
  217.         return row;   
  218.     }   
  219.        
  220.     /*   
  221.      * 这个函数的作用是显示List下面的四个Button   
  222.      */   
  223.     private View addButtonView()   
  224.     {   
  225.         //流式布局放四个button   
  226.         LinearLayout layout = new LinearLayout(mContext);   
  227.         layout.setOrientation(LinearLayout.HORIZONTAL);   
  228.         layout.setPadding(121121);   
  229.         layout.setBackgroundColor(android.R.color.darker_gray);   
  230.         layout.setGravity(Gravity.CENTER);   
  231.            
  232.         final Button bt1 = new Button(mContext);   
  233.         bt1.setText("转发");   
  234.         bt1.setFocusable(false);   
  235.         bt1.setFocusableInTouchMode(false);   
  236.         bt1.setClickable(true);   
  237.         bt1.setOnClickListener(new OnClickListener() {   
  238.                
  239.             @Override   
  240.             public void onClick(View v) {   
  241.                 // TODO Auto-generated method stub   
  242.                 System.out.println("wbTransmit");   
  243.             }   
  244.         });   
  245.         layout.addView(bt1,   
  246.                 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));   
  247.            
  248.         final Button bt2 = new Button(mContext);   
  249.         bt2.setText("评论");   
  250.         bt2.setFocusable(false);   
  251.         bt2.setFocusableInTouchMode(false);   
  252.         bt2.setClickable(true);   
  253.         bt2.setOnClickListener(new OnClickListener() {   
  254.                
  255.             @Override   
  256.             public void onClick(View v) {   
  257.                 // TODO Auto-generated method stub   
  258.                 System.out.println("wbComment");   
  259.             }   
  260.         });   
  261.         layout.addView(bt2,   
  262.                 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));   
  263.            
  264.         final Button bt3 = new Button(mContext);   
  265.         bt3.setText("看评论");   
  266.         bt3.setFocusable(false);   
  267.         bt3.setFocusableInTouchMode(false);   
  268.         bt3.setClickable(true);   
  269.         bt3.setOnClickListener(new OnClickListener() {   
  270.                
  271.             @Override   
  272.             public void onClick(View v) {   
  273.                 // TODO Auto-generated method stub   
  274.                 System.out.println("wbWatchComment");   
  275.             }   
  276.         });   
  277.         layout.addView(bt3,   
  278.                 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));   
  279.            
  280.            
  281.         final Button bt4 = new Button(mContext);   
  282.         bt4.setText("收起");   
  283.         bt4.setFocusable(false);   
  284.         bt4.setFocusableInTouchMode(false);   
  285.         bt4.setClickable(true);   
  286.         bt4.setOnClickListener(new OnClickListener() {   
  287.             @Override   
  288.             public void onClick(View v) {   
  289.                 // TODO Auto-generated method stub   
  290.                 System.out.println("wbRetract");   
  291.                 bt1.setVisibility(View.GONE);   
  292.                 bt2.setVisibility(View.GONE);   
  293.                 bt3.setVisibility(View.GONE);   
  294.                 bt4.setVisibility(View.GONE);   
  295.             }   
  296.         });   
  297.         layout.addView(bt4,   
  298.                 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));   
  299.            
  300.         return layout;   
  301.     }   
  302.   
  303. }  

 

其中关键的地方是在getView里面,这里我只说几点:

1。addListView(position, row)函数的作用是绘制整个list(没有button),这个是在第一次显示的时候去调用

2。addButtonView()函数的作用是添加点击list之后显示button重写绘制的list

3。图片是从网上获取需要异步加载

4。整个思想是点击list后重写加载list,当然这不是最好的办法,为了效率还需要改进。

MainActivity.java

 

Mainactivity代码 复制代码  收藏代码
  1. import java.util.ArrayList;   
  2. import java.util.HashMap;   
  3. import java.util.List;   
  4. import java.util.Map;   
  5.   
  6. import android.app.ListActivity;   
  7. import android.graphics.Bitmap;   
  8. import android.os.Bundle;   
  9. import android.util.Log;   
  10. import android.view.View;   
  11. import android.view.Window;   
  12. import android.widget.AdapterView;   
  13. import android.widget.Button;   
  14. import android.widget.ImageView;   
  15. import android.widget.ListView;   
  16. import android.widget.TextView;   
  17. import android.widget.AdapterView.OnItemLongClickListener;   
  18.   
  19. import com.woclub.beans.WeiboHolder;   
  20. import com.woclub.beans.WeiboInfo;   
  21. import com.woclub.utils.HttpDownloader;   
  22. import com.woclub.utils.MyBaseAdapter;   
  23. import com.woclub.utils.MySimpleAdapter;   
  24.   
  25. public class MainActivity extends ListActivity {   
  26.        
  27.     private MyBaseAdapter adapter = null;   
  28.     /** Called when the activity is first created. */   
  29.     @Override   
  30.     public void onCreate(Bundle savedInstanceState) {   
  31.         super.onCreate(savedInstanceState);   
  32.         //requestWindowFeature 要在setContentView之前    
  33.         //getWindow().setFeatureInit最好在setContentView之后    
  34.         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);// 注意顺序   
  35.         setContentView(R.layout.main);   
  36.         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);   
  37.            
  38.         adapter = new MyBaseAdapter(this, setWeiboList());   
  39.         setListAdapter(adapter);   
  40.            
  41.            
  42.         //setAdapter(setList());   
  43.     }   
  44.     
  45.     public List<WeiboInfo> setWeiboList()   
  46.     {   
  47.         List<WeiboInfo> list = new ArrayList<WeiboInfo>();   
  48.         WeiboInfo infos = new WeiboInfo();   
  49.         infos.setId("0");   
  50.         infos.setUserId("0");   
  51.         infos.setUserName("弦后");   
  52.         infos.setTime("3小时前");   
  53.         infos.setText("#sina玩玩#昨天刚在@宋丹丹: 微博上看到黄老师的餐厅故事,今天晚餐就巧遇黄老师,不过餐桌上准备好纸巾了http://www.baidu.com");   
  54.         infos.setCommentText("3.參與活動的春采需要統一聽從負責人的安排,不得影響阿嫂及其助理正常工作。轉發@應采儿:官方粉絲團 春春yu動家族");   
  55.         infos.setUserIcon("http://192.168.1.102:8081/image/1.jpg");   
  56.         infos.setImage("http://192.168.1.102:8081/image/2.jpg");   
  57.         list.add(infos);   
  58.            
  59.         WeiboInfo infos2 = new WeiboInfo();   
  60.         infos2.setId("1");   
  61.         infos2.setUserId("1");   
  62.         infos2.setUserName("弦后");   
  63.         infos2.setTime("1小时前");   
  64.         infos2.setText("#sina玩玩#昨天刚在@宋丹丹:微博上看到黄老师的餐厅故事,今天晚餐就巧遇黄老师,不过餐桌上准备好纸巾了");   
  65.         infos2.setCommentText("@應采儿:參與活動的春采需要統一聽從負責人的安排,不得影響阿嫂及其助理正常工作。轉發@應采儿: 官方粉絲團 春春yu動家族");   
  66.         infos2.setUserIcon("http://192.168.1.102:8081/image/2.jpg");   
  67.         infos2.setImage("http://192.168.1.102:8081/image/2.jpg");   
  68.         list.add(infos2);   
  69.            
  70.         WeiboInfo infos3 = new WeiboInfo();   
  71.         infos3.setId("2");   
  72.         infos3.setUserId("2");   
  73.         infos3.setUserName("弦后");   
  74.         infos3.setTime("2小时前");   
  75.         infos3.setText("#sina玩玩#昨天刚在@宋丹丹:微博上看到黄老师的餐厅故事,今天晚餐就巧遇黄老师,不过餐桌上准备好纸巾了");   
  76.         infos3.setCommentText("@應采儿:參與活動的春采需要統一聽從負責人的安排,不得影響阿嫂及其助理正常工作。轉發@應采儿: 官方粉絲團 春春yu動家族");   
  77.         infos3.setUserIcon("http://192.168.1.102:8081/image/2.jpg");   
  78.         infos3.setImage("http://192.168.1.102:8081/image/2.jpg");   
  79.         list.add(infos3);   
  80.         return list;   
  81.     }   
  82.   
  83.     @Override   
  84.     protected void onListItemClick(ListView l, View v, int position, long id) {   
  85.         // TODO Auto-generated method stub   
  86.         adapter.setSelectListItem(position);   
  87.         adapter.notifyDataSetChanged();   
  88.         super.onListItemClick(l, v, position, id);   
  89.     }   
  90. }  

 主函数没什么难的,很简单,因为都在Adapter实现了。

下面是MVC中所说的:M

WeiboHolder.java

Weiboholder代码 复制代码  收藏代码
  1. package com.woclub.beans;   
  2.   
  3. import android.widget.Button;   
  4. import android.widget.ImageView;   
  5. import android.widget.TextView;   
  6.   
  7. public class WeiboHolder {   
  8.     public ImageView wbImage;//微博中的图片   
  9.     public ImageView wbIcon;//发布人头像   
  10.     public TextView wbUser;   
  11.     public TextView wbTime;   
  12.     public TextView wbText;   
  13.     public TextView wbCommentText;   
  14.        
  15. }  

WeiboInfo.java

Weiboinfo代码
  1. public class WeiboInfo {   
  2.     private String id;//文章id   
  3.     private String userId;//发布人id   
  4.     private String userName;//发布人名字   
  5.     private String userIcon;//发布人头像   
  6.     private String time;//发布时间   
  7.     private String haveImage;//有图片   
  8.     private String text;//文章内容   
  9.     private String commentText;//评论内容   
  10.        
  11.     public static final String ID = "id";   
  12.     public static final String USERID = "userId";   
  13.     public static final String USERNAME = "userName";   
  14.     public static final String USERICON = "userIcon";   
  15.     public static final String TIME = "time";   
  16.     public static final String TEXT = "text";   
  17.     public static final String COMMENTTEXT = "commentText";   
  18.     public static final String HAVEIMAGE = "haveImage";   
  19.        
  20.     public String getId() {   
  21.         return id;   
  22.     }   
  23.     public void setId(String id) {   
  24.         this.id = id;   
  25.     }   
  26.     public String getUserId() {   
  27.         return userId;   
  28.     }   
  29.     public void setUserId(String userId) {   
  30.         this.userId = userId;   
  31.     }   
  32.     public String getUserName() {   
  33.         return userName;   
  34.     }   
  35.     public void setUserName(String userName) {   
  36.         this.userName = userName;   
  37.     }   
  38.     public String getUserIcon() {   
  39.         return userIcon;   
  40.     }   
  41.     public void setUserIcon(String userIcon) {   
  42.         this.userIcon = userIcon;   
  43.     }   
  44.     public String getTime() {   
  45.         return time;   
  46.     }   
  47.     public void setTime(String time) {   
  48.         this.time = time;   
  49.     }   
  50.     public String getImage() {   
  51.         return haveImage;   
  52.     }   
  53.     public void setImage(String haveImage) {   
  54.         this.haveImage = haveImage;   
  55.     }   
  56.     public String getText() {   
  57.         return text;   
  58.     }   
  59.     public void setText(String text) {   
  60.         this.text = text;   
  61.     }   
  62.     public String getCommentText() {   
  63.         return commentText;   
  64.     }   
  65.     public void setCommentText(String commentText) {   
  66.         this.commentText = commentText;   
  67.     }   
  68.        
  69.        
  70. }

转自:http://hao3100590.iteye.com/blog/1005422

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值