无法在列表视图下方显示按钮
Unable to show button below a list view
PROBLEM DESCRIPTION IMAGE. THIS IS WHAT IS DESIRED (CLICK ON THE IMAGE LINK)
我正在从 API 获取数据并制作 Android 新闻应用程序。每个页面都有 X 篇新闻文章。让我们说每页 10 篇文章。所以我使用 ListView 和 ArrayAdapter 来显示第一页上的所有文章。我想要在 listView 下面的第 10 篇文章的末尾显示 "MORE" 或类似的内容,它会加载包含下 10 篇文章的下一页。现在我正在处理 UI 并且按钮没有显示在 listView 下方。
我尝试了这些场景:
Layout_alignparentBotton:按钮使用 layout_alignParentBottom 卡在 parentBottom 但列表是可滚动的并且在按钮下方,因为按钮是固定的但列表是可滚动的。预期结果
layout_below: 我试了layout_below,给了列表视图的id。然后按钮根本不显示。我不知道为什么。我想知道这是可能的解决方案,但我想我做错了什么。
这是我的 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>
如果这有帮助(列表的单个项目的 XML),这就是 listView 的组成部分:
<?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>
尝试使用垂直方向的 LinearLayout 而不是 RelativeLayout(在您的第一个 XML 文件中)。
将您的按钮与 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;
}
}
}
}
PROBLEM DESCRIPTION IMAGE. THIS IS WHAT IS DESIRED (CLICK ON THE IMAGE LINK)
我正在从 API 获取数据并制作 Android 新闻应用程序。每个页面都有 X 篇新闻文章。让我们说每页 10 篇文章。所以我使用 ListView 和 ArrayAdapter 来显示第一页上的所有文章。我想要在 listView 下面的第 10 篇文章的末尾显示 "MORE" 或类似的内容,它会加载包含下 10 篇文章的下一页。现在我正在处理 UI 并且按钮没有显示在 listView 下方。
我尝试了这些场景:
Layout_alignparentBotton:按钮使用 layout_alignParentBottom 卡在 parentBottom 但列表是可滚动的并且在按钮下方,因为按钮是固定的但列表是可滚动的。预期结果
layout_below: 我试了layout_below,给了列表视图的id。然后按钮根本不显示。我不知道为什么。我想知道这是可能的解决方案,但我想我做错了什么。
这是我的 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>
如果这有帮助(列表的单个项目的 XML),这就是 listView 的组成部分:
<?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>
尝试使用垂直方向的 LinearLayout 而不是 RelativeLayout(在您的第一个 XML 文件中)。
将您的按钮与 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;
}
}
}
}