1. 程式人生 > >Android RecyclerView ItemDecoration timeline(一)

Android RecyclerView ItemDecoration timeline(一)

package zhangphil.book;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView mRecyclerView = findViewById(R.id.recycler_view);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        RecyclerViewAdapter adapter = new RecyclerViewAdapter();
        mRecyclerView.setAdapter(adapter);

        RVItemDecoration mRVItemDecoration = new RVItemDecoration(getApplicationContext());
        mRecyclerView.addItemDecoration(mRVItemDecoration);
    }

    private class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
        @NonNull
        @Override
        public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getApplicationContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
            RecyclerViewHolder holder = new RecyclerViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
            holder.text1.setText(position + "");
        }

        @Override
        public int getItemCount() {
            return Integer.MAX_VALUE;
        }
    }

    private class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public TextView text1;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            text1 = itemView.findViewById(android.R.id.text1);
        }
    }

    private class RVItemDecoration extends RecyclerView.ItemDecoration {
        private Drawable mDrawable;

        //軸線距離左側的距離,單位pix。
        private int marginLeft = 20;

        //軸線的寬度,單位pix。
        private int lineSize = 10;

        private Paint mPaint;

        public RVItemDecoration(Context context) {
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.RED);

            mDrawable = new ColorDrawable(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_blue_light));
        }

        @Override
        public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
            canvas.save();

            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);

                //先畫線。
                mDrawable.setBounds(marginLeft, child.getTop(), marginLeft + lineSize, child.getBottom());
                mDrawable.draw(canvas);

                //後畫覆蓋在線上的圓球。
                canvas.drawCircle(marginLeft + lineSize / 2, (child.getTop() + child.getBottom()) / 2, (lineSize + marginLeft) / 2, mPaint);
            }

            canvas.restore();
        }

        /**
         * outRect.set()函式相當於給RecyclerView的每一個item的View四周設定margin值。
         * 此處的margin值不同於onDraw裡面的margin,onDraw裡面給mDrawable設定的margin,僅僅是針對分割條而言。
         *
         * @param outRect
         * @param view
         * @param parent
         * @param state
         */
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.set((marginLeft + lineSize) * 2, 0, 0, 0);

            //測試。給RecyclerView的每一個item設定一個淺灰色背景。
            //便於觀察繪製結果。
            view.setBackgroundColor(Color.LTGRAY);
        }
    }
}

so: