2017-03-23 6 views
1

xmlレイアウトを使ってカスタマイズされた仕切りを表示する必要があります。だからこのようなことをする可能性がありますアンドロイド・リストビュー・ディバイダにadd xmlレイアウトはありますか?

<ListView 
        android:id="@+id/volunteer_list" 
        android:layout_width="match_parent" 
        android:divider="@layout/separator" 
        android:dividerHeight="5dp" 
        android:layout_height="wrap_content"/> 

添付のスクリーンショットを確認してください。そこにあなたはカスタムディバイダを見ることができます。私はアンドロイドに設定する必要がありますように。あなたのコメントは大歓迎です。 enter image description here

+0

レイアウトを分割線として使用することはできません。描画可能なもののみを受け入れます。おそらくあなたが必要とするのは、カスタムdrawable.xmlで達成することができます – MatPag

+0

分割可能なイメージを描画したいですか? – rafsanahmad007

+0

@MatPagもしそうなら、私にいくつかの例を与えることができます。 – itmani1990

答えて

0

次のことを試してみてください、私は達成するために、次のように行っていた。その後、

<LinearLayout 
      android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="10dip" 
     android:orientation="vertical" > 

     <com.altss.asterridepassenger.utils.customview.CustomTextView 
      android:id="@+id/search_list_value" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/black" 
      android:layout_marginRight="15dip" 
      android:textSize="14sp" 
      aster:customFont="@string/font_lato_medium" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="10dip" //(increase as your wish) 
      android:layout_marginTop="10dip" 
      android:background="@color/search_list_line_color" /> 
    </LinearLayout> 

list_row.xml main_listview.xml

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/listviewLayout" 
    android:visibility="visible"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/listview" 
     android:visibility="gone"> 
    </ListView> 

</LinearLayout> 

活動のコードを次のように

adapter = new ArrayAdapter(this, R.layout.list_row.xml,  R.id.search_list_value,list); //list is your list items 
+0

コードをありがとう。 もしそうなら、最後のリスト項目もこのビューを持っていますか? リストビューの最終項目にビューを追加しないようにすることは可能です。 – itmani1990

+0

Basu Singhの答えを使用して、必要に応じてforループカウンタを変更するか、ListViewを使用してgetView()で次のようにします:yourDividerView.setVisibility(position == adapterItem.size() - 1?Visibility .GONE:Visibility.VISIBLE) – 345

+0

@ User123ありがとうございました。ええ、それは本当に私のために役立ちます。 – itmani1990

1

ListViewを使用している場合は、確かにダリルの答えに従ってください。ただし、最後の項目の後にもディバイダが存在します。 ListViewの前バージョンであるRecyclerViewに切り替えることをお勧めします。 RecyclerViewでは、次のように入力します。

ラインディバイダを作成します。 (あなたの必要に応じてサイズと色を変えてください)。

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 

<size 
    android:width="4dp" 
    android:height="4dp"/> 

<solid android:color="#d5d5d5" /> 

DividerItemDecoratorクラスを作成します。

public class DividerItemDecorator extends RecyclerView.ItemDecoration { 
private Drawable mDivider; 

public DividerItemDecorator(Context context) { 
    mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider); 
} 

@Override 
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { 
    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 bottom = top + mDivider.getIntrinsicHeight(); 

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

}

最後に、このコード行を使用して、リサイクラービューでDividerItemDecoratorを設定します。

recyclerView.addItemDecoration(new DividerItemDecorator(getApplicationContext())); 
0

は、すべての可能なアンドロイドリストビューデバイダのXMLレイアウトを追加しますか?

いいえ、それはできません。

あなたが添付したスクリーンショットのような仕切りを探しているときに、そのためのドロウアブルファイルを使用できます。

1つの描画可能なファイルを作るには、divider.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:gravity="center"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#c3c3c3" /> 
     </shape> 
    </item> 
    <item 
     android:height="1dp" 
     android:gravity="top"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#000" /> 
     </shape> 
    </item> 
    <item 
     android:height="1dp" 
     android:gravity="bottom"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#000" /> 
     </shape> 
    </item> 
</layer-list> 

は今あなたにListView

分圧器として
<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:divider="@drawable/divider" 
    android:dividerHeight="10dp" /> <!-- adjust according to your need --> 

をこのファイルを渡し、あなたが行ってもいいと言います。それはあなたに必要なものを正確に与えるでしょう。

出力コーディングハッピー

enter image description here

+0

コードをありがとう。 – itmani1990

関連する問題