用RecycleView實(shí)現(xiàn)StepView效果

  • 要實(shí)現(xiàn)如下效果

    期待的效果

    發(fā)現(xiàn)要把文字上面的原點(diǎn)居中在文字區(qū)域好難實(shí)現(xiàn),突然想起來前兩天折騰的RecycleView的ItemDecoration,感覺可以用它來實(shí)現(xiàn),于是開始碼代碼。

    public class Main2Activity extends AppCompatActivity {

    RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext(),LinearLayoutManager.HORIZONTAL,false));
        recyclerView.setAdapter(new MAdapter());
        recyclerView.addItemDecoration(new MItemDecoration());
    }

    /**
     * 選中的position
     */
    int selPosition = 0;
    public void btnNext(View view) {
        int preSel = selPosition;
        selPosition = (selPosition + 1) % 4;
        //刷新前面選中的狀態(tài),和當(dāng)前選中的狀態(tài)
        recyclerView.getAdapter().notifyItemChanged(selPosition);
        recyclerView.getAdapter().notifyItemChanged(preSel);

    }


    class MViewHolder extends RecyclerView.ViewHolder{

        public MViewHolder(View itemView) {
            super(itemView);
        }
    }

    class MAdapter extends RecyclerView.Adapter<MViewHolder> {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            TextView textView = (TextView) getLayoutInflater().inflate(R.layout.layout_item_tv, parent, false);
            textView.setText("AAAA");

            return new MViewHolder(textView);
        }

        @Override
        public void onBindViewHolder(MViewHolder holder, int position) {
            if (position == selPosition) {//高亮當(dāng)前選中的文字顏色
                ((TextView)holder.itemView).setTextColor(Color.RED);
            }else{
                ((TextView)holder.itemView).setTextColor(Color.BLACK);
            }
        }

        @Override
        public int getItemCount() {
            return 4;
        }
    }



    class MItemDecoration extends RecyclerView.ItemDecoration{

        /**
         * 圓圈的半徑
         */
        private int cicleRadious = PxUtils.dpToPx(15, getBaseContext());
        /**
         * 圓圈與頂部的距離
         */
        private int marginTop=PxUtils.dpToPx(10, getBaseContext());
        /**
         * 圓圈與線的距離
         */
        private int marginLine=PxUtils.dpToPx(5, getBaseContext());
        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            Log.i("TEST", "onDrawOver: ");
            int childCount = parent.getChildCount();
            Drawable drawable = ContextCompat.getDrawable(getBaseContext(), R.drawable.shape_cicle);
            Drawable drawableSel = ContextCompat.getDrawable(getBaseContext(), R.drawable.shape_cicle_sel);
            Drawable drawableLine = ContextCompat.getDrawable(getBaseContext(), R.drawable.shape_line);
            Drawable drawableLineSel=ContextCompat.getDrawable(getBaseContext(), R.drawable.shape_line_sel);
            for (int i=0;i<childCount;i++) {
                View childAt = parent.getChildAt(i);
                int left = childAt.getLeft();
                int right = childAt.getRight();

                if (i == selPosition) {//選中的圓圈顏色
                    drawableSel.setBounds(left+childAt.getWidth()/2-cicleRadious,marginTop,right-childAt.getWidth()/2+cicleRadious,marginTop+cicleRadious*2);
                    drawableSel.draw(c);
                }else{
                    //圓圈的半徑40px,距離頂部30px
                    drawable.setBounds(left+childAt.getWidth()/2-cicleRadious,marginTop,right-childAt.getWidth()/2+cicleRadious,marginTop+cicleRadious*2);
                    drawable.draw(c);
                }



                if (i > 0) {
                    View preView = parent.getChildAt(i - 1);
                    if (i == selPosition) {//選中的線的顏色
                        drawableLineSel.setBounds(preView.getRight()-preView.getWidth()/2+(cicleRadious+marginLine),marginTop+cicleRadious-drawableLine.getIntrinsicHeight()/2,childAt.getLeft()+childAt.getWidth()/2-(cicleRadious+10),marginTop+cicleRadious+drawableLine.getIntrinsicHeight());
                        drawableLineSel.draw(c);
                    }else{
                        //直線距離左右兩邊10px
                        drawableLine.setBounds(preView.getRight()-preView.getWidth()/2+(cicleRadious+marginLine),marginTop+cicleRadious-drawableLine.getIntrinsicHeight()/2,childAt.getLeft()+childAt.getWidth()/2-(cicleRadious+10),marginTop+cicleRadious+drawableLine.getIntrinsicHeight());
                        drawableLine.draw(c);
                    }
                }

            }
        }

    }
}

