Android - Expandable ListView - using ViewHolder for optimization

后端 未结 1 1679
天涯浪人
天涯浪人 2020-12-11 05:55

I have a ListView where every row is an image+text. I have implemented this list as a seperate list using a ViewHolder to optimize scrolling. Now I\'m trying to use the same

相关标签:
1条回答
  • 2020-12-11 06:04

    You're going to want to use 2 different view holders. First, let's define a ViewHolder class.

    public class ViewHolder
    {
        private HashMap<Integer, View> storedViews = new HashMap<Integer, View>();
    
        public ViewHolder()
        {
        }
    
        /**
         * 
         * @param view
         *            The view to add; to reference this view later, simply refer to its id.
         * @return This instance to allow for chaining.
         */
        public ViewHolder addView(View view)
        {
            int id = view.getId();
            storedViews.put(id, view);
            return this;
        }
    
        public View getView(int id)
        {
            return storedViews.get(id);
        }
    }
    

    We want to use the ViewHolder in the getGroupView method and in the getChildView method.

        @Override
        public View getChildView(int groupPosition, final int position,
               boolean isLastChild, View convertView, ViewGroup parent)
        {
            View row = convertView;
            if (row == null)
            {
                row = inflater.inflate(R.layout.my_layout_for_row, parent, false);
                View myView = row.findViewById(R.id.myView);
                ViewHolder holder = new ViewHolder();
                holder.addView(myView);
                row.setTag(holder);
            }
    
            // Get the stored ViewHolder that also contains our views
            ViewHolder holder = (ViewHolder) row.getTag();
            View myView = holder.getView(R.id.myView);
            return row;
        }
    

    We do the same thing for the groupView method.

    @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
        {
            View v = convertView;
            if (v == null)
            {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.my_layout, parent, false);
                ViewHolder holder = new ViewHolder();
                holder.addView(v.findViewById(R.id.myView));
                v.setTag(holder);
            }
    
            ViewHolder holder = (ViewHolder) v.getTag();
            // Do whatever you need to with the group view
            return v;
        }
    

    EDIT: Perhaps a little bit of explanation is warranted for how ViewHolder actually works.

    The code inside the if(row == null) is only for initializing the views for the first time. This view gets passed around in the convertView field every time a new row wants to know what to what layout to use. We set the ViewHolder on this view that gets passed around so we don't have to call findViewById every time or inflate the layout, both of which are relatively expensive operations. So, when the convert view isn't null, it doesn't go in that if-statement and all we care about is setting custom values for the views contained inside the ViewHolder.

    0 讨论(0)
提交回复
热议问题