标签: linearlayoutmanager

如何滚动到scrollview内的recyclerview中的特定位置?

我已经搜索了很多关于我的查询,但没有一个是有用的.我在一个滚动视图中有一个recyclerview和一些静态数据,它位于root parentlayout中,如下所示.

我已经设定 -

scrollview.scrollto(0,0);
Run Code Online (Sandbox Code Playgroud)

因为每当我打开活动时它会跳转到recyclerview firstitem并且它会跳过recyclerview上方的静态数据.

recyclerview.setNestedScrollingEnabled(false); 
recyclerview.setfocusable(false);
Run Code Online (Sandbox Code Playgroud)

对于smoothscroll.

问题是 -

layoutmanager.scrollToPositionWithOffset(pos,0);
Run Code Online (Sandbox Code Playgroud)

它不起作用.我在将适配器设置为recyclerview后设置了aboveline.也尝试使用NestedScrollView但是徒劳无功.

虽然我用过

layoutmanager.scrollToPosition(pos);
Run Code Online (Sandbox Code Playgroud)

对于那些跳过代码的人,我已将match_parent设置为ScrollView,并将fillviewport设置为true.

这是我的布局.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:id="@+id/bottomsheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.inkdrops.khaalijeb.BrandCouponActivity">

    <RelativeLayout
        android:id="@+id/inclusionviewgroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <static data/>

        <ScrollView
            android:id="@+id/scrollviewmain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/one"
            android:fillViewport="true"
            android:scrollbars="none"
            android:layout_above="@+id/donelayout">


            <staticdata/>

                <TextView
                    android:id="@+id/dealstext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/card1"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="10dp"
                    android:textSize="16sp"
                    android:text="Deals &amp; Coupons"
                    android:textColor="#444444" />

                <RelativeLayout
                    android:id="@+id/recyclerlayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/dealstext"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:background="@color/colorbackground">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/coupon_rec_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/colorbackground"
                        android:visibility="visible"/>


                </RelativeLayout>

                <statisdata>


            </RelativeLayout>

        </ScrollView>

        include layout="@layout/activity_coupons"
            android:visibility="gone"
            /> …
Run Code Online (Sandbox Code Playgroud)

android scrollview android-recyclerview linearlayoutmanager android-nestedscrollview

29
推荐指数
1
解决办法
7747
查看次数

RecyclerView - 滚动到每次都不起作用的位置

我已经实现了水平滚动RecyclerView.我RecyclerView使用了LinearLayoutManager,而我面临的问题是,当我尝试使用scrollToPosition(position)smoothScrollToPosition(position)LinearLayoutManagerscrollToPositionWithOffset(position).对我来说都不适用.滚动调用不会滚动到所需位置,也不会调用OnScrollListener.

到目前为止,我已经尝试了很多不同的代码组合,我不能在这里发布它们.以下是对我有用的(但只是部分):

public void smoothUserScrollTo(final int position) {

    if (position < 0 || position > getAdapter().getItemCount()) {
        Log.e(TAG, "An attempt to scroll out of adapter size has been stopped.");
        return;
    }

    if (getLayoutManager() == null) {
        Log.e(TAG, "Cannot scroll to position a LayoutManager is not set. " +
                "Call setLayoutManager with a non-null layout.");
        return;
    }

    if (getChildAdapterPosition(getCenterView()) == position) {
        return;
    }

    stopScroll(); …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview linearlayoutmanager

26
推荐指数
7
解决办法
3万
查看次数

RecyclerView smoothScroll位于中心位置.安卓

我正在使用水平布局管理器RecyclerView.我需要以下RecyclerView一种方式制作:当点击某个项目时 - 将smoothScrool放到该位置并将该项目放在中心RecyclerView(如果可能的话,例如20中的10项).

所以,我没有问题smoothScrollToPosition(),但如何把项目放在RecyclerView??? 的中心?

谢谢!

android android-recyclerview linearlayoutmanager

24
推荐指数
3
解决办法
2万
查看次数

RecyclerView在插入时滚动

我想用来RecyclerView创建一个聊天应用程序.我使用的是LinearLayoutManagersetReverseLayout(true).

当我一直滚动到底部(这是数据集start =最新消息)并且新消息被插入到数据集中时,该项目按预期显示在列表的底部(视图向上滚动以腾出空间)对于新项目).

