美文网首页
Android RecyclerView

Android RecyclerView

作者: 潜心之力 | 来源:发表于2020-05-17 23:12 被阅读0次

    一、XML 布局文件

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never">
    </android.support.v7.widget.RecyclerView>
    

    二、Adapter 适配器

    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import com.bumptech.glide.Glide;
    import java.util.ArrayList;
    import java.util.List;
    
    public class SubmitAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
        private List<Submit> mSubmitRecies = new ArrayList<>();
        private Context mContext;
    
        public SubmitAdapter(List<Submit> Submit) {
            mSubmitRecies = Submit;
        }
    
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            mContext = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(mContext);
            switch (viewType) {
                case 0:
                    View view = inflater.inflate(R.layout.adapter_submit_image, parent, false);
                    return new SubmitViewHolder(view);
                case 1:
                    View view1 = inflater.inflate(R.layout.adapter_submit_add, parent, false);
                    return new SubmitViewHolder2(view1);
                default:
                    return null;
            }
        }
    
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            int type = getItemViewType(position);
            switch (type) {
                case 0:
                    ((SubmitViewHolder) holder).mTextView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mSubmitRecies.remove(position);
                            notifyDataSetChanged();
                        }
                    });
                    Glide.with(mContext)
                            .load(mSubmitRecies.get(position).getUrl())
                            .into(((SubmitViewHolder) holder).mImageView);
                    break;
                case 1:
                    ((SubmitViewHolder2)holder).mIconFontTextView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(mOnAddClickListener!=null){
                                mOnAddClickListener.onAddClick(v);
                            }
                        }
                    });
                    break;
                default:
                    break;
            }
        }
    
        @Override
        public int getItemCount() {
            return mSubmitRecies.size();
        }
    
        @Override
        public int getItemViewType(int position) {
            return mSubmitRecies.get(position).getType();
        }
    
        static class SubmitViewHolder extends RecyclerView.ViewHolder {
    
            ImageView mImageView;
            IconFontTextView mTextView;
    
            public SubmitViewHolder(View itemView) {
                super(itemView);
                AutoUtils.autoSize(itemView);
                mImageView = itemView.findViewById(R.id.widget_upload_image);
                mTextView = itemView.findViewById(R.id.widget_upload_cancel);
            }
        }
    
        static class SubmitViewHolder2 extends RecyclerView.ViewHolder{
    
            IconFontTextView mIconFontTextView;
    
            public SubmitViewHolder2(View itemView) {
                super(itemView);
                AutoUtils.autoSize(itemView);
                mIconFontTextView = itemView.findViewById(R.id.adapter_submit_add);
            }
        }
    
        public void addItem(String url, int type) {
            mSubmitRecies.add(0,new Submit(url,type));
            notifyDataSetChanged();
        }
    
        private OnAddClickListener mOnAddClickListener;
    
        public void setOnAddClickListener(OnAddClickListener onAddClickListener){
            mOnAddClickListener = onAddClickListener;
        }
    
        public interface OnAddClickListener{
            void onAddClick(View v);
        }
    }
    

    三、ItemDecoration 分割线

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.PorterDuff;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.DisplayMetrics;
    import android.view.View;
    import android.view.WindowManager;
    
    public class ItemDecoration extends RecyclerView.ItemDecoration {
    
        private static final int[] ATTRS = new int[]{
                android.R.attr.listDivider
        };
    
        public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    
        public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
    
        private Drawable mDivider;
    
        private int mOrientation;
    
        public ItemDecoration(Context context, int orientation) {
            final TypedArray array = context.obtainStyledAttributes(ATTRS);
            mDivider = array.getDrawable(0);
            array.recycle();
            setOrientation(orientation);
        }
    
        public void setOrientation(int orientation) {
            if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
                throw new IllegalArgumentException("invalid orientation");
            }
            mOrientation = orientation;
        }
    
        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                    if (mOrientation == VERTICAL_LIST) {
                drawVertical(c, parent);
            } else {
                drawHorizontal(c, parent);
            }
        }
    
        public void drawVertical(Canvas c, RecyclerView parent) {
            final int left = parent.getPaddingLeft() + getWidth(30);
            final int right = parent.getWidth() - parent.getPaddingRight();
    
            final int childCount = parent.getChildCount() - 1;
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                        .getLayoutParams();
                final int top = child.getBottom() + params.bottomMargin;
                //final int bottom = top + mDivider.getIntrinsicHeight();
                final int bottom = top + 1;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.setColorFilter(PileApplication.getPileContext().getResources().getColor(R.color.color_edeff3), PorterDuff.Mode.SRC);
                mDivider.draw(c);
            }
        }
    
        public int getWidth(int demin) {
            WindowManager manager = (WindowManager) PileApplication.getPileContext().getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics metrics = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(metrics);
            return metrics.widthPixels * demin / 750;
        }
    
        public void drawHorizontal(Canvas c, RecyclerView parent) {
            final int top = parent.getPaddingTop();
            final int bottom = parent.getHeight() - parent.getPaddingBottom();
    
            final int childCount = parent.getChildCount() - 1;
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                        .getLayoutParams();
                final int left = child.getRight() + params.rightMargin;
                final int right = left + mDivider.getIntrinsicHeight();
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (mOrientation == VERTICAL_LIST) {
                //outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
                int lastPosition = state.getItemCount() - 1;
                int position = parent.getChildAdapterPosition(view);
                if (position < lastPosition) {
                    outRect.set(0, 0, 0, 1);
                } else {
                    outRect.set(0, 0, 0, 0);
                }
            } else {
                outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
            }
        }
    }
    

    四、初始化渲染

    List<Submit> mSubmitRecies = new ArrayList<>();
    mSubmitRecies.add(new Submit("url",1));
    mSubmitAdapter = new SubmitAdapter(mSubmitRecies);
    mRecyclerView.setAdapter(mSubmitAdapter);
    GridLayoutManager manager = new GridLayoutManager(this,4);
    mRecyclerView.setLayoutManager(manager);
    
    recyclerView.setNestedScrollingEnabled(false); -> 解决滑动冲突
    recyclerView.canScrollVertically(1); -> 是否能向下滚动
    recyclerView.canScrollVertically(-1); -> 是否能向上滚动
    recyclerView.canScrollHorizontally(1); -> 是否能向右滚动
    recyclerView.canScrollHorizontally(-1); -> 是否能向左滚动
    holder.setIsRecyclable(false); -> item设置不回收
    

    相关文章

      网友评论

          本文标题:Android RecyclerView

          本文链接:https://www.haomeiwen.com/subject/pgvneftx.html