Android ListView填充内容而不移动ListView高度?

Bob*_*ke4 36 java android listview

我有一个ListView,里面有很多项目.如何使顶部项目的顶部和底部项目的上边距为10dp,底部项目的下边距为10dp?现在我可以使用ListView上的填充或边距来执行此操作,但结果是当您滚动ListView的边缘时,现在距屏幕底部10dp.无论如何围绕这个?我也尝试在我的适配器的getView方法中设置边距但我没有看到AbsListView.LayoutParams的任何边距选项.任何帮助都会很棒.

谢谢

ada*_*amp 124

诀窍是android:clipToPadding="false"在视图定义中包含,例如:

<ListView android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="16dip"
    android:paddingBottom="16dip"
    android:clipToPadding="false" />
Run Code Online (Sandbox Code Playgroud)


Cra*_*igy 12

您可以使用页眉和页脚在滚动区域内的ListView顶部和底部获取空间.这是一个简单的例子:

定义填充视图,我将其命名为header_footer.xml:

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="10dp"/>
Run Code Online (Sandbox Code Playgroud)

现在你的onCreate方法看起来像这样,假设你命名了ListView listView:

    final LayoutInflater inflater = 
        (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View headerFooter = inflater.inflate(R.layout.header_footer, null);
    listView.addFooterView(headerFooter);
    listView.addHeaderView(headerFooter);
Run Code Online (Sandbox Code Playgroud)

然后设置适配器.


dym*_*meh 5

为什么不检查项目相对于列表大小的位置:

填充:

 public View getView(int position, View convertView, ViewGroup parent)
    {
         //recycle views and whatever else you normally would do goes here..
         //...
         //...

         if (position == 0){
            convertView.setPadding(0, 10, 0, 0); //padding on top for top item
         }
         else if (position == getCount() - 1){
            convertView.setPadding(0, 0, 0, 10); //padding on bottom for bottom item
         }
         else{
            convertView.setPadding(0, 0, 0, 0); //no padding
         }
}
Run Code Online (Sandbox Code Playgroud)

用于保证金使用

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
convertView.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)

例如:

public View getView(int position, View convertView, ViewGroup parent)
    {
             //recycle views and whatever else you normally would do goes here..
             //...
             //...
             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,        LinearLayout.LayoutParams.WRAP_CONTENT);
             if (position == 0){
                 lp.setMargins(0, 10, 0, 0); //margin on top for top item
             }
             else if (position == getCount() - 1){
                lp.setMargins(0, 10, 0, 10); //margin on bottom for bottom item
             }
             else{
                lp.setMargins(0, 0, 0, 0); //no margin
             }
             convertView.setLayoutParams(lp);
    }
Run Code Online (Sandbox Code Playgroud)

如果您已为适配器正确实现了getCount()方法,则可以使用此方法.