ListView的适配器与行类型的任意数目(不知道不同的行类型的数量)类型、适配器、数目、数量

2023-09-12 08:15:07 作者:一直都是笑话

所以,我提出这个申请。应用程序解析网站,或更具体地一个vBulletin板。当我分析在论坛里线程,我分吧,这样,当我分析在该线程每个岗位,我得到的职位,如该部分的实际内容,而我储存的部分以正确的顺序数组中的:

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]

然而,后可被以任意顺序排列,你可能知道,可包括比在实施例更多或更少的部分,并且它不必具有在它引号或者,或它可能只是1或几个报价。一切皆有可能。

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。这个列表视图的每一行随后将总是的由一个头,而$ P $的任何组合pviously提到的部分。

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.

就是我在想Google上搜寻了一​​下关于它之后做的是有一个基本布局只是一个布局标签在一个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查看或者,因为它可能有一堆添加到它部分,我可能不需要在另一个调用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.

该基地布局仅仅是一个的LinearLayout标签

The base-layout is just a LinearLayout-tag

我膨胀的布局只是RelativeLayouts一些TextViews和ImageView的增加。

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

这code会产生这样的结果,我在那里有一个的头的带 用户名,图片等..,的的纯文本的,还有一个的报价单节的一个部分。 这似乎给所有的时间正常工作虽然没有,因为当我尝试过了刚才列表的副本似乎卡住的背景和另外一个滚动在它的上面。

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..

https://m.xsw88.com/allimgs/daicuo/20230912/6059.png

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

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

推荐答案

您需要重写 getViewItemType getViewTypeCount

getItemViewType(INT位置) - 返回其布局类型,您应该使用基于位置信息

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持有人的平滑滚动和性能。

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