原锟侥o拷ItemDecoration in Android Part 1 : Avoid adding dividers to the view layout 

Part 1锟斤拷锟斤拷要锟斤拷view锟斤拷锟街革拷锟斤拷

1-kX-yg-0eZKxyfA6oxQsjkg.jpeg

锟斤拷锟饺o拷什么锟斤拷ItemDecoration锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷谓锟斤拷偷摹锟?/p>

ItemDecoration锟斤拷锟斤拷锟斤拷adapter锟斤拷锟斤拷锟捷硷拷锟斤拷锟斤拷为锟截讹拷锟斤拷item锟斤拷图锟斤拷锟斤拷锟斤拷锟皆的伙拷锟斤拷锟皆硷拷锟斤拷锟街硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞碉拷锟絠tem之锟斤拷姆指锟斤拷撸锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟竭斤拷取锟?/span>

锟斤拷锟角诧拷锟杰简单的帮拷ItemDecoration锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥分革拷锟竭★拷锟斤拷锟斤拷divider要锟斤拷芏锟斤拷锟斤拷荨锟揭伙拷锟絛ivider只锟杰伙拷锟斤拷锟斤拷item之锟戒,锟斤拷锟斤拷ItemDecoration锟斤拷锟皆伙拷锟斤拷锟斤拷item锟斤拷锟侥边★拷ItemDecoration为decoration锟侥诧拷锟斤拷锟酵伙拷锟斤拷锟结供锟斤拷全锟斤拷位锟侥匡拷锟狡★拷一锟斤拷decoration锟斤拷锟斤拷锟斤拷一锟斤拷锟街革拷锟竭o拷也锟斤拷锟皆斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟絠nset锟斤拷锟斤拷

锟斤拷锟斤拷锟揭碉拷锟角o拷锟斤拷锟斤拷锟斤拷锟絘ndroid锟斤拷锟斤拷锟竭讹拷没锟斤拷使锟斤拷item decoration锟斤拷锟斤拷锟斤拷锟斤拷锟轿拷锟斤拷锟斤拷值锟较碉拷锟斤拷锟斤拷锟斤拷校锟斤拷锟斤拷墙锟斤拷私锟絀temDecoration锟斤拷强锟斤拷之锟斤拷锟斤拷

锟斤拷一锟斤拷锟斤拷: 锟斤拷要锟斤拷锟斤拷view锟斤拷锟斤拷锟街革拷锟竭★拷 使锟斤拷 ItemDecoration

锟节讹拷锟斤拷锟斤拷: 锟斤拷要使锟斤拷padding锟斤拷锟斤拷锟斤拷锟?#8202;锟斤拷使锟斤拷 ItemDecoration

锟斤拷锟斤拷锟斤拷锟斤拷: 锟斤拷GridLayoutManager锟叫革拷效锟侥伙拷锟斤拷decorations

锟斤拷锟斤拷锟角碉拷一锟斤拷锟街★拷

锟斤拷要锟斤拷view锟斤拷锟街革拷锟斤拷 锟斤拷锟斤拷影锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷一些锟斤拷锟斤拷锟斤拷锟斤拷为RecyclerView锟斤拷锟斤拷divider锟斤拷时锟斤拷锟斤拷锟斤拷锟揭恍╋拷菥锟斤拷锟皆拷锟杰简单o拷ListView原锟斤拷支锟斤拷divider锟斤拷锟斤拷锟斤拷直锟斤拷锟斤拷xml锟斤拷锟斤拷锟斤拷divider锟斤拷

<ListView
    android:id="@+id/activity_home_list_view"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:divider="@android:color/black"
    android:dividerHeight="8dp"/>

锟斤拷锟角碉拷锟斤拷RecyclerView锟斤拷锟斤拷锟斤拷也锟斤拷锟斤拷直锟斤拷锟斤拷锟斤拷divider锟剿★拷锟斤拷要锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷divider锟斤拷ItemDecoration锟斤拷锟斤拷锟角匡拷锟斤拷锟竭凤拷锟斤拷锟斤拷锟斤拷锟介烦锟斤拷锟斤拷锟斤拷直锟接帮拷divider锟斤拷锟接碉拷锟斤拷item锟侥o拷view锟较o拷锟斤拷锟斤拷锟斤拷使锟斤拷ItemDecoration锟斤拷

<LinearLayout android:orientation="vertical">
    <LinearLayout android:orientation="horizontal">
        <ImageView />
        <TextView />
    </LinearLayout>
    <View
        android:width="match_parent"
        android:height="1dp"
        android:background="#333" />
</LinearLayout>

每锟斤拷锟斤拷锟斤拷锟竭捷撅拷锟斤拷时锟津,讹拷锟叫匡拷锟杰伙拷锟斤拷锟斤拷锟斤拷锟斤拷谩锟斤拷锟斤拷锟斤拷锟侥革拷锟斤拷锟斤拷锟角匡拷锟斤拷影锟斤拷锟斤拷锟杰★拷

锟斤拷锟节诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷divider锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷view锟侥革拷锟斤拷锟斤拷锟斤拷锟角讹拷知锟斤拷view锟斤拷锟斤拷目越锟劫伙拷玫锟皆斤拷玫锟斤拷锟斤拷堋锟斤拷锟绞憋拷锟斤拷锟斤拷锟揭伙拷锟絭iew锟斤拷实锟斤拷divider锟斤拷锟斤拷锟斤拷锟接诧拷锟街的层级锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷校锟斤拷锟斤拷遣锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟絭iew锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟角碉拷 linear layout锟斤拷为锟斤拷一锟斤拷divider锟斤拷锟斤拷锟斤拷锟剿讹拷锟斤拷牟锟斤拷帧锟?/p>

