如何在Android列表视图中的特定位置放置分隔符

5

我需要在自定义列表视图中制作一个列表视图,其中我只想在某个位置上有分隔符,而不是在每个列表项后面都有。请问有没有解决这个问题的方法?

4个回答

3

您可以在列表适配器类中使用此XML文件,如下所示:

ItemsAdapter ItemsAdapter = new ItemsAdapter(EnterpriseFertilisersScreen.this,
                R.layout.list, Constant.FERTILIZERMANAGERARRAY);

R.layout."下面的XML文件"并将用户作为进一步的白色颜色。


<?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:layout_margin="2dp"

    android:background="@color/list_bg" >

    <TextView
        android:id="@+id/post"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginLeft="7dp"
        android:layout_toRightOf="@+id/bite_image"
        android:gravity="center_vertical"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

</RelativeLayout>

如果您有其他问题,请随时询问。

2
您可以为列表创建适配器,通过getView方法将分隔线作为元素放置在其中。这是Android的标准做法。

1

1

你可以按照以下方式将其添加到getView方法中:

public View getView(int position, View convertView, ViewGroup parent) {     
    if(items.get(position).get("name").startsWith("-")){    
       View divider = mInflater.inflate(R.layout."yourlayout",null);
        return divider;         } 

另外,您必须在想要添加分隔符的项目名称前加上“-”。

希望这有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接