Android的自定义列表视图 [英] Android Custom Listview

查看:90
本文介绍了Android的自定义列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了教程和搜查,但我仍然无法理解的,

  getView(INT位置,查看convertView,ViewGroup中ARG2)
 

方法的工作原理,当延伸 BaseAdapter 以创建我的Andr​​oid应用程序自定义的ListView。因此,我不能编辑自定义列表视图正是我想要的。

我需要知道什么时候这个方法调用和参数的含义。

如果有人可以解释下面的方法其巨大的。谢谢

  @覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中ARG2)
{

    ViewHolder持有人;
    LayoutInflater充气= context.getLayoutInflater();

    如果(convertView == NULL)
    {
        convertView = inflater.inflate(R.layout.listitem_row,NULL);
        持有人=新ViewHolder();
        holder.txtViewTitle =(TextView中)convertView.findViewById(R.id.textView1);
        holder.txtViewDescription =(TextView中)convertView.findViewById(R.id.textView2);

        convertView.setTag(保持器);
    }
    其他
    {
        支架=(ViewHolder)convertView.getTag();
    }

    holder.txtViewTitle.setText(标题[位置]);
    holder.txtViewDescription.setText(介绍[位置]);



  返回convertView;
}
 

解决方案

getView():正如规格getView方法显示在指定位置的数据。所以,当你setAdapter,当你滚动你的ListView getView方法将被调用。

你在这里复制的方法是 EfficientAdapter 以优化您的ListView性能的一部分,伴随着优化您使用的 ViewHolder 的模式。

从规格复制:几乎没有更多的解释

位置:该项目适配器的数据集,我们希望他们的观点的项目中的位置

convertView :老观点重用,如果可能的话。注意:您应该检查该 视图是使用前非空一个合适的类型和。如果它是不能转换该视图以显示正确的数据,该方法可以创建新的视图。异构列表可以指定自己的视图类型数量,因此,这种观点是正确的类型始终(见getViewTypeCount()和getItemViewType(INT))。

所以,当你正在做以下的事情你是上述方法重用你convertView。

 如果(convertView == NULL){
            ....
            convertView.setTag(保持器);
     } 其他 {
           支架=(ViewHolder)convertView.getTag();
      }
 

和做你避免查找(findViewById)以下的东西,这就是对好东西什么的 ViewHolder模式

  holder.txtViewTitle =(TextView中)convertView.findViewById(R.id.textView1);
 

:父,这种观点最终会被连接到

编辑

问:多少次getView被称为多少convertView将被创建? 答:让我们的例子<一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html"相对=nofollow> Efficeint适配器从ApiDemos 的。如果你的屏幕,显示10行,那么,

convertView计数:10 + 1 = 11(10行,你所看到的画面是什么,一个额外的显示滚动效果)。这意味着陈述,如果(convertView == NULL){...}块将被称为只有11次。

getView计数:最初数将是10,但是当你开始滚动数继续增加。 getView呼吁每一个更新数据的时间。

注:此为在问题中提到getView方法是唯一真正的

I went through tutorials and searched, but still I can't understand how the,

getView(int position, View convertView, ViewGroup arg2)

method works when extends BaseAdapter to create a custom listView in my android application. Therefore I cant Edit the Custom list view exactly I want.

I need to know when this method invokes and the meanings of the parameters.

If someone can explain the following method its great. Thanks

@Override
public View getView(int position, View convertView, ViewGroup arg2)
{

    ViewHolder holder;
    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.listitem_row, null);                     
        holder = new ViewHolder();
        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
        holder.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();                                        
    }

    holder.txtViewTitle.setText(title[position]);
    holder.txtViewDescription.setText(description[position]);



  return convertView;
}

解决方案

getView() : As mentioned in specs getView method displays the data at the specified position. So, when you setAdapter and when you scroll your listView getView method will be called.

The method you copied here is a part of EfficientAdapter to optimize your ListView performance and along with optimization you used ViewHolder pattern.

Copied from Specs : With little more explanation

position :The position of the item within the adapter's data set of the item whose view we want.

convertView: The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see getViewTypeCount() and getItemViewType(int)).

So, in above method when you are doing the following thing you are reusing your convertView.

     if (convertView == null){
            ....
            convertView.setTag(holder);
     } else {
           holder = (ViewHolder) convertView.getTag(); 
      }

And by doing following thing you are avoiding lookup (findViewById), thats what the good thing about ViewHolder pattern

      holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);

parent : The parent that this view will eventually be attached to

Edited

Question : How many times getView is called and how many convertView will be created ? Answer: Lets take an example of Efficeint Adapter from ApiDemos. If your screen showing 10 Rows, then,

convertView Count : 10 + 1 = 11 (10 Rows what you are seeing on screen, one extra to show scrolling effect). That means statements in if(convertView == null){...} block will be called only 11 times.

getView Count: Initially count will be 10, but when you start scrolling count keep on increasing. getView called every time to update data.

Note: This is only true for getView method mentioned in question.

这篇关于Android的自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