不知道那些工作了好久的大神是怎样坚持写博客的,我发现我完全没时间写啊!就算有时间写,在路上想好要学些什么结果一回到租房,直接就往床上躺了。今天在外面跑步发现年初立下要好好学习,抽时间出来写博客记录下自己的学习,结果发现博客上P都没有,于是跑完步回来立马撸了个demo。凑个数,不知道自己到底能否坚持下去,这年头在创业公司呆也不是走也不是,哎!
直接上代码吧!
DividerItemDecoration 分割线核心类
package com.example.richard.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Created by richard on 2016/6/19.
*/
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private int orientation = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int[] attrs = new int[]{android.R.attr.listDivider};
public DividerItemDecoration(Context context, int orientation) {
TypedArray typedArray = context.obtainStyledAttributes(attrs);
mDivider = typedArray.getDrawable(0);
typedArray.recycle();
setOrientation(orientation);
}
public DividerItemDecoration(Context context, int orientation, int resId){
mDivider = context.getResources().getDrawable(resId);
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
throw new IllegalArgumentException("this orientation is error!");
}
this.orientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (orientation == LinearLayoutManager.VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//先调用getItemOffsets获取条目之间的间隙
//获得条目的偏移量(所有的条目都回调用一次该方法)
if (orientation == LinearLayoutManager.VERTICAL) {//垂直的画水平线
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
//水平的画垂直线
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
/**
* 垂直方向 画水平线
*
* @param c
* @param parent
*/
private void drawVertical(Canvas c, RecyclerView parent) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight(); //减去内边距
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin + (int) ViewCompat.getTranslationY(child);//可能有外边距 或 者有偏移的动画
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
/**
* 水平方向 画垂直线
*
* @param c
* @param parent
*/
private void drawHorizontal(Canvas c, RecyclerView parent) {
int top = parent.getPaddingTop();
int bottom = parent.getHeight() - parent.getPaddingBottom(); //减去内边距
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int left = child.getRight() + params.rightMargin + (int) ViewCompat.getTranslationX(child);//可能有外边距 或 者有偏移的动画
int right = left + mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
adapter
package com.example.richard.myapplication;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.MyViewHolder> {
private List<String> dataList;
public TestAdapter(List<String> list) {
this.dataList = list;
}
@Override
public int getItemCount() {
return dataList.size();
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv.setText(dataList.get(position));
holder.tv.setBackgroundColor(Color.rgb((int)(Math.random() * 255),(int)(Math.random() * 255),(int)(Math.random() * 255)));
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new MyViewHolder(view);
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.tv);
}
}
}
MainActivity:
package com.example.richard.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button btn;
private boolean isVertichal = true;
private DividerItemDecoration itemDecoration;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
recyclerView = (RecyclerView) findViewById(R.id.recylerview);
List<String> dataList = new ArrayList<>();
for (int i = 0; i < 100; i++){
dataList.add("编号: " + i);
}
TestAdapter adapter = new TestAdapter(dataList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
itemDecoration = new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
recyclerView.addItemDecoration(itemDecoration);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recyclerView.removeItemDecoration(itemDecoration);
if (isVertichal){
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
itemDecoration = new DividerItemDecoration(MainActivity.this, LinearLayoutManager.HORIZONTAL, R.drawable.divider_item);
recyclerView.addItemDecoration(itemDecoration);
}else{
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
itemDecoration = new DividerItemDecoration(MainActivity.this, LinearLayoutManager.VERTICAL, R.drawable.divider_item);
recyclerView.addItemDecoration(itemDecoration);
}
isVertichal = !isVertichal;
}
});
}
}
divider_item:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="2dp"
android:height="2dp"/>
<solid
android:color="#ff0000"/>
</shape>
效果如下:
水平竖直的分割线就没截图了。PS:这年头理想值钱?让我很惆怅啊!感觉什么都是假的,唯有自己身体好才是真的。