Android: Implementing ViewHolder

后端 未结 3 1715
灰色年华
灰色年华 2020-12-15 21:11

I am trying to implement ViewHolder in my Android app, but I keep getting that ViewHolder cannot be resolved to a type, without any suggestions for an import. Anyone know ho

相关标签:
3条回答
  • 2020-12-15 21:26

    Maybe you are looking for the RecyclerView.ViewHolder which is part of the android support lib.

    Like the code from this link gist by Paul Burke

    public static class ItemViewHolder extends RecyclerView.ViewHolder implements 
            ItemTouchHelperViewHolder { 
    
        public final TextView textView;
        public final ImageView handleView;
    
        public ItemViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text);
            handleView = (ImageView) itemView.findViewById(R.id.handle);
        } 
    
        @Override 
        public void onItemSelected() { 
            itemView.setBackgroundColor(Color.LTGRAY);
        } 
    
        @Override 
        public void onItemClear() { 
            itemView.setBackgroundColor(0); 
        } 
    } 
    

    This would make sense for you if you were working with an Android RecyclerView

    In this case they need an object to hold the view so that it can be filled with content as needed.

    0 讨论(0)
  • 2020-12-15 21:29
    **Create a Holder class**
    
    protected static class ViewHolderItems
    {
        private ImageView mStoreImage;
        private TextView mStoreName;
    }
    
    And use In getView method of adapter
    
     public View getView(int position, View convertView, ViewGroup parent) {
    
            ViewHolderItems viewHolder;
            if (inflater == null)
                inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.coupen_row, null);
                viewHolder = new ViewHolderItems();
                viewHolder.mStoreImage = (ImageView) convertView.findViewById(R.id.storeImage);
    
                viewHolder.mStoreName = (TextView) convertView.findViewById(R.id.storeName);
    
                convertView.setTag(viewHolder);
    
            } 
           else 
            {
                viewHolder = (ViewHolderItems) convertView.getTag();
            }
    
             return convertView;
        }
    
    0 讨论(0)
  • 2020-12-15 21:41

    That's because a ViewHolder is not a class that is from the Android SDK, you make it yourself. Based on what I can find, a ViewHolder is an implementation that stores Views (per row in a ListView usually) for a larger area, so it is a sort of helper class and cache mechanism. This is one example I found on Android Developers of what a ViewHolder would contain.

    static class ViewHolder {
      TextView text;
      TextView timestamp;
      ImageView icon;
      ProgressBar progress;
      int position;
    }
    

    Then you can implement it in a ListAdapter or a similar class.

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