锟斤拷要锟斤拷view锟斤拷锟街革拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟?/h3>

锟斤拷为divider锟斤拷view锟斤拷一锟斤拷锟街o拷锟斤拷锟斤拷锟斤拷item 锟斤拷锟斤拷锟节间,divider也锟斤拷一锟斤拷锟斤拷哦锟斤拷锟斤拷锟斤拷锟斤拷锟酵硷拷锟?/p>

1-bADMI7LYWaJo-ZGiWk0HVw.gif

锟斤拷然divider锟斤拷应锟斤拷锟斤拷锟斤拷item一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角猴拷item锟街匡拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角对的o拷

1-Jk50iGRPixKN6oe9onq75w.gif

锟斤拷要锟斤拷view锟斤拷锟街革拷锟竭★拷 缺锟斤拷锟斤拷锟斤拷锟?/h3>

锟斤拷锟絛ivider锟角o拷item锟侥o拷view锟斤拷一锟斤拷锟街o拷锟斤拷么锟斤拷锟斤拷薹锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿ㄒ伙拷芸锟斤拷频木锟斤拷歉锟斤拷锟絠tem锟斤拷position锟侥憋拷divider锟侥可硷拷状态锟斤拷 锟斤拷item decoration锟斤拷锟斤拷锟斤拷锟剿★拷

1-VdxwtT2NKyLm3PkAxNwLvQ.png

In the above image for the last item in the group divider fills the entire width. Other dividers have a margin of 56dp to their left side. Here is the ItemDecorator锟斤拷s onDraw code.

锟斤拷锟斤拷图锟叫o拷group锟斤拷锟揭伙拷锟絠tem锟斤拷divider锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟饺★拷锟斤拷锟斤拷锟斤拷divider锟斤拷锟斤拷一锟斤拷56dp锟斤拷锟斤拷呔唷o拷锟斤拷锟斤拷锟斤拷ItemDecorator锟斤拷onDraw锟斤拷锟诫:

@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
  canvas.save();
  final int leftWithMargin = convertDpToPixel(56);
  final int right = parent.getWidth();
  final int childCount = parent.getChildCount();
  for (int i = 0; i < childCount; i++) {
    final View child = parent.getChildAt(i);
    int adapterPosition = parent.getChildAdapterPosition(child);
    left = (adapterPosition == lastPosition) ?  0 : leftWithMargin;
    parent.getDecoratedBoundsWithMargins(child, mBounds);
    final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));
    final int top = bottom - mDivider.getIntrinsicHeight();
    mDivider.setBounds(left, top, right, bottom);
    mDivider.draw(canvas);
  }
  canvas.restore();
}

锟斤拷要锟斤拷view锟斤拷锟街革拷锟竭★拷使锟斤拷 ItemDecoration

写一锟斤拷锟皆硷拷锟斤拷ItemDecoration锟斤拷实锟角筹拷锟津单★拷锟斤拷只锟斤拷要锟斤拷锟斤拷一锟斤拷锟教筹拷锟斤拷ItemDecoration锟斤拷锟斤拷涂锟斤拷锟斤拷恕锟斤拷锟叫?nbsp;getItemOffsets() 锟斤拷 onDraw() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷实锟街匡拷锟皆参匡拷 锟斤拷锟?/a> 示锟斤拷锟斤拷

锟斤拷 25.0.0锟芥本锟斤拷支锟街匡拷锟叫o拷锟斤拷锟斤拷锟斤拷一锟斤拷锟铰碉拷锟斤拷 锟斤拷DividerItemDecoration锟斤拷锟斤拷锟斤拷锟斤拷锟街憋拷锟绞碉拷锟斤拷锟絛ivider锟斤拷

DividerItemDecoration decoration = new DividerItemDecoration(getApplicationContext(), VERTICAL);
recyclerView.addItemDecoration(decoration);

锟斤拷示

  1. 一锟斤拷RecyclerView锟斤拷锟斤拷锟斤拷锟接讹拷锟絀temDecoration锟斤拷锟斤拷锟斤拷头锟皆风暴锟斤拷时锟斤拷锟剿★拷

  2. 锟斤拷锟斤拷decoration锟斤拷锟斤拷item锟斤拷锟斤拷之前锟斤拷锟狡★拷锟斤拷锟斤拷锟斤拷锟斤拷锟絛ecoration锟斤拷view之锟斤拷锟斤拷疲锟斤拷锟叫磑nDrawOver() 锟斤拷锟斤拷锟斤拷onDraw() 锟斤拷

锟斤拷锟斤拷锟铰达拷锟斤拷为RecyclerView锟斤拷锟接分革拷锟竭碉拷时锟津,憋拷使锟斤拷锟斤拷item锟斤拷锟斤拷锟斤拷锟斤拷view锟斤拷锟街凤拷式锟剿o拷使锟斤拷ItemDecoration锟斤拷

为锟剿帮拷锟斤拷锟斤拷锟斤拷锟揭碉拷锟斤拷篇锟斤拷锟铰o拷锟斤拷锟斤拷❤ 锟斤拷锟斤拷锟解,锟揭伙拷写锟斤拷一锟斤拷锟斤拷锟斤拷锟津化达拷锟皆讹拷锟斤拷item decoration锟斤拷adapter锟斤拷写锟斤拷锟斤拷library锟斤拷

锟斤拷锟斤拷Riyaz Ahamed  印锟饺帮拷锟斤拷薅锟斤拷锟?/p>