当我打电话给recyclerView.smoothScrollToPosition
时,我发现了意想不到的行为。
当我添加足够的项目并开始离开屏幕时,它没有正确滚动到底部,保持2-3落后。然后,在添加15左右后,当我添加项目时它开始滚动到位置0,然后下一个项目滚动到底部。添加另一个项目,它将变为0,然后再次进入底部。
您可以在附带的视频中看到发生了什么。一旦它开始离开屏幕,请注意滚动条。
Gif on imgur,但限于15秒。 http://i.imgur.com/FnjsTY1.gifv]
整个视频可在此处获取 - https://www.dropbox.com/s/k6aaca5ue6lkk21/device-2016-05-27-130928.mp4?dl=0
我将我的应用程序拆开,直到我将其缩小为两个突然解决测试应用中问题的事情。
我的活动布局使用布局权重,当我删除它时,它开始再次正确滚动。另外,我在recylerView中显示的CardView在它周围有一个额外的LinearLayout。删除它也可以解决问题。
这些解决方案都不适用于我的应用,因为可能更多"陷阱"问题就像这两个。这也是我把它简化为显示错误的最小数量,我的布局要复杂得多。
我不知道该怎么做,因为我甚至不确定为什么它错了。我已经尝试过使用自定义滚动条的自定义线性布局管理器,但它始终存在同样的问题。
以下是用于重新创建问题的代码
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFragment fragment = new MyFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment).commit();
}
}
使用RecyclerView的MyFragment
public class MyFragment extends android.support.v4.app.Fragment {
RecyclerView recyclerView;
RecyclerAdapter adapter;
ViewGroup rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.fragmentlayout, container, false);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView = (RecyclerView) rootView.findViewById(R.id.inventory_recycler);
recyclerView.setLayoutManager(llm);
adapter = new RecyclerAdapter(new ArrayList<RecyclerItem>());
recyclerView.setAdapter(adapter);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addItem();
}
});
return rootView;
}
public void addItem(){
adapter.getList().add(new RecyclerItem());
adapter.notifyItemAdded(adapter.getList().size()-1);
recyclerView.smoothScrollToPosition(adapter.getList().size()-1);
}
public class RecyclerItem {
public RecyclerItem(){
}
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView name;
public RecyclerViewHolder(View v) {
super(v);
name = (TextView) v.findViewById(R.id.name);
}
}
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
private final List<RecyclerItem> items;
public RecyclerAdapter(List<RecyclerItem> items) {
this.items = items;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public void onBindViewHolder(final RecyclerViewHolder viewHolder, final int position) {
viewHolder.name.setText("" + position);
}
public List<RecyclerItem> getList() {
return items;
}
public void notifyItemAdded(int position) {
notifyItemInserted(position);
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.card, viewGroup, false);
return new RecyclerViewHolder(itemView);
}
}
}
活动布局(删除布局权重和设置值可解决问题)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"/>
</LinearLayout>
片段布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="add item" />
<android.support.v7.widget.RecyclerView
android:id="@+id/inventory_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/button"
android:scrollbars="vertical" />
</RelativeLayout>
卡片布局(删除额外的LinearLayout解决了问题)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="140dp"
android:layout_margin="4dp"
app:elevation="3dp"
card_view:cardCornerRadius="2dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
摇篮
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.sample.recyclertest"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
}
答案 0 :(得分:4)
执行此操作的一个直接解决方案是使用recyclerview-v7:23.1.1而不是recyclerview-v7:23.4.0。
另一种解决方案是使用此方法
/**
* RecyclerView can perform several optimizations if it can know in advance that RecyclerView's
* size is not affected by the adapter contents. RecyclerView can still change its size based
* on other factors (e.g. its parent's size) but this size calculation cannot depend on the
* size of its children or contents of its adapter (except the number of items in the adapter).
* <p>
* If your use of RecyclerView falls into this category, set this to {@code true}. It will allow
* RecyclerView to avoid invalidating the whole layout when its adapter contents change.
*
* @param hasFixedSize true if adapter changes cannot affect the size of the RecyclerView.
*/
public void setHasFixedSize(boolean hasFixedSize)
recyclerView.setHasFixedSize(true);
应该可以解决您的问题。为23.4.0
您的代码存在的问题是,无论何时调用notifyItemInserted(),都会再次绘制整个Recyclerview,这会将您的recyclerview移动到第一个位置。最重要的是,你正在调用recyclerView.smoothScrollToPosition(adapter.getList().size() - 1);
,这意味着视图将在顶部以及同时滚动到最后一个位置。这就是为什么它没有走到最底层的原因。所以只需致电recyclerView.setHasFixedSize(true);
以防止重绘。