基于RecyclerView的校车时刻表小练习

Posted Notebook

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于RecyclerView的校车时刻表小练习相关的知识,希望对你有一定的参考价值。

第一阶段工作:http://www.cnblogs.com/hbuwyg/p/6978592.html

第二阶段工作:修改app图标,文本内容,并加入点击按钮切换方向功能。

 1 final Button  buttonChange = (Button) findViewById(R.id.my_button) ;
 2 
 3         buttonChange.setOnClickListener(new View.OnClickListener(){
 4             @Override
 5             public void onClick(View v){
 6                 String flag = buttonChange.getText().toString();
 7 
 8                 if(flag.equals( "本部-新区")) {
 9                     buttonChange.setText("新区-本部");
10                     initData2();
11                     initView();
12                 }
13                 if(flag.equals("新区-本部") ) {
14                     buttonChange.setText("本部-新区");
15                     initData();
16                     initView();
17                 }
18             }
19         });

第三阶段工作:美化程序

RecyclerView增加分隔线

增加新类 DividerItemDecoration

 1 package com.example.reclerviewpractice;
 2 
 3         import android.content.Context;
 4         import android.content.res.TypedArray;
 5         import android.graphics.Canvas;
 6         import android.graphics.Rect;
 7         import android.graphics.drawable.Drawable;
 8         import android.support.v7.widget.LinearLayoutManager ;
 9         import android.support.v7.widget.RecyclerView;
10         import android.view.View;
11 
12 public class DividerItemDecoration extends RecyclerView.ItemDecoration {
13 
14     private static final int[] ATTRS = new int[]{
15             android.R.attr. listDivider
16     };
17 
18     public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
19 
20     public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
21 
22     private Drawable mDivider;
23 
24     private int mOrientation;
25 
26     public DividerItemDecoration(Context context, int orientation) {
27         final TypedArray a = context.obtainStyledAttributes(ATTRS );
28         mDivider = a.getDrawable(0);
29         a.recycle();
30         setOrientation(orientation);
31     }
32 
33     public void setOrientation( int orientation) {
34         if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
35             throw new IllegalArgumentException( "invalid orientation");
36         }
37         mOrientation = orientation;
38     }
39 
40     @Override
41     public void onDraw(Canvas c, RecyclerView parent) {
42         if (mOrientation == VERTICAL_LIST) {
43             drawVertical(c, parent);
44         } else {
45             drawHorizontal(c, parent);
46         }
47     }
48 
49     public void drawVertical(Canvas c, RecyclerView parent) {
50         final int left = parent.getPaddingLeft();
51         final int right = parent.getWidth() - parent.getPaddingRight();
52 
53         final int childCount = parent.getChildCount();
54         for (int i = 0; i < childCount; i++) {
55             final View child = parent.getChildAt(i);
56             final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
57                     .getLayoutParams();
58             final int top = child.getBottom() + params.bottomMargin;
59             final int bottom = top + mDivider.getIntrinsicHeight();
60             mDivider.setBounds(left, top, right, bottom);
61             mDivider.draw(c);
62         }
63     }
64 
65     public void drawHorizontal(Canvas c, RecyclerView parent) {
66         final int top = parent.getPaddingTop();
67         final int bottom = parent.getHeight() - parent.getPaddingBottom();
68 
69         final int childCount = parent.getChildCount();
70         for (int i = 0; i < childCount; i++) {
71             final View child = parent.getChildAt(i);
72             final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
73                     .getLayoutParams();
74             final int left = child.getRight() + params.rightMargin;
75             final int right = left + mDivider.getIntrinsicHeight();
76             mDivider.setBounds(left, top, right, bottom);
77             mDivider.draw(c);
78         }
79     }
80 
81     @Override
82     public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
83         if (mOrientation == VERTICAL_LIST) {
84             outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
85         }else{
86             outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
87         }
88     }
89 }

 

 2 在 MainActivity中设置,新增黄色部分代码。

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        // 设置布局管理器
        mRecyclerView.setLayoutManager(mLayoutManager);
        // 设置adapter
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
    }

 

效果图如下:

 

隐藏标题栏,actionbar

requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
getSupportActionBar().hide();// 隐藏ActionBar

以上是关于基于RecyclerView的校车时刻表小练习的主要内容,如果未能解决你的问题,请参考以下文章

智能校车刷卡解决方案--微信推送

如何挑选童书《神奇的校车》

2015新交规扣分标准规定

案例分析Yunxion车辆跟踪设备助力校车安全

干货练习题︱基于今日头条开源数据的文本挖掘

关于校车安全的英语作文附中文翻译80字