我在填写RecyclerView时出现了一个奇怪的问题。 我有一个带有4个标签的TabLayout,在第一个标签中我想显示一个联系人列表,但它只显示部分结果(3个联系人或根本没有) 而我的联系人名单大约是250。 我还注意到,起初只有前两个选项卡被实例化,例如我按第三个选项卡然后第一个选项卡显示预期结果。
这是我的适配器:
public class rvAdapter extends RecyclerView.Adapter<rvAdapter.CustomViewHolder> {
private static List<Contact> contacts;
// 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 static class CustomViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextViewName;
public CheckBox mCheckBox;
public TextView mTextViewNumber;
public CustomViewHolder(View view) {
super(view);
this.mTextViewName = (TextView) view.findViewById(R.id.tv_name);
this.mCheckBox = (CheckBox) view.findViewById(R.id.cb_check_box);
this.mTextViewNumber = (TextView) view.findViewById(R.id.tv_number);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public rvAdapter(List<Contact> contacts) {
this.contacts = contacts;
Log.d("TAG","constructor "+contacts.size());
}
// Create new views (invoked by the layout manager)
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.contacts_list_item, parent, false);
// set the view's size, margins, paddings and layout parameters
CustomViewHolder vh = new CustomViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(CustomViewHolder holder,int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
String name = contacts.get(position).getName();
String number = contacts.get(position).getPhone_number();
//Boolean status = contacts.get(position).isSelected();
holder.mTextViewName.setText(name);
holder.mTextViewNumber.setText(number);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return contacts.size();
}
}
这是如何设置的:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_invite, container, false);
numbers = new ArrayList<>();
contacts = new ArrayList<>();
//find done button
btn_done = (Button) view.findViewById(R.id.btn_done);
//find listview
rv_invite = (RecyclerView) view.findViewById(R.id.rv_invite);
//get contacts list
setContacts(numbers);
//sorting the contact list
Collections.sort(contacts);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
//mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv_invite.setLayoutManager(mLayoutManager);
// specify an adapter
mAdapter = new rvAdapter(contacts);
rv_invite.setAdapter(mAdapter);
return view;
}
这是布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_invite"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_done"
android:layout_gravity="center"
android:text="@string/Done" />
</LinearLayout>