I have a RecyclerView where each item is a LinearLayout containing two EditTexts and an ImageButton. The RecyclerView is showing every item except the first one of my data set.
Can anyone tell me what's going on?
public class EditListAdapter extends RecyclerView.Adapter<EditListAdapter.ListViewHolder> {
private ArrayList mListCardset;
public static class ListViewHolder extends RecyclerView.ViewHolder{
public EditText frontEditText;
public EditText backEditText;
public ImageButton editActionButton;
public ListViewHolder(View view) {
super(view);
frontEditText = (EditText)view.findViewById(R.id.front_edit_text);
backEditText = (EditText)view.findViewById(R.id.back_edit_text);
editActionButton = (ImageButton) view.findViewById(R.id.edit_action_button);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public EditListAdapter(ArrayList aList) {
mListCardset = aList;
}
// Create new views (invoked by the layout manager)
@Override
public EditListAdapter.ListViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_edit_item, parent, false);
// set the view's size, margins, paddings and layout parameters
ListViewHolder vh = new ListViewHolder((LinearLayout)v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ListViewHolder listHolder, int pos) {
String[] eListItem = (String[]) mListCardset.get(pos); //get individual card @position
listHolder.frontEditText.setText(eListItem[0]);
listHolder.backEditText.setText(eListItem[1]);
}
private String[] strArrayListHelper(String frontString, String backString){
String[] returnStrings = new String[2];
returnStrings[0] = frontString;
returnStrings[1] = backString;
return returnStrings;
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mListCardset.size();
}
}
This will only show items 2 through 5:
mRecyclerView = (RecyclerView)findViewById(R.id.list_edit_recycler_view);
ArrayList testListCards = new ArrayList<String[]>();
testListCards.add(strArrayListHelper("Test 1","Test 1"));
testListCards.add(strArrayListHelper("Test 2","Test 2"));
testListCards.add(strArrayListHelper("Test 3","Test 3"));
testListCards.add(strArrayListHelper("Test 4","Test 4"));
testListCards.add(strArrayListHelper("Test 5","Test 5"));
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(EditActivity.this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter
mAdapter = new EditListAdapter(testListCards);
mRecyclerView.setAdapter(mAdapter);
This method calls onCreateViewHolder to create a new ViewHolder and initializes some private fields to be used by RecyclerView. Returns the position of the given ViewHolder in the given Adapter . Returns the total number of items in the data set held by the adapter. Return the stable ID for the item at position .
onCreateViewHolder is called when you need a new View. by Pavlos-Petros Tournaris | Medium. This is not the way a recycling works.
The adapter is a class. It takes the data set that needs to be shown to the user in RecyclerView. It acts as the main responsible class for binding the views and displaying them. The majority of the work is done within the RecyclerView's adapter class.
Maybe your Toolbar
hides your first item of your RecyclerView
.
Try to use this attribute
android:layout_marginTop="?attr/actionBarSize"
in your RecyclerView
xml.
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With