使用RecyclerView时出错(未连接适配器;跳过布局)

时间:2017-04-21 06:53:30

标签: android android-fragments android-recyclerview

我面临错误“RecyclerView:没有附加适配器;跳过布局”,我搜索了相关的解决方案,但错误仍然存​​在。

我尝试在onCreate()方法中初始化视图和数据,但仍然遇到了同样的错误。我已经提到RecyclerViewFragment.java from Android Developer site代码,有些库在onCreateView()方法中初始化视图。

活动的布局包含用于切换片段的tablayout和viewpager。 而FragmentList就是其中一个片段。

以下是我的部分代码:

public class FragmentList extends Fragment {
View view;
SwipeRefreshLayout swipeRefreshLayout;
AppCompatActivity activity;
RecyclerView list_container;
Adapter recyclerviewAdapter;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Activity a = getActivity();
    if (a instanceof AppCompatActivity)
        activity = (AppCompatActivity) a;
    view = inflater.inflate(R.layout.fragment_list, container, false);
    initViews();
    return view;
}

private void initViews() {
    swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout);
    swipeRefreshLayout.setColorSchemeColors(getColor(R.color.colorAccentLight), getColor(R.color.colorAccent), getColor(R.color.colorAccentDark));
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(false);
        }
    });

    list_container = (RecyclerView) view.findViewById(R.id.list_container);
    list_container.setHasFixedSize(true);
    LinearLayoutManager manager = new LinearLayoutManager(getContext());
    manager.setOrientation(LinearLayoutManager.VERTICAL);
    list_container.setLayoutManager(manager);
    list_container.setItemAnimator(new DefaultItemAnimator());


    List<Test> list = new ArrayList<>();
    list.add(new Test(new String("0"), 0));
    list.add(new Test(new String("1"), 1));
    list.add(new Test(new String("2"), 3));
    recyclerviewAdapter = new Adapter(list);

    list_container.setAdapter(recyclerviewAdapter);

}

private int getColor(int res) {
    return ContextCompat.getColor(getContext(), res);
}

class Adapter extends RecyclerView.Adapter<holder> {
    List<Test> list;

    public Adapter(List<Test> list) {
        this.list = list;
    }

    public void add(Test test) {
        list.add(test);
    }
    @Override
    public holder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("", "onCreateViewHolder: " + viewType);
        View vvv = LayoutInflater.from(parent.getContext()).inflate(R.layout.testlayout, parent, false);
        return new holder(vvv);
    }

    @Override
    public void onBindViewHolder(holder holder, int position) {
        Log.d("", "onBindViewHolder: " + position);
        holder.title.setText(list.get(position).str);
    }

    @Override
    public int getItemCount() {
        Log.d("", "getItemCount: " + list.size());
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        Log.d("", "getItemViewType: " + position);
        return list.get(position).id;
    }
}

static class holder extends RecyclerView.ViewHolder {
    View view;
    TextView title;

    public holder(View itemView) {
        super(itemView);
        view = itemView;
        title = (TextView) itemView.findViewById(R.id.text1);
    }
}

class Test {
    String str;
    int id;

    public Test(String str, int id) {
        this.id = id;
        this.str = str;
    }
}
}

fragment_list.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/fragment_list_appbar" />

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swiperefreshlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.SwipeRefreshLayout>

testlayout.xml用于查看者的视图。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text1"/>
</LinearLayout>

请告诉我问题出在哪里。感谢。

已更新

我更新了部分代码,其他代码保持不变。但是这个观点并没有显示任何东西。

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Activity a = getActivity();
    if (a instanceof AppCompatActivity)
        activity = (AppCompatActivity) a;
    view = inflater.inflate(R.layout.fragment_list, container, false);

    swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout);
    list_container = (RecyclerView) view.findViewById(R.id.list_container);
    recyclerviewAdapter = new Adapter();
    list_container.setAdapter(recyclerviewAdapter);
    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    initViews();
}

private void initViews() {
    //swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout);
    swipeRefreshLayout.setColorSchemeColors(getColor(R.color.colorAccentLight), getColor(R.color.colorAccent), getColor(R.color.colorAccentDark));
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(false);
        }
    });

    //list_container = (RecyclerView) view.findViewById(R.id.list_container);
    list_container.setHasFixedSize(true);
    LinearLayoutManager manager = new LinearLayoutManager(getContext());
    manager.setOrientation(LinearLayoutManager.VERTICAL);
    list_container.setLayoutManager(manager);
    list_container.setItemAnimator(new DefaultItemAnimator());


    List<Test> list = new ArrayList<>();
    list.add(new Test(new String("0"), 0));
    list.add(new Test(new String("1"), 1));
    list.add(new Test(new String("2"), 3));
    //recyclerviewAdapter = new Adapter();
    recyclerviewAdapter.add(list);
    recyclerviewAdapter.notifyDataSetChanged();
    //list_container.setAdapter(recyclerviewAdapter);
}

private int getColor(int res) {
    return ContextCompat.getColor(getContext(), res);
}

class Adapter extends RecyclerView.Adapter<holder> {
    List<Test> list = new ArrayList<>();

    public Adapter() {
    }

    public void add(Test test) {
        list.add(test);
    }

    public void add(List<Test> test) {
        list.addAll(test);
    }

    @Override
    public holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (list == null || list.size() == 0) return null;
        Log.e("", "onCreateViewHolder: " + viewType);
        View vvv = LayoutInflater.from(parent.getContext()).inflate(R.layout.testlayout, parent, false);
        holder hh = new holder(vvv);
        return hh;
    }

    @Override
    public void onBindViewHolder(holder holder, int position) {
        if (holder == null) return;
        Log.e("", "onBindViewHolder: " + position);
        holder.title.setText(list.get(position).str);
    }

    @Override
    public int getItemCount() {
        Log.e("", "getItemCount: " + list.size());
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        Log.e("", "getItemViewType: " + position);
        return list.get(position).id;
    }
}

1 个答案:

答案 0 :(得分:0)

首先将空列表传递给适配器,然后在获取数据后通知适配器(例如,adapter.notifyDataSetChanged)。你的oncreateView方法中的另一件事只做findViewById。其他初始化和传递数据到适配器在onActivityCreated()中执行。 从onCreateView中删除适配器代码。 现在让我们看看代码。

    //Global Declaration;
    List<Text> list=new ArrayList<>();
    LinearLayoutManager linearLayoutManager;
    RecyclerView recyclerView;

//Inside onActivityCreated
    linearLayoutManager = new LinearLayoutManager(getActivity);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(linearLayoutManager);

            mAdapter = new Adapter(list);
            recyclerView.setAdapter(mAdapter);
//Do your list.add here then notify adapter
mAdapter.notifyDataSetChanged();