Android开发各种Adapter的用法

   ListView和GridView的Adapter:

1、继承BaseAdapter

2、新建Holder类声明控件

private class DiyHolder{
        TextView tv_job;
        ImageView iv_select_job;
    }

3、覆写getView中做好converView重用,用converView.setTag(diyHolder)

4、如果用到getItemViewType(),一般是从数据中赋值确定的,getView方法就要根据type来判断后再设定converView.setTag(diyHolder),一个type对应一个holder对象,

      取出来是也要根据type来判断。

示例代码,只给getView();

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ListTopHolder mListTopHolder;
        
        final CommentTitleHolder mCommentTitleHolder;

        final TopicManagerListInfo topicManagerListInfo = mInfo.get(position);
        final int type = getItemViewType(position);

        if (convertView == null) {
            if (type == TopicManagerListInfo.list_top) {
                convertView=LayoutInflater.from(parent.getContext()).inflate(R.layout.topic_manager_top_layout,null);
                ListTopHolder listTopHolder=new ListTopHolder();
                listTopHolder.topic_banner_img=(ImageView) convertView.findViewById(R.id.topic_banner_img);
                listTopHolder.topic_title=(TextView) convertView.findViewById(R.id.topic_title);
                listTopHolder.topic_description=(TextView) convertView.findViewById(R.id.topic_description);
                convertView.setTag(listTopHolder);
               
            }  else if (type == TopicManagerListInfo.user_title) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.topic_manager_list_title_layout, null);
                CommentTitleHolder commentTitleHolder = new CommentTitleHolder();
                commentTitleHolder.comment_title_txt = (TextView) convertView.findViewById(R.id.comment_title_txt);
                convertView.setTag(commentTitleHolder);
            }
        }


        if (type == TopicManagerListInfo.list_top) {
            mListTopHolder=(ListTopHolder) convertView.getTag();
            //话题banner
            if(topicManagerListInfo.getTopicBannerPath() != null && !topicManagerListInfo.getTopicBannerPath().isEmpty()){
                String path=topicManagerListInfo.getTopicBannerPath();
                mListTopHolder.topic_banner_img.setTag(path);
                ImageCacheManager.loadImage(path,
                        ImageCacheManager.getImageListener(mListTopHolder.topic_banner_img, mContext.getResources().getDrawable(R.drawable.avatar_default), mContext.getResources().getDrawable(R.drawable.avatar_default), path));
                ImageCacheManager.getImageListener(mListTopHolder.topic_banner_img, mContext.getResources().getDrawable(R.drawable.avatar_default), mContext.getResources().getDrawable(R.drawable.avatar_default), path);
                mListTopHolder.topic_banner_img.setVisibility(View.VISIBLE);
            }else{
                mListTopHolder.topic_banner_img.setVisibility(View.GONE);
            }
            //话题标题
            mListTopHolder.topic_title.setText(topicManagerListInfo.getTopicTitle());
            //话题描述
            mListTopHolder.topic_description.setText(topicManagerListInfo.getTopicDescription());

        }  else if (type == TopicManagerListInfo.user_title) {
            mCommentTitleHolder = (CommentTitleHolder) convertView.getTag();
            //标题
            mCommentTitleHolder.comment_title_txt.setText("话题管理");
            mCommentTitleHolder.comment_title_txt.setTextSize(14);


        }
        return convertView;
    }

    RecyclerView的Adapter

1、首先要setLayoutManager(),比如:setLayoutManager(new LinearLayoutManager(mContext)),这里的setLayoutManager()大有文章,竖排列,横排列,两列或者多列

      都是有它来设置的。

2、继承RecyclerView.Adapter

3、DiyHolder需要继承RecyclerView.ViewHolder

4、onCreateViewHolder绑定布局item_layout

5、onBindViewHolder数据处理

整个Adapter的示例代码:

public class GreetWordsPreviewAdapter extends RecyclerView.Adapter{
    private Context mContext;

    private ArrayList mInfo = new ArrayList<>();

    public GreetWordsPreviewAdapter(Context mContext, ArrayList mInfo) {
        this.mContext = mContext;
        this.mInfo = mInfo;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_greet_words_preview, parent, false);
        DiyHolder holder = new DiyHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        String msg = mInfo.get(position);
        if(holder instanceof DiyHolder){
            DiyHolder diyHolder = (DiyHolder)holder;
            ImageLoaderUtil.loadCircular(diyHolder.iv_head, Utils.getUser().getHead_img_small(), mContext);
            diyHolder.tv_greet_word.setText(msg);
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return mInfo.size();
    }

    static class DiyHolder extends RecyclerView.ViewHolder {
        @BindById(R.id.iv_head)
        ImageView iv_head;
        @BindById(R.id.tv_greet_word)
        TextView tv_greet_word;

        public DiyHolder(View view) {
            super(view);
            PreIOC.binder(this,view);

        }
    }


}


你可能感兴趣的:(android开发日记)