關(guān)鍵代碼如下:

  • 繪制圓圈和線,高亮當(dāng)前的step
@Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            Log.i("TEST", "onDrawOver: ");
            int childCount = parent.getChildCount();
            Drawable drawable = ContextCompat.getDrawable(getBaseContext(), R.drawable.shape_cicle);
            Drawable drawableSel = ContextCompat.getDrawable(getBaseContext(), R.drawable.shape_cicle_sel);
            Drawable drawableLine = ContextCompat.getDrawable(getBaseContext(), R.drawable.shape_line);
            Drawable drawableLineSel=ContextCompat.getDrawable(getBaseContext(), R.drawable.shape_line_sel);
            for (int i=0;i<childCount;i++) {
                View childAt = parent.getChildAt(i);
                int left = childAt.getLeft();
                int right = childAt.getRight();

                if (i == selPosition) {//選中的圓圈顏色
                    drawableSel.setBounds(left+childAt.getWidth()/2-cicleRadious,marginTop,right-childAt.getWidth()/2+cicleRadious,marginTop+cicleRadious*2);
                    drawableSel.draw(c);
                }else{
                    //圓圈的半徑40px,距離頂部30px
                    drawable.setBounds(left+childAt.getWidth()/2-cicleRadious,marginTop,right-childAt.getWidth()/2+cicleRadious,marginTop+cicleRadious*2);
                    drawable.draw(c);
                }



                if (i > 0) {
                    View preView = parent.getChildAt(i - 1);
                    if (i == selPosition) {//選中的線的顏色
                        drawableLineSel.setBounds(preView.getRight()-preView.getWidth()/2+(cicleRadious+marginLine),marginTop+cicleRadious-drawableLine.getIntrinsicHeight()/2,childAt.getLeft()+childAt.getWidth()/2-(cicleRadious+10),marginTop+cicleRadious+drawableLine.getIntrinsicHeight());
                        drawableLineSel.draw(c);
                    }else{
                        //直線距離左右兩邊10px
                        drawableLine.setBounds(preView.getRight()-preView.getWidth()/2+(cicleRadious+marginLine),marginTop+cicleRadious-drawableLine.getIntrinsicHeight()/2,childAt.getLeft()+childAt.getWidth()/2-(cicleRadious+10),marginTop+cicleRadious+drawableLine.getIntrinsicHeight());
                        drawableLine.draw(c);
                    }
                }

            }
        }
  • 切換Step
    /**
     * 選中的position
     */
    int selPosition = 0;
    public void btnNext(View view) {
        int preSel = selPosition;
        selPosition = (selPosition + 1) % 4;
        //刷新前面選中的狀態(tài),和當(dāng)前選中的狀態(tài)
        recyclerView.getAdapter().notifyItemChanged(selPosition);
        recyclerView.getAdapter().notifyItemChanged(preSel);

    }
  • 高亮選中文字
        @Override
        public void onBindViewHolder(MViewHolder holder, int position) {
            if (position == selPosition) {//高亮當(dāng)前選中的文字顏色
                ((TextView)holder.itemView).setTextColor(Color.RED);
            }else{
                ((TextView)holder.itemView).setTextColor(Color.BLACK);
            }
        }
layout_item_tv.xml
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:gravity="bottom|center_horizontal"
            android:layout_width="80dp" android:layout_height="60dp">
        
        </TextView>

shape_cicle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring"
    android:innerRadiusRatio="5"
    android:thicknessRatio="10"
    android:useLevel="false"
    >
    <solid android:color="#333" />

</shape>

shape_cicle_sel.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring"
    android:innerRadiusRatio="5"
    android:thicknessRatio="10"
    android:useLevel="false"
    >

    <solid android:color="@android:color/holo_red_light" />

</shape>

shape_line.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@android:color/holo_purple" />
    <size android:height="1dp" />
</shape>

shape_line_sel.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@android:color/holo_red_dark" />
    <size android:height="1dp" />
</shape>
  • 實(shí)現(xiàn)的效果


    效果

    是不是還不錯(cuò)。發(fā)現(xiàn)到盡頭再滑動(dòng)會(huì)有overScroll的效果,有點(diǎn)惡心,于是去掉它。
    配置如下屬性

    android:overScrollMode="never"
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:overScrollMode="never"
        android:layout_height="60dp">
    
    </android.support.v7.widget.RecyclerView>
當(dāng)然,這個(gè)實(shí)現(xiàn)很簡陋,只是做一個(gè)記錄,后面會(huì)做進(jìn)一步封裝。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容