Items not showing in recycle view
Closed this issue · 1 comments
wadhah101 commented
I am trying to get a simple recycle view using the example provided but Only the header and the footer are showing while it displays a blank space for items
My items are simple textviews
`
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/text"
android:text="jjdjdjdjd"/>
`
my adapter code
` public class ExampleAdapter extends HFRecyclerView {
ArrayList<String> data ;
public ExampleAdapter(ArrayList<String> data) {
// With Header & With Footer
super(true, false);
this.data = data ;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.text.setText(data.get(position));
itemViewHolder.bind(getItem(position));
} else if (holder instanceof HeaderViewHolder) {
} else if (holder instanceof FooterViewHolder) {
}
}
//region Override Get ViewHolder
@Override
protected RecyclerView.ViewHolder getItemView(LayoutInflater inflater, ViewGroup parent) {
return new ItemViewHolder(inflater.inflate(R.layout.item_example, parent, false));
}
@Override
protected RecyclerView.ViewHolder getHeaderView(LayoutInflater inflater, ViewGroup parent) {
return new HeaderViewHolder(inflater.inflate(R.layout.item_header, parent, false));
}
@Override
protected RecyclerView.ViewHolder getFooterView(LayoutInflater inflater, ViewGroup parent) {
return new FooterViewHolder(inflater.inflate(R.layout.item_footer, parent, false));
}
//endregion
//region ViewHolder Header and Footer
class ItemViewHolder extends RecyclerView.ViewHolder {
TextView text;
ItemViewHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.text);
}
void bind(String item) {
text.setText(item);
}
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
HeaderViewHolder(View view) {
super(view);
}
}
class FooterViewHolder extends RecyclerView.ViewHolder {
FooterViewHolder(View view) {
super(view);
}
}
//endregion
}
and here is the main acitivity
`public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
// Init Data
ArrayList<String> list = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
list.add("Example " + i);
}
// Add Adapter
ExampleAdapter adapter = new ExampleAdapter(list);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
`
lopspower commented
I found several errors during your implementation.
- You must define the type of object you will use in your list
HFRecyclerView<String>
:
public class ExampleAdapter extends HFRecyclerView<String> {
// ...
}
- You don't have to redefine your object list in your adapt. It is already present in HFRecyclerView.
ExampleAdapter adapter = new ExampleAdapter();
adapter.setData(youtDataList);
recyclerView.setAdapter(adapter);
If you want to define your data list directly to the creation of your adapter you must use the function setData(data);
:
public ExampleAdapter(ArrayList<String> data) {
// With Header & With Footer
super(true, false);
setData(data);
}
- You must always use the
getItem(position)
function to retrieve an object from your list. It is this function that manages the position of the items.