import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewCompat; import android.support.v7.widget.RecyclerView; import android.view.View; import com.ys.a2rvdemo.R; /** * Created by lzc on 2017-12-27. */ public class MyDividerItemDecoration extends RecyclerView.ItemDecoration { private Drawable drawable; public MyDividerItemDecoration(Context context) { drawable = ContextCompat.getDrawable(context, R.drawable.dividerline); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); drawVerticalLine(c,parent); } private void drawVerticalLine(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);//获取子Veiw在ViewGroup的确切位置 RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); int top = child.getBottom() + params.bottomMargin + Math.round(ViewCompat.getTranslationY(child)); int bottom = top + drawable.getIntrinsicHeight(); drawable.setBounds(left,top,right,bottom); drawable.draw(c); } } }
RecyclerView添加分割线
最新推荐文章于 2024-09-18 07:16:23 发布