第四周(1)

本周我主要想实现发送图片的功能,这次我先实现了图片选择界面的相关功能,主要有图片展示用的RecycleViewAdapter和对应的RViewHolder。参考博文地址:https://blog.csdn.net/u014628886/article/details/52184027

构造一个比较完整的Adapter至少需要完成以下三件事情:

1.onCreateViewHolder通过视图id加载不同Item视图并生成ViewHolder用来保存每个列表Item视图。

2.onBindViewHolder更新列表Item视图(填充model数据)

3.新建ViewHolder类来存储Item视图及其子视图

public abstract class RecycleViewAdapter<T> extends RecyclerView.Adapter<RViewHolder> {

    private Context mContext;
    private List<T> mList;
    private LayoutInflater mInflater;
    private OnItemClickListener mClickListener;
    public RecycleViewAdapter(Context context, List<T> data){
        this.mContext = context;
        this.mList = data;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RViewHolder onCreateViewHolder(ViewGroup parent, int itemLayout) {
        View view = mInflater.inflate(itemLayout, parent, false);
        return new RViewHolder(mContext,view);
    }

    @Override
    public void onBindViewHolder(final RViewHolder holder, int position) {
        View view = holder.getConvertView();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClickListener != null){
                    mClickListener.onItemClick(holder,holder.getAdapterPosition());
                }
            }
        });
        bindView(holder,position);
    }

    @Override
    public int getItemViewType(int position) {
        return this.setItemLayoutId(position);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    /**
     * set item layout id
     * @param position item'position in list
     * @return layout id
     */
    public abstract int setItemLayoutId(int position);

    /**
     * bind view by holder
     * @param holder view holder
     * @param position position in data list
     */
    public abstract void bindView(RViewHolder holder, int position);


    public void setItemClickListener(OnItemClickListener clickListener){
        this.mClickListener = clickListener;
    }
}

对应Item的点击事件,需要在onBindViewHolder中适当绑定点击事件。具体的实现要在使用该Adapter的activity中为adapter设置点击监听器。

view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClickListener != null){
                    mClickListener.onItemClick(holder,holder.getAdapterPosition());
                }
            }

对应的RviewHolder:

public class RViewHolder extends RecyclerView.ViewHolder{
    private SparseArray<View> mViews;
    private View mConvertView;
    private Context mContext;

    public RViewHolder(Context context, View itemView) {
        super(itemView);
        mContext = context;
        mConvertView = itemView;
        mViews = new SparseArray<>();
    }

    private <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }

    public ImageView getImageView(int viewId){
        return getView(viewId);
    }

    public void setText(int viewId, String text) {
        TextView tv = getView(viewId);
        tv.setText(text);
    }
    public void setVisible(int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
    }
    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        view.setOnClickListener(listener);
    }
}


展开阅读全文

没有更多推荐了,返回首页