ListView Adapter with an arbitrary number of row types (I don’t know how many different row types)

So, I am making this application. The application analyzes the website, or more specifically, the vbulletin-board. When I parse a thread in a forum, I split it so that when I parse every post in that thread, I get the actual contents of the message in sections like this, and I store the sections in the correct order in the array:

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

However, the message can be organized in any order, as you probably know, and can consist of more or fewer sections than in the example, and it also does not need to have quotation marks, or it can be just or a few quotation marks. Everything is possible.

When I list messages in my application, I use ListView. Each line of this list will always consist of a heading and any combination of the previously mentioned sections.

The way I thought about doing this after I worked on it a bit was to have one “basic layout” with only a layout in one XML file and a separate layout for each section, stored in a separate XML file and each call to getView () in my adapter, look at the message at this position in my "Post-list", and then scroll through the sections in this particular message and inflate the new "Quote-layout", for each section of the quote stored in the message, and inflate "Plain-text-layout" for each text section in the message. And for each of them, I fill out all the content that belongs to this post.

, , ? , , View, getView(), , getView().. , getView() .

, 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;
}

// , .

- LinearLayout

, , - RelativeLayouts TextViews ImageView.

, , .., Quote. , , , , , , , .

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

? , .

+1
3

getViewItemType getViewTypeCount.

getItemViewType(int position) - ,

, null 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;    
 .... 

.

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

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

+5

, convertView, . , View.

-, - ViewHolder View s. ViewHolder , id, .

ViewHolder View.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    ViewHolder viewHolder;

    // if possible reuse view
    if (convertView == null) {
        final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(resource, parent, false);
        viewHolder = new ViewHolder(mInflater.inflate(R.layout.post_header_layout, null));
        view.setTag(viewHolder);
    } else {
        // reuse view
        view = convertView;
        viewHolder = (ViewHolder) view.getTag();
    }

    //set text, listeners, icon, etc.

    return view;
}

ViewHolder - , .

ViewHolder {

    private final View view;

    private ViewHolder(View view) {
        this.view = view;
    }
}

ListView Google IO 2010.

0

ViewGroup futur, :

view = mInflater.inflate(R.layout.forum_post,null);

:

view = mInflater.inflate(R.layout.forum_post,viewGroup,false);

The same goes for the other inflate: use the real parent ( viewin this case) or another group of views that is of the same type as the parent (futur); otherwise, the LayoutParameters will not be set to the correct type, and the values ​​that you specified in your XML code will be lost (never used).

0
source

Source: https://habr.com/ru/post/1599045/


All Articles