片段内的空白Recyclerview

时间:2016-11-10 14:48:44

标签: android xml android-fragments android-recyclerview

我一起使用片段和recyclerview。我也有数据库,我想查询来自数据库的结果并在活动中显示结果。

然而,每当我尝试运行应用程序并切换到部件以获取并查看结果时,我似乎什么也得不到。没有任何结果只是空白。我不知道为什么它没有出现。

这是我的完整代码

示例类

public class Sample extends Fragment {
    private View rootView;

    public Sample() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        rootView = inflater.inflate(sample, container, false);

        RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView_sample);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setHasFixedSize(true);

        DatabaseHandler db= new DatabaseHandler(getActivity());
        List<SampleModel> list = db.getResults();

        SampleAdapter sampleAdapter = new SampleAdapter(list);
        mRecyclerView.setAdapter(sampleAdapter);

        return rootView;
    }
}

适配器类

public class SampleAdapter extends RecyclerView.Adapter <SampleAdapter.ViewHolder> {

    private List<SampleModel> list;
    private Context mContext;

    public SampleAdapter (List<SampleModel> list) {
        list = list;
    }

    @Override
    public SampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.sample_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SampleAdapter.ViewHolder holder, int position) {
        SampleModel sample = list.get(holder.getAdapterPosition());
        holder.title.setText(sample.getTitle())
    }

    @Override
    public int getItemCount() {
        return (list != null? list.size():0);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView title;

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

XML

sample.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

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

    </android.support.design.widget.CoordinatorLayout>

sample_recyclerview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_sample"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"/>

</RelativeLayout>

sample_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:focusable="true"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardBackgroundColor="#808080">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff" />

    </LinearLayout>

</android.support.v7.widget.CardView>

3 个答案:

答案 0 :(得分:0)

SampleAdapter更改

list = list;

this.list = list;

答案 1 :(得分:0)

您的Adapter构造函数错误。您没有将dataSource传递给适配器。将其更改为

 public SampleAdapter (List<SampleModel> list) {
        this.list = list;
    }

此外,您必须通知适配器您更改了dataSource。您可以通过调用方法notifyDataSetChanged

来实现
SampleAdapter sampleAdapter = new SampleAdapter(list);
mRecyclerView.setAdapter(sampleAdapter);
sampleAdapter.notifyDataSetChanged();

答案 2 :(得分:-1)

如果未正确返回大小,RecyclerViews将不返回任何内容(例如,使用:return 0;)  从更改代码:   public int getItemCount(){         return(list!= null?list.size():0);      要:

 public int getItemCount() {
    return list.size();