简体   繁体   English

使用View在ListView中添加分隔符

[英]Using View to add divider in listView

I am using a view to add a customized divider to listview. 我正在使用一个视图向列表视图添加自定义分隔符。 I have a problem,I want to hide the divider which comes before the first list item. 我有一个问题,我想隐藏第一个列表项之前的分隔线。 Any suggestions how to do this? 有什么建议怎么做? 列表显示

I don't want that divider over first item (Akshay) should be visible. 我不希望第一个项目(Akshay)上的分隔线可见。

Help me. 帮我。 Here is my code. 这是我的代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
 android:orientation="horizontal"
android:padding="5dip" >
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="0dp"
android:background="@android:color/darker_gray"/>
<!-- ListRow Left side Thumbnail image -->
<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:padding="3dip" >
    <ImageView
        android:id="@+id/user_dp"
        android:contentDescription="@string/app_name"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:background="@drawable/ic_launcher"
       />

 </LinearLayout>

 <!-- Rightend Arrow -->
 <ImageView
    android:id="@+id/arrow"
    android:contentDescription="@string/app_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:background="@drawable/arrow"

    />

 <!--User's Name-->
<TextView
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/list_image"
    android:layout_marginLeft="75dip"
    android:layout_centerVertical="true"
    android:paddingBottom ="10dip"
    android:text="hhs"
    android:textColor="#040404"
    android:textSize="20dip"
    android:textStyle="bold"
    android:typeface="sans" />

<!-- chat-->
<TextView
    android:id="@+id/chat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/username"
    android:layout_alignLeft="@+id/username"
    android:paddingTop="5dip"
    android:layout_centerHorizontal="true"
    android:text="Hi how are you"
    android:textColor="#343434"
    android:textSize="15dip" />

   </RelativeLayout>

Why don't you try this approach for custom divider 为什么不尝试将这种方法用于自定义分隔线

you need to check the position of the cell in your getView() and accordingly you can from your code hide/show your custom divider 您需要检查cellgetView() ,因此可以从代码中隐藏/显示自定义分隔符

 if(childPosition==0)//is first child
    {
        YOURDIVIDER.setVisibility(View.GONE);
    }
    else
    {
        YOURDIVIDER.setVisibility(View.VISIBLE);
    }

you need to find your divider view for this, before doing this. 为此,您需要先找到分频器视图。

in your custom adapter class where you setting value to the custom layout in getView method 在自定义适配器类中,在其中将值设置为getView方法中的自定义布局

check position of layout if it is 0 then set its visibility GONE or INVISIBLE as per your requirement 检查布局的位置是否为0,然后根据您的要求将其可见性设置为GONE或INVISIBLE

for that create object of VIEW of divider and set its setVisibility() method 为此创建分隔线的VIEW对象并设置其setVisibility()方法

 <ListView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/list"
     android:dividerHeight="1px">
 </ListView>

try 尝试

android:layout_alignParentBottom="true"

instead of 代替

android:layout_marginTop="0dp"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM