Java LinearLayoutManager setReverseLayout()=true,但项目从底部堆叠
这似乎是一个简单的解决方案,但似乎Java LinearLayoutManager setReverseLayout()=true,但项目从底部堆叠,java,android,android-recyclerview,reverse,layout-manager,Java,Android,Android Recyclerview,Reverse,Layout Manager,这似乎是一个简单的解决方案,但似乎 private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private LinearLayoutManager mLayoutManager; .... // More code mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view); // Add ite
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager mLayoutManager;
.... // More code
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
// Add item decoration
mRecyclerView.addItemDecoration(new SpacesItemDecoration(DIVIDER_SPACE));
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true); // THIS ALSO SETS setStackFromBottom to true
mRecyclerView.setLayoutManager(mLayoutManager);
似乎还将项目设置为从底部堆叠
我试图将setStackFromBottom
设置为false,但这没有任何作用,什么是颠倒项目顺序但仍然从顶部填充的最佳方法?我应该改用自定义比较器类吗?我希望这是一个比创建另一个类更简单的过程。来自
用于反转项目遍历和布局顺序。其行为类似于RTL视图的布局更改。当设置为true时,第一个项目将在UI的末尾进行布局,第二个项目将在其之前进行布局,等等。对于水平布局,这取决于布局方向。当设置为true时,如果RecyclerView为LTR,则它将从RTL布局,如果RecyclerView}为RTL,则它将从LTR布局。如果您要查找与setStackFromBottom(布尔值)
完全相同的行为,请使用setStackFromEnd(布尔值)
因此,请尝试在LinearLayoutManager实例上使用
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
被接受的答案很有效,我经历了一个艰难的过程,因为我得到了
无法解决方法setReverseLayout
错误
然后在寻找解决方案后,我发现有一个愚蠢的错误。我使用的是RecyclerView.LayoutManager
而不是LinearLayoutManager
所以我想,为了消除这里的困惑,我需要把它作为一个答案
不要使用RecyclerView.LayoutManager
而不是LinearLayoutManager
// Declare your RecyclerView and the LinearLayoutManager like this
private RecyclerView mRecyclerView;
private LinearLayoutManager mLayoutManager;
我找到了使用xml解决此问题的有效方法:
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:stackFromEnd="true"
我正在使用一个水平方向的回收视图,由于未知原因,当我尝试使用
setReverseLayout(true)
反转布局时,接受的答案对我无效
但是,通过XML实现这一点效果很好
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"
android:orientation="horizontal"
app:reverseLayout="true"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
我有这个错误,我的工作输入为false
recyclerView = vista.findViewById(R.id.recyclerView);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(false);
mLayoutManager.setStackFromEnd(false);
recyclerView.setLayoutManager(mLayoutManager);
要在我的应用程序中搜索,请搜索名称
Query busqueda = databaseReference.child("Productos").orderByChild("pTitle").startAt(txtSearch.getText().toString());
busqueda.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postModelList.clear();
for(DataSnapshot ds: dataSnapshot.getChildren()){
PostModel postModel = ds.getValue(PostModel.class);
postModelList.add(postModel);
postAdapter = new PostAdapter(getContext(), postModelList);
recyclerView.setAdapter(postAdapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
干得好,这很简单:)谢谢,分别尝试了这两种方法,甚至没有想过一起使用它们来获得结果:)我得到了
无法解决方法setReverseLayout
错误。@ReazMurshed检查您的sdk版本(分钟和目标)和依赖项(回收器视图支持库)用于您的项目。感谢您的评论。我的问题很愚蠢。我使用的是RecyclerView.LayoutManager
而不是LinearLayoutManager
。使用LinearLayoutManager而不是RecyclerView.LayoutManager有什么缺点吗?
Query busqueda = databaseReference.child("Productos").orderByChild("pTitle").startAt(txtSearch.getText().toString());
busqueda.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postModelList.clear();
for(DataSnapshot ds: dataSnapshot.getChildren()){
PostModel postModel = ds.getValue(PostModel.class);
postModelList.add(postModel);
postAdapter = new PostAdapter(getContext(), postModelList);
recyclerView.setAdapter(postAdapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});