无法在列表视图下方显示按钮

时间:2017-07-16 01:14:14

标签: android listview user-interface android-arrayadapter

PROBLEM DESCRIPTION IMAGE. THIS IS WHAT IS DESIRED (CLICK ON THE IMAGE LINK)

我从API获取数据并制作Android新闻应用。每个页面都有X个新闻文章。让我们说每页10篇文章。所以我使用ListView和ArrayAdapter来显示第1页上的所有文章。我想在listView下面的第10篇文章中说“更多”或类似的内容,它会加载包含下一篇10篇文章的下一页。现在我正在使用UI,而按钮没有显示在listView下面。

我尝试了这些方案:

  1. Layout_alignparentBotton:使用layout_alignParentBottom按钮停留在parentBottom,但列表是可滚动的,并且在按钮固定但是列表可滚动时位于按钮下方。预期成果

  2. layout_below:我尝试了layout_below并给出了列表视图的id。然后按钮根本不显示。我不知道为什么。我想知道这是可能的解决方案,但我想我做错了。

  3. 这是我的XML:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    
    
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    
    
        <Button
            android:id="@+id/moreButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/list"
            android:text="More" />
    
    </RelativeLayout>
    

    这就是使listView有用的原因(List的单个项目的XML):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/customListViewID"
            android:background="@drawable/background_shape_adapter"
            android:orientation="horizontal">
    
            <ImageView
                android:id="@+id/newsThumbnailImage"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerCrop" />
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:orientation="vertical"
                android:paddingStart="10dp"
                android:paddingTop="10dp">
    
                <TextView
                    android:id="@+id/titleNewsTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxLines="3"
                    android:text="Title Of News"
                    android:textAppearance="@android:style/TextAppearance.DeviceDefault.Small"
                    android:textColor="#000" />
    
                <TextView
                    android:id="@+id/newsSnippetTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxLines="2"
                    android:text="Content Snippet"
                    android:textAppearance="@android:style/TextAppearance.DeviceDefault.Small.Inverse"
                    android:textColor="@color/black_overlay" />
            </LinearLayout>
        </LinearLayout>
    
    
    </LinearLayout>
    

2 个答案:

答案 0 :(得分:1)

使用带有alignParentBottom的按钮并将其设置为不可见,当列表视图到达结尾时,显示它。

private int preLast;
// Initialization stuff.
yourListView.setOnScrollListener(this);

// ... ... ...

@Override
public void onScroll(AbsListView lw, final int firstVisibleItem,
    final int visibleItemCount, final int totalItemCount)
{

switch(lw.getId()) 
{
    case R.id.your_list_id:     

        // Make your calculation stuff here. You have all your
        // needed info from the parameters of this function.

        // Sample calculation to determine if the last 
        // item is fully visible.
        final int lastItem = firstVisibleItem + visibleItemCount;

        if(lastItem == totalItemCount)
        {
            if(preLast!=lastItem)
            {
                bottomButton.setVisibility(View.VISIBLE);
                preLast = lastItem;
            }
        }
}
}

答案 1 :(得分:0)

尝试使用垂直方向的LinearLayout而不是RelativeLayout(在第一个XML文件中)。

相关问题