ListView Adapter 具有任意数量的行类型(不知道不同行类型的数量) [英] ListView Adapter with arbitrary number of row types (Don't know the number of different row types)

查看:21
本文介绍了ListView Adapter 具有任意数量的行类型(不知道不同行类型的数量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在制作这个应用程序.该应用程序解析一个网站,或者更具体地说是一个 vbulletin-board.当我解析论坛中的一个主题时,我将其进行了划分,以便在我解析该主题中的每个帖子时,我会在诸如此类的部分中获得帖子的实际内容,并以正确的顺序存储这些部分在数组中:

So, I am making this application. The application parses a website, or more specifically a vbulletin-board. When I'm parsing a thread in the forum, I have divided it up so that when I parse each post in that thread, I get the actual content of the post in sections such as this, and I store the sections in the correct order in an array:

[Plain text]
[Quote from somebody]
[Plain text]
[Another quote]
[Another quote again]
[Some more plain text]

但是,如您所知,帖子可以按任何顺序排列,并且可以包含比示例中更多或更少的部分,并且其中也不必包含引号,或者它可能只是一个或几个引号.一切皆有可能.

However, a post can be arranged in any order as you might know, and can consist of more or fewer sections than in the example, and it doesn't have to have quotes in it either, or it might just be one or several quotes. Anything is possible.

当我在我的应用程序中列出帖子时,我使用的是 ListView.这个列表视图的每一行将总是由一个标题和前面提到的部分的任意组合组成.

When I list the posts in my application, I am using a ListView. Each row of this listview will then always consist of a header, and any combination of the previously mentioned sections.

我在谷歌上搜索了一下之后想这样做的方法是在一个 XML 文件中有一个只有一个布局标签的基本布局",每个部分都有一个单独的布局,存储在单独的 XML 中-files,并在我的适配器中每次调用 getView() 时,查看我的帖子列表"中该位置的帖子,然后循环浏览该特定帖子中的部分,并增加一个新的引用布局"对于存储在帖子中的每个引用部分,并为帖子中的每个纯文本部分增加一个纯文本布局".对于每个人,我填写属于该帖子的所有内容.

The way I was thinking of doing it after googling a bit about it is to have one "Base-layout" with just a layout-tag in one XML-file, and a separate layout for each section, stored in separate XML-files, and at each call to getView() in my adapter, look at the post at that position in my "Post-list", and then loop through the sections in that particular post, and inflate a new "Quote-layout" for each quote-section stored in the post, and inflate a "Plain-text-layout" for each plain-text-section in the post. And for each of those I fill in all the content belonging to that post.

我认为这可行,但可能存在性能问题?据我所知,布局膨胀非常昂贵,而且我也无法回收传递给 getView() 的 View,因为它可能添加了一堆部分,而我在另一个 getView 调用中可能不需要这些部分()..也就是说,如果我对 getView() 和回收有所了解.

I think this would work, but there might be a performance problem? As I understand it layout inflation is quite expensive, and I won't be able to recycle the View passed in to getView() either, since it might have a bunch of sections added to it that I might not need in another call to getView().. That is, if I understand getView() and the recycling somewhat.

这是我对适配器的 getView() 方法的意思的一个基本示例:

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    // Inflate the base-layout, which the others are added to.
    view = mInflater.inflate(R.layout.forum_post,null);

    View header = mInflater.inflate(R.layout.post_header_layout, null);
    View message = mInflater.inflate(R.layout.post_text_layout, null);
    View quote = mInflater.inflate(R.layout.post_quote_layout, null);

    ((ViewGroup)view).addView(header);
    ((ViewGroup)view).addView(message);
    ((ViewGroup)view).addView(quote);
    return view;
}

然后在我从保存的帖子列表中提取数据时根据需要增加更多引用视图/消息视图.

And then inflate more quote-views/message-views as needed when I extract the data from my list of saved posts.

base-layout 只是一个 LinearLayout-tag

The base-layout is just a LinearLayout-tag

我膨胀的布局只是添加了一些 TextView 和 ImageView 的相对布局.

The layouts I inflate are just RelativeLayouts with some TextViews and an ImageView added.

此代码产生此结果,其中我有一个 Header用户名、图片等,一段纯文本,一段引用段.不过,这似乎并不能一直正常工作,因为当我刚刚尝试它时,列表的副本似乎卡在了背景上,而另一个则滚动到了它之上..

This code produces this result, where I have a Header with username, picture, etc.., One section of Plain text, and one Quote-section. This doesn't seem to work properly all the time though, because when I tried it out just now a copy of the list seemed to get stuck on the background and another one scrolled on top of it..

http://s14.postimg.org/rizid8q69/view.png

有没有更好的方法来做到这一点?因为我认为这不是很有效

Is there a better way to do this? Because I imagine this isn't very efficient

推荐答案

您需要覆盖 getViewItemTypegetViewTypeCount.

getItemViewType(int position) - 根据位置返回您应该使用哪种布局类型的信息

getItemViewType(int position) - returns information which layout type you should use based on position

然后仅当布局为空时才膨胀布局并使用 getItemViewType 确定类型.

Then you inflate layout only if it's null and determine type using getItemViewType.

示例:

   private static final int TYPE_ITEM1 = 0;
   private static final int TYPE_ITEM2 = 1;
   private static final int TYPE_ITEM3 = 2;
    @Override; 
    public int getItemViewType(int position) 
    {
    int type;
    if (position== 0){ // your condition
        type = TYPE_ITEM1;  //type 0 for header
    } else if(position == 1){
        type = TYPE_ITEM2; //type 1 for message
    }else {
        type = TYPE_ITEM3; //type 2 for Quote
    }
    return type;
    }
@Override
public int getViewTypeCount() {
    return 3;    //three different layouts to be inflated
}

getView

 int type= getItemViewType(i); // determine type using position.
 switch (type) {
 case TYPE_ITEM1: 
    view= mInflater.inflate(R.layout.post_header_layout, null);   // inflate layout for header
 break;
 case TYPE_ITEM2:
     view = mInflater.inflate(R.layout.post_text_layout, null); //   inflate layout for quote
 break;
 case TYPE_ITEM3:
      quote = mInflater.inflate(R.layout.post_quote_layout, null);  //   inflate layout for message
 break;    
 .... 

您需要使用 View Holder 来实现平滑滚动和性能.

You need to use a View Holder for smooth scrolling and performance.

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

您可以查看下面的教程

http://android.amberfog.com/?p=296

这篇关于ListView Adapter 具有任意数量的行类型(不知道不同行类型的数量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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