2017-09-26 130 views
0

我想插入一個RecyclerView ItemDecorator分隔線,左對齊列表元素中的父級左側約束爲72dp的TextView(項目標題)。就像我們在Google Material Design Docs中看到here一樣。我假設我需要以某種方式引用titleTextView上的佈局參數,但我不確定如何從我的ItemDecorator代碼執行該操作,因爲我似乎只能獲得ConstraintLayout的參數,而不是textview本身。任何幫助或指針在正確的方向將不勝感激!相關ItemDecorator代碼,在那裏我試圖讓TextView的PARAM看起來是這樣的:Android RecyclerView ItemDecorator:將插入分隔線與列表項對齊TextView

 for (int i = 0; i < recyclerView.getChildCount() - 1; ++i) { 
      View child = recyclerView.getChildAt(i); 
      RecyclerView.LayoutParams params =(RecyclerView.LayoutParams) child.getLayoutParams(); 
//this just gives me the constraint layout, not the TextView params, how to reference the textViewParams!? 

的RecyclerView看起來是這樣的:

 <android.support.v7.widget.RecyclerView 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/myList" 
     android:name="com.example.Something" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layoutManager="LinearLayoutManager" 
     tools:context=".controllers.fragments.ExampleFragment" 
     tools:listitem="@layout/fragment_something" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" /> 

列表項XML是一種約束佈局一些TextViews,並且相關部分如下所示:

<android.support.constraint.ConstraintLayout 
     android:layout_width="match_parent" 
     android:layout_height="72dp"> 

    <ImageView 
     android:id="@+id/myImageView" 
     android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:layout_marginLeft="16dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     tools:src="@drawable/ic_menu_manage" 
     android:src="@drawable/ic_menu_manage" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="16dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="16dp" /> 

    <TextView 
     android:id="@+id/titleTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="4dp" 
     android:layout_marginLeft="72dp" 
     android:layout_marginTop="20dp" 
     android:text="MyTitle" 
     android:textAppearance="@style/TextAppearance.AppCompat.Subhead" 
     android:textStyle="bold" 
     app:layout_constraintBottom_toTopOf="@+id/textView5" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     tools:text="SampleTitle" /> 

    ... 

    </android.support.constraint.ConstraintLayout> 

回答

1

使用自定義項目修飾符。

class CustomItemDecoration(context: Context) : RecyclerView.ItemDecoration() 
{ 

    private val mDivider: Drawable = context.resources.getDrawable(R.drawable.custom_item_divider) // this is a shape that i want to use 

    override fun onDrawOver(c: Canvas, 
          parent: RecyclerView, 
          state: RecyclerView.State) 
    { 
     val left = parent.paddingLeft // change to how much padding u want to add here . 
     val right = parent.width - parent.paddingRight 

     val childCount = parent.childCount 
     for (i in 0 until childCount) 
     { 
      val child = parent.getChildAt(i) 

      val params = child.layoutParams as RecyclerView.LayoutParams 

      val top = child.bottom + params.bottomMargin 
      val bottom = top + mDivider.intrinsicHeight 

      mDivider.setBounds(left, top, right, bottom) 
      mDivider.draw(c) 
     } 
    } 
} 

你設置它像

recyclerView.addItemDecoration(CustomItemDecoration(context)) 
+0

嗨Arutha,感謝您的回覆。我所擁有的基本上就是你寫的東西,除了你在那裏寫了「改變你想在這裏填充多少內容」,那就是我想在textview中引用填充的地方。不知道如何訪問? – bryant

+2

parent是recyclerview,所以你可以從那裏獲得視圖, val item = parent.getchildat(0) val textview = item.getchild(x)as textview 然後你可以使用textviews的data/layout參數來獲得什麼你需要 是不是在textview上的填充是靜態的?你之前在72dp的xml中寫過嗎?只需用72 * getDpi()更改paddingleft部分(某些函數將72轉換爲dpi) – Arutha

0

由於ConstraintLayout是一個ViewGroup中,就可以通過簡單地做一些像是從ItemDecorator內訪問ConstraintLayout的孩子:

public class MyItemDivider extends RecyclerView.ItemDecoration { 

....some code here 


@Override 
public void onDrawOver(Canvas canvas, RecyclerView recyclerView, 
         RecyclerView.State state) { 
    super.onDrawOver(canvas, recyclerView, state); 

    for (int i = 0; i < recyclerView.getChildCount() - 1; ++i) { 
     ViewGroup item = (ViewGroup) recyclerView.getChildAt(i); 

     //for now the titleText is at index 1 of constraint layout 
     TextView titleChild = (TextView) item.getChildAt(1); 
     int titleTextLeftMargin = titleChild.getLeft(); 

     //this would probably be less brittle 
     //but I would need to somehow change the context for this to be doable? 
     //View titleChild = item.findViewById(R.id.contractListProductNameTextView); 
     ...some more code here 

      divider.setBounds(titleTextLeftMargin, top, right, bottom); 
      divider.draw(canvas); 
     } 
    } 
} 
0

由於您已經知道您需要從左側插入分隔線多少(如果您不知道,請從您的佈局計算出),因此您需要做的只是將該分隔符t dp值到px,如here所示,並將其設置爲您的自定義ItemDecoration中的左邊界。

public class InsetDividerDecoration extends RecyclerView.ItemDecoration { 

    private final int insetValueInDp = 72; // in your case 

    @Override 
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) { 
     int dividerLeft = (int) (insetValueInDp * Resources.getSystem().getDisplayMetrics().density); 

     ... 
    } 
} 

而在你的回收站視圖中使用它 -

recyclerView.addItemDecoration(new InsetDividerDecoration());