ListView、API 个调用和 onScrollListener

ListView, API calls and onScrollListener

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

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

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

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

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



额外信息

我在 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。此数据由自定义 class 检索并存储在列表中,然后该列表作为参数传递给我用于设置此类适配器的方法。

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;
    }
}

如果我没理解错你想:

  1. 在屏幕底部添加一个 Prev/Next 按钮

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

第一个可以使用 RelativeLayout and for the second you should use 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 显示给定数量的项目 (e.x.10) 并预加载一些下一个和上一个项目。它是无限列表的理想选择。如果您想动态地向其中添加项目,您需要在适配器中通过将项目添加到列表并调用 notifyItemInserted(int position) 方法来实现。