我遇到的问题是当我向上滚动以查看较旧的消息时.当新消息插入到数据集的开头时,视图向上滚动大约一个消息高度,即使该消息甚至没有呈现,因为它超出了视口的范围.

当视图滚动到底部时,如何保持滚动行为,但在滚动到较旧的消息时禁用它?

更新: 我还制作了一个小应用程序,重现了这个问题:https: //github.com/ehehhh/RecyclerViewProblem

更新2:我提交的修复工作也适用于我制作的回购.

相关代码(希望如此):

compile 'com.android.support:recyclerview-v7:24.2.0'
Run Code Online (Sandbox Code Playgroud)

XML:

<android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="8dp"
            android:paddingTop="8dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
Run Code Online (Sandbox Code Playgroud)

RecyclerView初始化代码:

layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setScrollContainer(true);
recyclerView.setLayoutAnimation(null);
recyclerView.setItemAnimator(null);
adapter = new ChatAdapter(...);
recyclerView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

适配器:

public class ChatAdapter extends RecyclerView.Adapter<ChatViewHolder> {

    private List<MessageWrapper> dataset;

    public ChatAdapter(List<MessageWrapper> dataset, ...) {
        this.dataset = dataset;
        setHasStableIds(true);
    }

    ...

    @Override
    public long getItemId(int position) {
        return dataset.get(position).getId();
    }

    @Override …
Run Code Online (Sandbox Code Playgroud)

reverse android chat android-recyclerview linearlayoutmanager

18
推荐指数
1
解决办法
8606
查看次数

RecyclerView在聊天屏幕中的notifyDataSetChanged上滚动到顶部

我正在尝试使用recyclelerView创建消息类型的屏幕,它将从底部开始,并在用户到达聊天的最高端时加载更多数据.但我正面临着这个奇怪的问题.

我的recyclerView在调用notifyDataSetChanged时滚动到顶部.由于这个onLoadMore被多次调用.

这是我的代码:

LinearLayoutManager llm = new LinearLayoutManager(context);
llm.setOrientation(LinearLayoutManager.VERTICAL);
llm.setStackFromEnd(true);
recyclerView.setLayoutManager(llm);
Run Code Online (Sandbox Code Playgroud)

**在适配器中

 @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (messages.size() > 8 && position == 0 && null != mLoadMoreCallbacks) {
        mLoadMoreCallbacks.onLoadMore();
    }
Run Code Online (Sandbox Code Playgroud)

**在活动中

@Override
public void onLoadMore() {
    // Get data from database and add into arrayList
      chatMessagesAdapter.notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)

这只是recyclerView滚动到顶部.如果滚动到顶部停止,则此问题将得到解决.请帮我弄清楚这个问题的原因.提前致谢.

android notifydatasetchanged recycler-adapter android-recyclerview linearlayoutmanager

14
推荐指数
2
解决办法
8027
查看次数

LinearLayoutManager无法解析getOrientation()

我有一个RecyclerView片段实现,我试图在代码中添加分隔符.我RecylcerView按预期工作,但LinearLayoutManager无法解决getOrientation()功能.

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View root = (View) inflater.inflate(R.layout.fragment_settings, null);
    getActivity().setTitle("Settings");

    mRecyclerView = (RecyclerView) root.findViewById(R.id.recycler_view_settings);

    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new LinearLayoutManager(getContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new SettingsAdapter(getResources().getStringArray(R.array.setting_list));
    mRecyclerView.setAdapter(mAdapter);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(), mLayoutManager.getOrientation());
    mRecyclerView.addItemDecoration(dividerItemDecoration);
    return root;
}
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview linearlayoutmanager

13
推荐指数
2
解决办法
4250
查看次数

RecyclerView在添加新项目时滚动到位置

当新项目添加到列表时,我希望我的RecyclerView滚动到底部.以下是我的代码:

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);

adapter = new RecyclerViewAdapter(data, recyclerView);
recyclerView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

当我点击下面的按钮时RecyclerView,数据会添加到列表中:

public void addNewCard() {
    data.add("New");
    adapter.notifyItemInserted(data.size() - 1);
    recyclerView.scrollToPosition(data.size() - 1);
}
Run Code Online (Sandbox Code Playgroud)

