ҳ › ׿ › android

ΪʲôҪʹItemDecoration

ϵ / 2017-06-22 17:20 Ķ ItemDecoration,RecyclerView

ԭģItemDecoration in Android Part 1 : Avoid adding dividers to the view layout 

Part 1Ҫviewָ

1-kX-yg-0eZKxyfA6oxQsjkg.jpeg

ȣʲôItemDecorationν͵ġ

ItemDecorationadapterݼΪضitemͼԵĻԼּʵitem֮ķָߣ߽ȡ

Dzܼ򵥵İItemDecorationһķָߡdividerҪܶݡһdividerֻܻitem֮䣬ItemDecorationԻitemıߡItemDecorationΪdecorationIJͻṩȫλĿơһdecorationһָߣҲԽһinset

ҵǣandroid߶ûʹitem decorationΪֵϵУǽ˽ItemDecorationǿ֮

һ: Ҫviewָߡ ʹ ItemDecoration

ڶ: Ҫʹpadding ʹ ItemDecoration

: GridLayoutManagerиЧĻdecorations

ǵһ֡

Ҫviewָ Ӱ

һЩΪRecyclerViewdividerʱһЩݾԭܼ򵥣ListViewԭ֧dividerֱxmldivider

<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ˡҪһdividerItemDecorationǿ߷鷳ֱӰdividerӵitemģviewϣʹ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ĿԽٻõԽõܡʱһviewʵdividerӲֵIJ㼶УDzһviewһǵ linear layoutΪһdivider˶IJ֡

Ҫviewָ 

Ϊdividerviewһ֣item ڼ䣬dividerҲһŶͼ

1-bADMI7LYWaJo-ZGiWk0HVw.gif

ȻdividerӦitemһǺitemֿǶԵģ

1-Jk50iGRPixKN6oe9onq75w.gif

Ҫviewָߡ ȱ

dividerǣitemģviewһ֣ô޷ΨһܿƵľǸitempositionı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 ItemDecorators onDraw code.

ͼУgroupһitemdividerȡdividerһ56dp߾ࡣItemDecoratoronDraw룺

@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ʵdz򵥡ֻҪһ̳ItemDecorationͿˡд getItemOffsets()  onDraw() ʵֿԲο  ʾ

 25.0.0汾ֿ֧Уһµ DividerItemDecorationֱʵdivider

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

ʾ

  1. һRecyclerViewӶItemDecorationͷԷ籩ʱˡ

  2. decorationitem֮ǰơdecorationview֮ƣдonDrawOver() onDraw()

´ΪRecyclerViewӷָߵʱ򣬱ʹitemviewַʽˣʹItemDecoration

Ϊ˰ҵƪ£❤ ⣬һдһ򻯴Զitem decorationadapterдlibrary

Riyaz Ahamed  ӡȰ޶

ղ (13) (1)
һƪAndroid ʹúʽ
ԭӣ https://github.com/xitu/gold-miner/blob/master/TODO/functional-programming-for-android-developers-part-2.md ԭĵַ Functional Programming for Android developers??Part 2 ԭߣ Anup Cowkur ijԣ ƻ ߣ
һƪAndroidʵָӶ룩
AndroidԶżõ֧֣ʱῴЧ ܻڴ˿ס֪ӺοʼĽһһƯĶ һοЧʱܻúܸӣǿ԰ΪҪĶ 1. The animation that plays w