首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListView、API调用和onScrollListener

ListView、API调用和onScrollListener
EN

Stack Overflow用户
提问于 2017-09-29 11:09:02
回答 1查看 60关注 0票数 0

我正在为一个在线博客开发一个Android应用程序,它可以从它的API中检索数据(公司制作它是为了让我可以在它中使用它,这样它就可以被修改)。

这个应用程序显示了一个加载了ListView的n博客条目。问题是,在过去的三天里,我一直在寻找一种方法,在said ListView的底部添加一个前一个/Next按钮,最终放弃并尝试另一种方式。

我见过应用程序更新并将从服务器中提取的内容附加到列表中(不确定是哪种类型),而用户则在上面上下滚动。

这个是可能的吗?如果是的话,如何才能做到呢?

任何透露的信息,例子(尽管它可能是简单的)或帮助将不胜感激!

附加信息

我正在将ListView加载到LinearLineout中,这是在onCreate方法中调用的。

content_main.xml

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

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-29 12:33:19

如果我理解正确,你想:

  1. 在屏幕底部添加Prev/Next按钮
  2. 您有无穷无尽的内容要动态显示。

使用RelativeLayout可以很容易地实现第一个目标,而对于第二个步骤,您应该使用RecyclerView

对于您的布局,您可以使用如下所示:

代码语言:javascript
复制
<?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)方法来做到这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46487584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档