每当我添加一个项目时,它总是会滚动到最顶端的位置.我尝试使用smoothScrollToPosition(),并尝试使用scrollToPosition()LinearLayoutManager了.我甚至尝试过使用ViewTreeObserver.尝试更改RecyclerView依赖项的版本.我在Stack Overflow上进行了彻底搜索,没有一个解决方案适合我.

关于什么可能是问题的任何想法?我在RecyclerView片段里面使用.这可能是问题的原因吗?

我的.xml文件 RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll_view_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/rel_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp" />

    <Button
        android:id="@+id/button_add_new_card"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/listView"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="8dp"
        android:padding="20dp"
        android:text="+ Add new Item" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview linearlayoutmanager

13
推荐指数
3
解决办法
2万
查看次数

使用 RecyclerView 和 LinearLayoutManager 使用 reverseLayout == true 的奇怪行为(错误?)

当使用带有 LinearLayoutManager 和“reverseLayout”标志设置为 true 的 RecyclerView 时,当通过notifyItemChanged它通知任何项目时,也会调用onBindViewHolder第一个不可见的项目。之后它不会要求onViewRecycled该项目。因此,如果 ViewHolder 在 onBind 中进行某种订阅,它将永远不会被释放,因为不会调用 onRecycle。

这实际上看起来像是LinearLayoutManager. 如果您查看fillLinearLayoutManager中的方法,则有以下代码:

if (!layoutChunkResult.mIgnoreConsumed || layoutState.mScrapList != null || !state.isPreLayout()) {
    layoutState.mAvailable -= layoutChunkResult.mConsumed;
    // we keep a separate remaining space because mAvailable is important for recycling
    remainingSpace -= layoutChunkResult.mConsumed;
}
Run Code Online (Sandbox Code Playgroud)

据我所知,我们迭代子视图,直到我们填满所有需要的空间,换句话说layoutState.mAvailableremainingSpace它们都以像素为单位。如果您进一步layoutChunk查看方法中发生的事情,您将看到这段代码:


// Consume the available space if the view is not removed OR changed
if (params.isItemRemoved() || params.isItemChanged()) {
    result.mIgnoreConsumed = …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview linearlayoutmanager

10
推荐指数
0
解决办法
424
查看次数

查找RecyclerView中第一个可见项目的顶部偏移量

我有一个RecyclerView + LinearLayoutManger,它使用一个包含聊天消息的适配器.我将聊天消息的数量限制在最近的100个.这个问题是,当我删除旧的聊天时,recyclerview中聊天的滚动位置会发生变化,因为索引0已被删除.我开始编写下面的代码:

int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
View v = layoutManager.getChildAt(firstVisiblePosition);
if (firstVisiblePosition > 0 && v != null) {
    int offsetTop = //need to get the view offset here;
    chatAdapter.notifyDataSetChanged();

    if (firstVisiblePosition - 1 >= 0 && chatAdapter.getItemCount() > 0) {
        layoutManager.scrollToPositionWithOffset(firstVisiblePosition - 1, offsetTop);
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为获得第一个可见项目位置的可见偏移很容易.防爆.如果第一个可见视图是300dp但只有最后200dp是可见的,我想获得100偏移量.

这样我就可以使用scrollToPositionWithOffset(firstVisiblePosition - 1,offsetTop).

我在这里错过了什么吗?这似乎是一个很容易解决的问题,但我还没有看到任何方法可以支持这一点.

android android-recyclerview linearlayoutmanager

9
推荐指数
1
解决办法
2万
查看次数

app:stackFromEnd for RecyclerView无法在xml中运行?

我有一个RecyclerView定义为:

    <android.support.v7.widget.RecyclerView
      android:id="@+id/message_list"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_above="@id/message_input"
      android:layout_alignParentTop="true"
      app:stackFromEnd="true" />
Run Code Online (Sandbox Code Playgroud)

相关代码也常用于:

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    //layoutManager.setStackFromEnd(true);
    mRecyclerView.setLayoutManager(layoutManager);
Run Code Online (Sandbox Code Playgroud)

但是,当我添加一个项目时RecyclerView,它不尊重app:stackFromEnd="true".另一方面,如果我以layoutManager.setStackFromEnd(true);编程方式取消注释并使用它,它可以正常工作.我错过了什么问题?欢迎任何想法.

android android-layout android-recyclerview linearlayoutmanager

9
推荐指数
1
解决办法
5163
查看次数