android的listview有很多适配器:

我初学android的时候用过SimpleAdapter,ListAdapter,两种适配器,但是感觉还是要自己写个BaseApater好,

关于BaseApdater,我们先给出一个继承的示例代码:

        public int getCount() {//返回列表的数目return this.m_load_data_list.size(); }public Object getItem(int position) {//返回列表数据解析的容器中一个选中对象return this.m_load_data_list.get(position);}public long getItemId(int pos) {//返回选中的Item的postionreturn pos;}

方法都对应了释,一定要写好,不然的话,肯定会崩溃掉的。

接下来我们详细的说下getView这个方法:

 public View getView(int position, View contentView, ViewGroup parent) {  this.one_comment_message=(CommentMessage)this.m_load_data_list.get(position);  ViewHolder viewHolder=null;  if(contentView==null)  {     contentView=LayoutInflater.from(mContext).inflate(R.layout.comments_item, null);          viewHolder=new ViewHolder();     viewHolder.comment_article=(TextView)contentView.findViewById(R.id.leave_word_article);     viewHolder.comment_name=(TextView)contentView.findViewById(R.id.leave_word_user_name);     viewHolder.comment_time=(TextView)contentView.findViewById(R.id.leave_word_time);          viewHolder.comment_article.setText(this.one_comment_message.getComment_aricle());     //viewHolder.comment_name.setText(this.one_comment_message.getComment_name());     viewHolder.comment_time.setText(this.one_comment_message.getComment_time());     this.replace_me_in_comments(viewHolder,      this.one_comment_message.getComment_name(),      this.one_comment_message.get_Com_user_id()+"");          contentView.setTag(viewHolder);  }else{// use graphics buf     viewHolder=(ViewHolder)contentView.getTag();          viewHolder.comment_article.setText(this.one_comment_message.getComment_aricle());     //viewHolder.comment_name.setText(this.one_comment_message.getComment_name());     viewHolder.comment_time.setText(this.one_comment_message.getComment_time());     this.replace_me_in_comments(viewHolder,      this.one_comment_message.getComment_name(),      this.one_comment_message.get_Com_user_id()+"");  }  return contentView;   } private class ViewHolder{   TextView comment_name;   TextView comment_article;   TextView comment_time; }  private void  replace_me_in_comments(ViewHolder viewHolder,String com_name_original,String com_user_id) { if(com_name_original.equals(DetaiedMessageActivity.m_nickname)     &&com_user_id.equals(DetaiedMessageActivity.m_user_id))//防止两个用户取名字相同,{ viewHolder.comment_name.setText("我 :");}else{viewHolder.comment_name.setText(com_name_original);} }

网上关于getView的方法很详细的解释有这个连接:http://blog.csdn.net/tianshuguang/article/details/7344315

然后我们对上述代码进行一个讲解:

上述代码是ListView最高效的一种办法了:

为什么?

1.这种方法省去了重复的findViewById()(用Viewholder和contentView的settag和gettag),为重复的方法布局。

2.cotentIVew缓存,这种机制是极大的提高的了内存的使用效率,contentView==null,和contentVIiew!=null到底是指什么情况呢?

contentView其实是指手机屏幕上显示的item,一个listview加载的item项目可能是一个屏幕放不下的啊,一般初始化的listview都是contentView=null的时候,

然后,当我们用手去滑动屏幕,向下查看更多选项的时候:

此时,如果没有什么特使的加载(比如不同的位置加载不同的布局的时候)contentView!=null,那么我们将contentView作为缓存,重新进行数据加载

具体的缓存图片我简单的描述下:




长条的表示手机屏幕的最上边。

所以,我们可以清楚的了解ListVIew是如何工作的吧,其实,当我们每次滑动listview的时候,调用Basedapter的getview的方法,所以,如果我们不写contentView!=null,那么内存将会极大的泄漏。所以,listview建议都用BaseAdapter来加载吧。


2.然后来说下关于listview的一些点击高亮问题:

一般,我们都可以设置listView加载的layout的点击高亮:

                  <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:orientation="vertical"                    android:background="@drawable/item_layout_state"                    >
<selector xmlns:android="http://schemas.android.com/apk/res/android">      <item android:state_pressed="false">            <shape>                 <gradient                     android:startColor="#7fffffff"                     android:endColor="#7fffffff"                     android:angle="90"/>            </shape>      </item>      <item android:state_pressed="true"           >            <shape>                  <gradient                       android:startColor="#afffffff"                      android:endColor="#afffffff"                      android:angle="90"/>            </shape>      </item></selector>
这里也就说下Layout的点击高亮,一般都是这种,点击换颜色,

但是请注意:

ListView 组件的首个线性布局不要用clickable属性,否则会写死ListView的OnitemClick的方法

3.关于ListView加入button无法点击的办法:

android:descendantFocusability="blocksDescendants"

<Button>中

android:focusable="false"

具体方法见:http://blog.csdn.net/gyflyx/article/details/6567701

这些就是我在项目关于ListVIew上碰到的问题了,希望可以解决大家的困扰







更多相关文章

  1. Android Activity之间传递图片(Bitmap)的方法
  2. 【android】ORMLite框架 的使用方法---给你的数据库操作插上翅膀
  3. Android错误解决方法大集合
  4. Android下拉刷新和上拉加载更多
  5. Android中WebView获取网页中标题 ,内容, 图片的方法
  6. Android获取IPV4的方法
  7. 【安卓】Android播放器的三种实现方法
  8. Android系统手机端抓包方法
  9. 实现Android ListView 自动加载更多内容

随机推荐

  1. Android(安卓)-- RecyclerView实现顶部吸
  2. Android(安卓)-- 小功能 如何处理未捕获
  3. Dialog对话框(此5种)
  4. Android(安卓)面试题之系统相关
  5. Ubuntu下Genymotion模拟器启动卡死
  6. Android(安卓)Material Design之CardView
  7. listview Recycleview中imageview图片显
  8. Android十八章:Android(安卓)Studio打包ja
  9. Android(安卓)通过高德地图获取地址的经
  10. Android(安卓)Studio导入第三方类库、jar