我遇到了这个恼人的问题,即使我以前的应用程序没有遇到这样的错误,
我在RecyclerView
内有NestedScrollView
,并且正确设置了参数。查看代码,我在setAdapter
setLayoutManager
方法
正如您在代码中看到的那样,我正在添加一个空的ArrayList
,然后先设置LayoutManager
,然后再设置setNestedScrollingEnabled
到false
,最后我正在设置Adapter
。
在XML中,我将其作为wrap_content
。
尽管如此,我遇到了这个错误,之后我的应用只是FC。
代码:
if(mRecyclerView == null)
mRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_section_home_recyclerview);
mGiftsItemsList = new ArrayList<>();
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
mLinearLayoutManager.setAutoMeasureEnabled(true);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mAdapter = new AdapterFragmentSectionHome(getActivity(),mGiftsItemsList);
mRecyclerView.setNestedScrollingEnabled(false);
setOnItemClickListener(this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new ListenerInterfaceHomeClick() {
@Override
public void onItemClick(int position, View v) {
if(mGiftsItemsList.get(position).isGiveaway_has_finished()){
} else {
if(getActivity() != null){
Intent intent = new Intent(v.getContext(), ActivityGy.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intent.putExtra(Constants.GIVEAWAY_IS_ANIMATED,true);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),Pair.create(v, "cardview") );
try{
startActivity(intent,options.toBundle());
} catch (Exception e){
e.printStackTrace();
}
} else
{
try{
startActivity(intent);
} catch (Exception e){
e.printStackTrace();
}
}
}
}
}
});
RecyclerView.ItemAnimator animator = mRecyclerView.getItemAnimator();
if (animator instanceof SimpleItemAnimator) { ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false); }
布局:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_section_home_nestedscrollview"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/fragment_section_home_nestedscrollview_layout">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fragment_section_home_recyclerview"
android:scrollbars="none"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
答案 0 :(得分:0)
在我的一个项目中,我也遇到了这个但没有崩溃。
RecyclerView:没有连接适配器;跳过布局
以下是我如何定义元素(专注于回收器,即显示网格):
<强> layout.xml 强>:
<android.support.v4.widget.NestedScrollView
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- stuff here -->
<android.support.v7.widget.RecyclerView
android:id="@+id/myRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<强> Activity.java 强>:
@VisibleForTesting
RecyclerView myRecycler;
private MyAdapter myRecyclerAdapter;
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setLayout();
}
private void setLayout() {
myRecycler = findViewById(R.id.myRecycler);
ViewCompat.setNestedScrollingEnabled(myRecycler, false);
}
@Override
protected void onResume() {
super.onResume();
setLayoutValues();
}
private void setLayoutValues() {
myRecyclerAdapter = new MyAdapter();
int nbCols = 2;
GridLayoutManager layout = new GridLayoutManager(this, nbCols);
myRecycler.setLayoutManager(layout);
myRecycler.setAdapter(myRecyclerAdapter);
}
我希望你能从中激励并解决你的问题:)
答案 1 :(得分:-1)
试试这个: 您必须将布局管理器添加到RecyclerView From xml
<android.support.v7.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" >
和 从回收站视图初始化后的代码
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);