gpt4 book ai didi

android - 回收站 View : scrollToPosition not working

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:31 25 4
gpt4 key购买 nike

我有一个显示用户短信的简单项目。我目前无法将 RecyclerView 滚动到最后收到的短信。
我的 recyclerView 在一个使用 Coordinator 布局的非常简单的 Activity 中:

<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
android:background="@color/background"
tools:context="com.myproject.myproject.MessageActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_message" />
</android.support.design.widget.CoordinatorLayout>

和 content_message.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"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.textbutler.textbutler.MessageActivity"
tools:showIn="@layout/activity_message">

<!-- some views here -->

<android.support.v7.widget.RecyclerView
android:id="@+id/content_text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical" />

<!-- some views here -->
</LinearLayout>

Activity 通过基本加载程序加载数据。我强制访问 UI 线程上的 recyclerview 并延迟(以防万一)

public class MessageActivity extends AppCompatActivity {
private static final int URL_LOADER = 0;

private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private MessageAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

initializeList(savedInstanceState);
}

private void initializeList(Bundle savedInstanceState) {
if (recyclerView == null) {
recyclerView = (RecyclerView) findViewById(R.id.content_text);

if (recyclerView == null)
return;

if (layoutManager == null) {
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
}

recyclerView.setLayoutManager(layoutManager);

final Activity me = this;

if (adapter == null) {
adapter = new MessageAdapter(new ObservableCallback() {
@Override
public void callback() {
// this function is called right after the recyclerview received notifyDataSetChanged
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

me.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("ScrollingUI", "scrolltoposition " + adapter.getItemCount());
recyclerView.scrollToPosition(adapter.getItemCount() - 1);
}
});
}
}, 1000);
}
});
SMSByUserLoader loader = new SMSByUserLoader(this, adapter);
getSupportLoaderManager().initLoader(URL_LOADER, savedInstanceState, loader);
}

recyclerView.setAdapter(adapter);
}
}

当我运行这段代码时,RecyclerView 被完美填充。一秒钟后,我有了正确的日志,但 View 没有改变。我已经找到了这个问题的各种答案,但它们似乎都不适用于我的情况。
在 RecyclerView 中滚动非常有效。

最佳答案

我有同样的问题,smoothScrollToPosition 方法解决了它:

 RecyclerView.smoothScrollToPosition(postion);

关于android - 回收站 View : scrollToPosition not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37644217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com