【问题标题】:Android recyclerview scroll to topAndroid recyclerview 滚动到顶部
【发布时间】:2017-01-14 22:06:16
【问题描述】:

这是我处理适配器、LinearLayoutManager 等的 onCreate。 我尝试了每一个选项来尝试将其滚动到顶部,但不能。 我什至尝试创建自己的自定义 LinearLayoutManager。

//Reson for static to call from a static method to scroll it up
private static RecyclerView taskRecyclerView;
private FirebaseAdapter firebaseAdapter;
private static LinearLayoutManager linearLayoutManager;

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.fragment_tasklist, container, false);



    linearLayoutManager = new LinearLayoutManager(getActivity());

    taskRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_mainTask);
    databaseRef = FirebaseDatabase.getInstance().getReference().child(userAccount.getId());


    firebaseAdapter = new FirebaseAdapter(TaskObject.class, R.layout.one_task, TaskViewHolder.class, databaseRef.child("Task"), getContext());

    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);
    linearLayoutManager.setSmoothScrollbarEnabled(true);


    taskRecyclerView.setAdapter(firebaseAdapter);
    taskRecyclerView.setLayoutManager(linearLayoutManager);
    relativeLayoutAdd.setOnClickListener(this);
    //Listen for any data change
    databaseRef.child("Task").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //onDataChange called so remove progress bar

            //make a call to dataSnapshot.hasChildren() and based
            //on returned value show/hide empty view

            //use helper method to add an Observer to RecyclerView

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    databaseRef.child("Task").keepSynced(true);
    return view;
}

【问题讨论】:

  • 你为什么要逆转你的法学硕士?每次在 recyclerview 底部添加新内容时,您想滚动到底部吗?喜欢消息应用?
  • 我希望最新的始终位于顶部。这就是我反转它的原因。
  • 你能用 UI 展示一个例子吗?如果您想将项目添加到回收站视图的顶部,则不必反转 LLM,您必须反转 Array。如果可以的话,用图片解释一下。我不确定你的问题是什么。
  • 我想在每次添加项目时滚动到顶部,以便最新的项目位于顶部,而较旧的项目位于底部。
  • linearLayoutManager.setReverseLayout(true); 这会导致问题吗?

标签: java android


【解决方案1】:

你试过taskRecyclerView.getLayoutManager().scrollToPosition(0)taskRecyclerView.scrollToPosition(your_position)吗?

RecyclerView 在taskRecyclerView.scrollToPosition(your_position) 中没有实现任何滚动逻辑,taskRecyclerView.getLayoutManager().scrollToPosition(0) 更具体,它会根据实现的布局进入特定的索引,在你的情况下是线性的。

【讨论】:

  • 试过taskRecyclerView.scrollToPosition(your_position)taskRecyclerView.getLayoutManager().scrollToPosition(0) 这个工作。你能解释一下为什么吗?
  • RecyclerView 没有在taskRecyclerView.scrollToPosition(your_position) 中实现任何滚动逻辑,taskRecyclerView.getLayoutManager().scrollToPosition(0) 更具体,它会根据实现的布局进入特定的索引,在你的情况下是线性的。
  • 我明白了。非常感谢!
【解决方案2】:

使用 RecyclerView 滚动到特定项目是一个棘手的部分,但下面的代码有助于滚动

平滑滚动的方法

fun Context.getSmoothScroll(): LinearSmoothScroller {
        return object : LinearSmoothScroller(this) {
            override fun getVerticalSnapPreference(): Int {
                return LinearSmoothScroller.SNAP_TO_START
            }
        }
    }

对 RecyclerView 应用平滑滚动

fun RecyclerView.scrollToPositionSmooth(int: Int) {
        this.layoutManager?.startSmoothScroll(this.context.getSmoothScroll().apply {
            targetPosition = int
        })
    }

对 RecyclerView 应用无平滑滚动(快速)

fun RecyclerView.scrollToPositionFast(position: Int) {
        this.layoutManager?.scrollToPosition(position)
    }

要使用的代码

itemFilteredRecyclerView.scrollToPositionFast(0) //Normal Scroll
itemFilteredRecyclerView.scrollToPositionSmooth(0) //Smooth Scroll

额外信息: 您可以将 ..(0) 替换为需要滚动的位置。

完整代码:Link

【讨论】:

    【解决方案3】:

    添加下面的代码也可以。

    linearLayoutManager.setStackFromEnd(false);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      相关资源
      最近更新 更多