我有这个奇怪的问题,我的适配器没有调用任何方法..构造函数工作,它停在那里,如果我有一个断点,但从那里,没有.. 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>
解决方法
@H_404_20@ 我看了你的代码不幸的是没有真正看到一个问题.可能有问题的一点是,在ViewHolder构造函数中,您调用super(v).您可能需要调用super((LinearLayout)v).但这可能没什么区别.祝好运!