问题描述
我正在尝试在Fragment中使用RecyclerView,但是我遇到了一个问题,我的列表没有出现,并且出现此错误:
I'm trying to use RecyclerView in Fragment, but I'm facing a problem, my list doesn't appear and I have this error:
我检查了解决方案,显然问题出现在以下时间:
I checked the solutions, apparently the problems appeared when:
-
RecyclerView在适配器后初始化
RecyclerView initialized after Adapter
在onCreateView而不是onViewCreated绑定并设置recyclerView
Bind and set recyclerView in onCreateView instead of onViewCreated
但是我给予了关注,我尝试了替代方法,但仍然面临着同样的问题.
But I gave attention, I tried alternatives and I'm still facing the same issue.
这是我的片段代码:
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.gosia.weightapplication.databinding.FragmentHistoryBinding;
import com.example.gosia.weightapplication.model.WeightData;
import com.orhanobut.logger.Logger;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import java.util.ArrayList;
import java.util.List;
public class HistoryFragment extends Fragment {
private List<WeightData> mWeightData = new ArrayList<>();
private FragmentHistoryBinding mFragmentHistoryBinding;
private ListAdapter mAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, viewGroup, false);
//binding variable
mFragmentHistoryBinding = DataBindingUtil.setContentView(getActivity(), R.layout.fragment_history);
mFragmentHistoryBinding.setVariable(this);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//recycler
RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
// define an adapter
mAdapter = new ListAdapter(mWeightData, getContext());
recyclerView.setAdapter(mAdapter);
prepareWeightData();
}
private void prepareWeightData() {
mWeightData = SQLite.select().
from(WeightData.class).queryList();
Logger.d(mWeightData.toString()); //The list isn't null, I have data to show
mAdapter.notifyDataSetChanged();
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View" />
<variable
name="variable"
type="com.example.gosia.weightapplication.HistoryFragment" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack">
</View>
</LinearLayout>
</layout>
ListAdapter的代码:
Code of the ListAdapter:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.gosia.weightapplication.model.WeightData;
import java.util.List;
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
private List<WeightData> mList;
private Context mContext;
private final LayoutInflater mLayoutInflater;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView date, weight;
public ImageView image;
public ViewHolder(View v) {
super(v);
date = v.findViewById(R.id.text_view_date);
weight = v.findViewById(R.id.text_view_weight);
image = v.findViewById(R.id.image);
}
}
public ListAdapter(List<WeightData> plist, Context pContext) {
this.mList = plist;
this.mContext = pContext;
this.mLayoutInflater = LayoutInflater.from(this.mContext);
}
@Override
public ListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = this.mLayoutInflater.inflate(R.layout.item_weight_history, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//set data
WeightData weightData = mList.get(position);
holder.date.setText(weightData.getLastDayWeightMeasurement());
holder.weight.setText(weightData.getWeight());
}
@Override
public int getItemCount() {
return mList.size();
}
}
并且我的活动正确扩展了android.support.v7.app.AppCompatActivity
And my Activity is properly extending android.support.v7.app.AppCompatActivity
任何帮助将不胜感激
显然与此DataBindingUtil有关吗?
it's apparently related to this DataBindingUtil?
推荐答案
我无需绑定即可运行您的代码,并且代码运行良好.您应该检查绑定
I run your code without bindings and its working perfect. You should check your bindings
public class HistoryFragment extends Fragment {
private ListAdapter mAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, viewGroup, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//recycler
RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
// define an adapter
mAdapter = new ListAdapter(null, getContext());
recyclerView.setAdapter(mAdapter);
}
}
即使使用getContext(),它也可以工作,即使您传递空列表,它也不会在回收器视图上显示任何内容,但不会出现跳过布局错误的情况.
even with getContext() it will work and if you are passing null list though it will just not show anything on recycler view but will not give skipping layout error.
这篇关于片段中的RecyclerView:未连接适配器,跳过布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!