简体   繁体   中英

Inflate two layouts in getView() to implement ListView Section header

I'm trying to create section headers for my ListView in a way that some list rows have headers and some do not. So what i have is three xml layout files. fragment_listview.xml contains the <ListView>

row_listview.xml contains the content for ListView rows

header_listview.xml is for the section headers.

Feeds.java is my model class.

So in my adapter i have something like this:

public class FeedsArrayAdapter extends ArrayAdapter<Feeds> {

private Context context;
private List<Feeds> feeds;
private LayoutInflater vi;

FeedsArrayAdapter(Context c, List<Feed> ff){ //initializes all the class member fields }

public void getView(int position, View convertView, ViewGroup parent){

    Feeds f = feeds.get(position);
    if(f.headerNeeded()){

       convertView = vi.inflate(R.layout.header_listview, null);
       TextView textViewHeader = (TextView) convertView.findViewById(R.id.feed_header);

       textViewHeader.setText(new SimpleDateFormat("dd/MM/yyyy").format(feed.date));
   }
   else {
        convertView = vi.inflate(R.layout.row_listview, null);

        TextView textViewfeedHeading = (TextView) convertView.findViewById(R.id.feed_heading);
        TextView textViewfeedSubheading = (TextView) convertView.findViewById(R.id.feed_subheading);
        TextView textViewfeedDate = (TextView) convertView.findViewById(R.id.textViewDate);
        ImageView imageViewfeedIcon = (ImageView) convertView.findViewById(R.id.feed_icon);
        ImageView imageViewfeedBanner = (ImageView) convertView.findViewById(R.id.feed_banner);

        textViewfeedHeading.setText(feed.title);
        textViewfeedSubheading.setText(feed.subtitle);
        textViewfeedDate.setText(new SimpleDateFormat("dd/MM/yyyy").format(feed.date));

    }

    return convertView;
 }

So this gives us something like this:

---header1----
---header2----
___Row 3_____
___Row 4_____

What i want is:

-----header 1------
_____Row 1_______
-----header 2------
_____Row 2_______
_____Row 3_______
_____Row 4_______

If i try to inflate row.listview in if(true) I get an exception.

Any approach how to resolve this.

您可以在此处使用粘性标题库

与BaseExpandableAdapter一起使用可扩展列表视图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM