不调用RecyclerView Adapter方法

时间:2015-07-12 20:04:44

标签: java android android-fragments adapter android-recyclerview

我有一个奇怪的问题,我的适配器没有调用任何方法..构造函数工作,如果我有一个断点,它停在那里,但从那里,没有.. onCreateViewHolder没有被调用和既不是onBindViewHolder也不是getItemCount ..我尝试了我能在网上找到的所有内容,但似乎没有任何工作,所以我不知道该怎么做。有什么建议?这是我的代码: 片段:

public class FragmentPackageDetails extends Fragment {
    //other vars
    RecyclerView rvPackageDetails;
    DatabaseHandler database;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle args = getArguments();
        String temp = args.getString(KEY_TRACKER_CODE);
        View v = inflater.inflate(R.layout.fragment_package_details_new, container, false);

        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rvPackageDetails = (RecyclerView) view.findViewById(R.id.rvPackageDetails);
        rvPackageDetails.setLayoutManager(new LinearLayoutManager(getActivity()));

        database = new DatabaseHandler(getActivity());
        selectedPackage = database.getPackage(trackerCode);

        RecyclerView.Adapter adapter = new MyAdapter(selectedPackage.getRecords());
        rvPackageDetails.setAdapter(adapter);
    }
    //other methods in class
}

MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private ArrayList<TrackingRecordCheckedOut> mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView tvPackageDetailsRowDayOfWeek, tvPackageDetailsRowDayOfMonth, tvPackageDetailsRowNameOfMonth,
                tvPackageDetailsRowDetailsTitle, tvPackageDetailsRowDetailsDescription, tvPackageDetailsRowDetialsTime;

        public ViewHolder(View v) {
            super(v);
            tvPackageDetailsRowDayOfWeek = (TextView) v.findViewById(R.id.tvPackageDetailsRowDayOfWeek);
            tvPackageDetailsRowDayOfMonth = (TextView) v.findViewById(R.id.tvPackageDetailsRowDayOfMonth);
            tvPackageDetailsRowNameOfMonth = (TextView) v.findViewById(R.id.tvPackageDetailsRowNameOfMonth);
            tvPackageDetailsRowDetailsTitle = (TextView) v.findViewById(R.id.tvPackageDetailsRowDetialsTime);
            tvPackageDetailsRowDetailsDescription = (TextView) v.findViewById(R.id.tvPackageDetailsRowDetailsDescription);
            tvPackageDetailsRowDetialsTime = (TextView) v.findViewById(R.id.tvPackageDetailsRowDetialsTime);
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(ArrayList<TrackingRecordCheckedOut> myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_list_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        TrackingRecordCheckedOut tr = mDataset.get(position);
        holder.tvPackageDetailsRowDayOfWeek.setText(tr.getDayOfWeek());
        holder.tvPackageDetailsRowDayOfMonth.setText(tr.getDayOfWeek());
        holder.tvPackageDetailsRowNameOfMonth.setText(tr.getMonthOfYear());
        holder.tvPackageDetailsRowDetailsTitle.setText(tr.getTitle());
        holder.tvPackageDetailsRowDetailsDescription.setText(tr.getDetails());
        holder.tvPackageDetailsRowDetialsTime.setText(tr.getTimeOfDay());

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

}

片段布局:

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvPackageDetails"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

RecyclerView行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llPackageDetailsRecyclerViewRow"
    android:layout_width="match_parent"
    android:layout_height="96dip"
    android:orientation="horizontal"
    android:weightSum="100">

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:orientation="vertical"
        android:weightSum="10">


        <TextView
            android:id="@+id/tvPackageDetailsRowDayOfWeek"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="3"/>

        <TextView
            android:id="@+id/tvPackageDetailsRowDayOfMonth"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="4"/>

        <TextView
            android:id="@+id/tvPackageDetailsRowNameOfMonth"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="3"/>


    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="80"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvPackageDetailsRowDetailsTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"/>

        <TextView
            android:id="@+id/tvPackageDetailsRowDetailsDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4"/>

        <TextView
            android:id="@+id/tvPackageDetailsRowDetialsTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"/>

    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

我看了你的代码,不幸的是,我真的没有看到任何问题。可能有问题的一点是,在ViewHolder构造函数中,您调用super(v)。您可能需要调用super((LinearLayout)v)。但这可能没什么区别。

祝你好运!

答案 1 :(得分:0)

我遇到了同样的问题,即onCreateViewHolder或onBindViewHolder都没有被调用。在Adapter的构造函数中保存对Context的引用,然后更改onCreateViewHolder方法以使用保存的Context语句而不是parent.getContext()来修复它:

private Context mContext;

public MyAdapter(Context context, ArrayList<TrackingRecordCheckedOut> myDataset) {
    mDataset = myDataset;
    mContext = context;
}

public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view using saved reference to Context (mContext)
    View v = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_list_item, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ViewHolder vh = new ViewHolder(v);
    return vh;
}