关于android的listview的数据解析和性能优化问题

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的postion
		return 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上碰到的问题了,希望可以解决大家的困扰



 

         




你可能感兴趣的:(Android开发)