ViewHolder pattern correctly implemented in custom CursorAdapter?

后端 未结 3 1422
既然无缘
既然无缘 2020-12-05 00:15

Here is my custom CursorAdapter:

public class TasksAdapter extends CursorAdapter implements Filterable {

    private final Context context;

    public Task         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-05 00:52

    If you are overriding newView() and bindView(), you don't need to do anything extra in getView(). CursorAdapter has an implementation of getView() that delegates to newView() and bindView() to enforce the row recycling.

    findViewById() maybe called frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. To avoid this, ViewHolder pattern is useful.

    Here's an example of ViewHolder pattern implemented for a weather app:

    public class ForecastAdapter extends CursorAdapter {
    
        public ForecastAdapter(Context context, Cursor cursor, int flags) {
            super(context, cursor, flags);
        }
    
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View view = LayoutInflater.from(context).inflate(
                    R.layout.list_item_forecast, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
            return view;
        }
    
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            ViewHolder viewHolder = (ViewHolder) view.getTag();
    
            long date = cursor.getLong(ForecastFragment.COL_WEATHER_DATE);
            viewHolder.dateView.setText("Today");
    
            String weatherForecast =
                    cursor.getString(ForecastFragment.COL_WEATHER_DESC);
            viewHolder.descriptionView.setText(weatherForecast);
    
            double high = cursor.getFloat(ForecastFragment.COL_WEATHER_MAX_TEMP);
            viewHolder.highTempView.setText("30");
    
            double low = cursor.getFloat(ForecastFragment.COL_WEATHER_MIN_TEMP);
            viewHolder.lowTempView.setText("24");
    
            int weatherConditionId =
                    cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID);
            viewHolder.iconView.setImageResource(R.drawable.ic_snow);
        }
    
        /** Cache of the children views for a list item. */
        public static class ViewHolder {
            public final ImageView iconView;
            public final TextView dateView;
            public final TextView descriptionView;
            public final TextView highTempView;
            public final TextView lowTempView;
    
            public ViewHolder(View view) {
                iconView =
                        (ImageView) view.findViewById(R.id.item_icon);
                dateView =
                        (TextView) view.findViewById(R.id.item_date_textview);
                descriptionView =
                        (TextView) view.findViewById(R.id.item_forecast_textview);
                highTempView =
                        (TextView) view.findViewById(R.id.item_high_textview);
                lowTempView =
                        (TextView) view.findViewById(R.id.item_low_textview);
            }
        }
    }
    

提交回复
热议问题