Android Recycleview-显示底部的第一个项目,并从下到上滚动下一个项目

时间:2017-04-20 08:17:57

标签: android scroll android-recyclerview material-design

我有几个项目的recyclerview。我需要显示从底部开始的项目,即第一项应显示在底部,从底部到顶部滚动后,将显示更多项目。 为此,我尝试了,

linearlayoutManager.setStackFromEnd(true);

这有助于从底部开始这些项目。但是当有多个项目,超过同时适合屏幕的大小时,随着项目数量的增加,第一个项目会在顶部移动。

我也试过

linearlayoutManager.scrollToPosition(0);

这没有改变任何事情。

这是一张显示我正在寻找的图片。

enter image description here

上面列表中还有一些项目仍然不可见。

这里看起来如何滚动这些项目, enter image description here

我已在图像中检查了此视图的视图层次结构,该图像仅显示了Recyclerview及其中的项目。而recyclerview的高度是match_parent,即顶部空间,没有项目显示另一个视图在recyclerview下面是google地图。 我只需要知道如何在不使用更多额外视图的情况下实现recyclerview的这一功能。

任何帮助都将非常感谢.. !!

7 个答案:

答案 0 :(得分:3)

尝试初始化线性布局,如下所示

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, true);

third boolean表示它应该反转这些项目。 但请记住,您的数据也应该从最新到最旧排序。

答案 1 :(得分:1)

尝试使用反向LinearLayoutManager

LinearLayoutManager linearlayoutManager = new LinearLayoutManager(context, VERTICAL, true);

答案 2 :(得分:1)

将这些属性添加到recyclerview(XML)

android:paddingTop="500dp"          
android:clipToPadding="false"

你也可以在代码中设置它:

recyclerView.setPadding(0, 500, 0, 0);
recyclerView.setClipToPadding(false);

这适用于不同的屏幕尺寸

int height = recyclerView.getHeight()
recyclerView.setPadding(0, height - 200, 0, 0);
recyclerView.setClipToPadding(false);

如果你不知道如何获得屏幕宽度,高度并将dp转换为px

以下是链接:How to get screen width and heightConverting pixels to dp

答案 3 :(得分:0)

将此行写入xml以从底部开始列表

 android:stackFromBottom="true"

 <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stackFromBottom="true">

    </android.support.v7.widget.RecyclerView>

然后在添加适配器

之前反转你的数组列表

答案 4 :(得分:0)

看起来你想要的是在你的Recycler适配器中有一个单独的视图类型,它不会显示任何东西,只有地图的高度。这将允许您从列表开始的底部“向上滚动”并继续查看不同的视图。

一个简单的例子:

public class ScreenBottomRecyclerAdapter extends RecyclerView.Adapter {
    final int VIEW_TYPE_MAP = 0;
    final int VIEW_TYPE_ROW = 1;

    @Override
    public int getItemCount() {
        return dataset.length + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEW_TYPE_MAP;
        } else {
            return VIEW_TYPE_ROW;
        }
    }

    @Override
    public RecyclerView.ViewHolder createViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_MAP: return new ViewHolder(null); // This is an empty recycler row so you can see and interact with the map, it will need to be the height of the map so you'll probably want to make a new view type that the same height but doesn't show anything and doesn't intercept touches
            case VIEW_TYPE_ROW: return new RowViewHolder(someInflatedView); // This will be the rows you are already creating
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_ROW: holder.update(position - 1) // Do your normal update here, but remember that your data will be off by 1 for the map view row in the recycler
        }
    }
}

答案 5 :(得分:0)

fig = plt.figure()
plt.plot([2,5,1,2]
fig.text(.5, .05, "text", ha="center")
plt.show()

然后填充数据后

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true);
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setReverseLayout(true);

答案 6 :(得分:0)

使用以下代码反向列出:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false);

重要:将具有属性反向布局的布局管理器设置为true

layoutManager.setReverseLayout(true);

RecyclerView.setLayoutManager(layoutManager);

此代码将显示您可能已分配给RecyclerView的反向列表