【问题标题】:ListView, API calls and onScrollListenerListView、API 调用和 onScrollListener
【发布时间】:2017-09-29 11:09:02
【问题描述】:

我正在为一个在线博客开发一个从其 API 检索数据的 Android 应用程序(该公司制作了它,以便我可以在其中使用它,因此可以对其进行修改)。

该应用会显示一个 ListView,其中加载了 n 个博客条目。问题是我过去三天一直在寻找一种在所述 ListView 底部添加上一个/下一个按钮的方法,只是为了最终放弃并尝试另一种方法。

我看到当用户在列表上上下滚动时,应用会更新并将从服务器拉取的内容附加到列表(不确定是哪种类型)。

这可能吗?如果可以,怎么做?

任何有启发性的信息、示例(尽可能简单)或帮助将不胜感激!



额外信息

我在一个 LinearLineout 中加载 ListView,它在 onCreate 方法中被调用。

content_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.company.myApp.MainActivity"
    tools:showIn="@layout/app_bar_main"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@color/myGrey"
            android:scrollbars="none"
            android:visibility="visible" />

    </LinearLayout>

</LinearLayout>



我还使用自定义适配器用检索到的数据填充 ListView。此数据由自定义类检索并存储在 List 中,然后将该列表作为参数传递给我用来设置此类适配器的方法。

public class AdaptadorPosts extends ArrayAdapter {
    public AdaptadorPosts(Context context, List objects) {
        super(context, 0, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = convertView;

        if (null == convertView) {
            v = inflater.inflate(R.layout.lista_posts, parent, false);
        }

        TextView titulo = (TextView)v.findViewById(R.id.titulo);
        TextView resumen = (TextView)v.findViewById(R.id.resumen);
        TextView fecha = (TextView)v.findViewById(R.id.fecha);
        TextView autor = (TextView)v.findViewById(R.id.autor);

        Post item = (Post) getItem(position);

        titulo.setText(Html.fromHtml(item.getTitulo()));
        if(item.getResumen().isEmpty() || item.getResumen().equals(null)) {
            resumen.setText("¡No hay resumen!");
        } else {
            resumen.setText(Html.fromHtml(item.getResumen()));
        }
        fecha.setText(Html.fromHtml(item.getFecha()));
        autor.setText(Html.fromHtml(item.getAutor().getNombre()));

        return v;
    }
}

【问题讨论】:

标签: java android api listview adapter


【解决方案1】:

如果我理解正确你想:

  1. 在屏幕底部添加上一个/下一个按钮

  2. 您有无穷无尽的内容想要动态显示

第一个可以通过使用RelativeLayout 轻松实现,第二个您应该使用RecyclerView。

对于您的布局,您可以使用如下内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/light_gray">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:scrollbars="none"
            android:visibility="visible"/>

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:src="@drawable/btn_prev"/>

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:src="@drawable/btn_next"/>

</RelativeLayout>

如果您希望您的按钮直接位于列表下方,您可以使用android:layout_below="@id/lv" 附加它们并删除android:layout_alignParentBottom="true"

Android RecyclerView 显示给定数量的项目(例如 10 个)并预加载一些下一个和上一个项目。它是无尽列表的理想选择。如果要动态添加项目,则需要在适配器中通过将项目添加到列表并调用notifyItemInserted(int position) 方法来实现。

【讨论】:

  • 谢谢!我知道我已经很久没有问这个问题了,但我实际上忘记了我在这里有一个帐户!
猜你喜欢
  • 1970-01-01
  • 2020-02-10
  • 2012-08-23
  • 2022-01-02
  • 2015-08-30
  • 1970-01-01
  • 2016-03-21
  • 1970-01-01
  • 2011-09-15
相关资源
最近更新 更多