how to resolve “Layout has more than 80 views, bad for performance”?

最后都变了- 提交于 2019-12-05 16:56:13

This definitely looks like a good candidate for a ListView. As I stated in my comment, you can use a custom layout in your Adapter. I don't know how you are getting your data for it so I can't say exactly how to check but in each round of getView() you can check the data and decide which Views to show/hide.

Simply create the layout with the TextViews, etc... that each row might need, looks like probably inside of a vertical LinearLayout with horizontal LinearLayouts inside or just a RelativeLayout then change visibility to visible/gone depending on what you need.

If you are unfamiliar with doing this then you can see this tutorial on doing it.

Adapter Docs

ListView Docs

As pointed out in a comment, you could inflate different layouts in your getView() method of your Adapter if you have very different layouts which may make toggling visibility a bad approach. This just depends on your data and layouts.

Wow this is bad, use a ListView instead of a TableView and dynamically add rows based on what section you are in.

I would extend BaseAdapter to achieve this

you must use a MergeAdapter where you can merge differents adapter to only one. For example titles and values. I got the code from StackOverflow but i can't find the topic so i paste here the code that i found.

package com.mydocum.adapters;

import java.util.ArrayList;

import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

/**
* Adapter that merges multiple child adapters and views into a single
* contiguous whole.
* 
* Adapters used as pieces within MergeAdapter must have view type IDs
* monotonically increasing from 0. Ideally, adapters also have distinct ranges
* for their row ids, as returned by getItemId().
* 
*/
public class MergeAdapter extends BaseAdapter implements SectionIndexer {
    protected ArrayList<ListAdapter> pieces = new ArrayList<ListAdapter>();
    protected String noItemsText;

    /**
    * Stock constructor, simply chaining to the superclass.
    */
    public MergeAdapter() {
        super();
    }

    /**
    * Adds a new adapter to the roster of things to appear in the aggregate
    * list.
    * 
    * @param adapter
    *            Source for row views for this section
    */
    public void addAdapter(ListAdapter adapter) {
        pieces.add(adapter);
        adapter.registerDataSetObserver(new CascadeDataSetObserver());
    }

    /**
    * Get the data item associated with the specified position in the data set.
    * 
    * @param position
    *            Position of the item whose data we want
    */
    public Object getItem(int position) {
        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                return (piece.getItem(position));
            }

            position -= size;
        }

        return (null);
    }

    public void setNoItemsText(String text){
        noItemsText = text;
    }

    /**
    * Get the adapter associated with the specified position in the data set.
    * 
    * @param position
    *            Position of the item whose adapter we want
    */
    public ListAdapter getAdapter(int position) {
        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                return (piece);
            }

            position -= size;
        }

        return (null);
    }

    /**
    * How many items are in the data set represented by this Adapter.
    */
    public int getCount() { 
        int total = 0;

        for (ListAdapter piece : pieces) {
            total += piece.getCount();
        }

        if(total == 0 && noItemsText != null){
            total = 1;
        }

        return (total);
    }

    /**
    * Returns the number of types of Views that will be created by getView().
    */
    @Override
    public int getViewTypeCount() {
        int total = 0;

        for (ListAdapter piece : pieces) {
            total += piece.getViewTypeCount();
        }

        return (Math.max(total, 1)); // needed for setListAdapter() before
                                        // content add'
    }

    /**
    * Get the type of View that will be created by getView() for the specified
    * item.
    * 
    * @param position
    *            Position of the item whose data we want
    */
    @Override
    public int getItemViewType(int position) {
        int typeOffset = 0;
        int result = -1;

        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                result = typeOffset + piece.getItemViewType(position);
                break;
            }

            position -= size;
            typeOffset += piece.getViewTypeCount();
        }

        return (result);
    }

    /**
    * Are all items in this ListAdapter enabled? If yes it means all items are
    * selectable and clickable.
    */
    @Override
    public boolean areAllItemsEnabled() {
        return (false);
    }

    /**
    * Returns true if the item at the specified position is not a separator.
    * 
    * @param position
    *            Position of the item whose data we want
    */
    @Override
    public boolean isEnabled(int position) {
        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                return (piece.isEnabled(position));
            }

            position -= size;
        }

        return (false);
    }

    /**
    * Get a View that displays the data at the specified position in the data
    * set.
    * 
    * @param position
    *            Position of the item whose data we want
    * @param convertView
    *            View to recycle, if not null
    * @param parent
    *            ViewGroup containing the returned View
    */
    public View getView(int position, View convertView, ViewGroup parent) {
        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {

                return (piece.getView(position, convertView, parent));
            }

            position -= size;
        }

        if(noItemsText != null){
            TextView text = new TextView(parent.getContext());
            text.setText(noItemsText);
            return text;
        }

        return (null);
    }

    /**
    * Get the row id associated with the specified position in the list.
    * 
    * @param position
    *            Position of the item whose data we want
    */
    public long getItemId(int position) {
        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                return (piece.getItemId(position));
            }

            position -= size;
        }

        return (-1);
    }

    public int getPositionForSection(int section) {
        int position = 0;

        for (ListAdapter piece : pieces) {
            if (piece instanceof SectionIndexer) {
                Object[] sections = ((SectionIndexer) piece).getSections();
                int numSections = 0;

                if (sections != null) {
                    numSections = sections.length;
                }

                if (section < numSections) {
                    return (position + ((SectionIndexer) piece)
                            .getPositionForSection(section));
                } else if (sections != null) {
                    section -= numSections;
                }
            }

            position += piece.getCount();
        }

        return (0);
    }

    public int getSectionForPosition(int position) {
        int section = 0;

        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                if (piece instanceof SectionIndexer) {
                    return (section + ((SectionIndexer) piece)
                            .getSectionForPosition(position));
                }

                return (0);
            } else {
                if (piece instanceof SectionIndexer) {
                    Object[] sections = ((SectionIndexer) piece).getSections();

                    if (sections != null) {
                        section += sections.length;
                    }
                }
            }

            position -= size;
        }

        return (0);
    }

    public Object[] getSections() {
        ArrayList<Object> sections = new ArrayList<Object>();

        for (ListAdapter piece : pieces) {
            if (piece instanceof SectionIndexer) {
                Object[] curSections = ((SectionIndexer) piece).getSections();

                if (curSections != null) {
                    for (Object section : curSections) {
                        sections.add(section);
                    }
                }
            }
        }

        if (sections.size() == 0) {
            return (null);
        }

        return (sections.toArray(new Object[0]));
    }

    private class CascadeDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            notifyDataSetChanged();
        }

        @Override
        public void onInvalidated() {
            notifyDataSetInvalidated();
        }
    }
}

Here how you need to use it:

    MergeAdapter adapter = new MergeAdapter();
AdapterA adapterA = new AdapterA(...);
AdapterB adapterB = new AdapterB(...);

adapter.addAdapter(adapterA);
adapter.addAdapter(adapterB);

listview.setAdapter(adapter);

In the listview you will have the adapterA and andapterB one after other and shower correcly. The two adapter can have diffente object.

Bye.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!