I've implementend a RecyclerView as follow
The Adapter with the Holder
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.indovinelloelement,parent,false);
MyHolder holder = new MyHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
holder.category.setText(data.get(position).getCategory());
holder.difficulty.setText(""+data.get(position).getDifficulty());
holder.title.setText(data.get(position).getTitle());
holder.score.setText(""+data.get(position).getScore());
if(data.get(position).isLocked())
holder.setLocked();
else
holder.setUnlocked();
}
public DataGestour.Indovinello getIndovinello(int pos){
return data.get(pos);
}
#Override
public int getItemCount() {
return data.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
public TextView category,score,difficulty,title;
private ImageView lock_state;
public MyHolder(View itemView) {
super(itemView);
category = (TextView)itemView.findViewById(R.id.categoria);
score = (TextView)itemView.findViewById(R.id.punteggio);
difficulty = (TextView) itemView.findViewById(R.id.difficolta);
title = (TextView)itemView.findViewById(R.id.titolo);
lock_state = (ImageView) itemView.findViewById(R.id.lock_state);
}
public void setLocked(){
lock_state.setImageResource(R.drawable.ic_lock_outline_black_24dp);
}
public void setUnlocked(){
lock_state.setImageResource(R.drawable.ic_lock_open_black_24dp);
}
}
ArrayList<DataGestour.Indovinello> data;
Context context;
public MyAdapter(Context context, ArrayList<DataGestour.Indovinello> list){
this.context = context;
this.data = list;
}
}
and the RecyclerView:
rc = (RecyclerView)root_view.findViewById(R.id.lista_domande);
rc.setHasFixedSize(true);
LinearLayoutManager ly = new LinearLayoutManager(getContext());
ly.setOrientation(LinearLayoutManager.VERTICAL);
rc.setLayoutManager(ly);
try{
gestour = new DataGestour(getContext());
}catch (IllegalStateException e){
Log.e("DataManager",e.getMessage());
};
adapter = new MyAdapter(getContext(),gestour.getAllDatas());
rc.setAdapter(adapter);
adapter.notifyDataSetChanged();
where DataGestoure is a class that manipulates some data from a
database and store them in a ArrayList (DataGestour.getAllDatas is the method to return the data under a ArrayList)
The problem start only if the ArrayList contains more then 3 items becouse the RecyclerView doen't show them despite the fact that the adapter holds all the data in ArrayList< DataGestour.Indovinello > data
Well, you need a little more order in your code, since it is not properly structured, by default the RecyclerView has its Scrolleable view unless in the XML this attribute is removed, well I recommend that you use this code in your adapter and use a model to save the information you get from your BD, that is the correct way to work to keep everything in order and it is easier to work it, once this is done you can use this code perfectly that I am sure will work, remember to only pass information to my Model that in my case calls it "Model" and with this the error of your data load will be solved. It is also advisable to use a List <> for these cases since the Arrays <> tend to have errors when you manipulate the data and more if you do it the way you present the code.
private List<Model> mDataList;
public MyAdapter(Context context){
this.context = context;
this.mDataList = new ArrayList<>();
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.indovinelloelement,parent,false);
return new MyHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
MyHolder holder = (MyHolder) MyHolder;
hoder.bind(mDataList.getPosition(position));
}
#Override
public int getItemCount() {
return mDataList.size();
}
public void setData(List<Model> list) {
mDataList.clear();
mDataList.addAll(list);
notifyDataSetChanged();
}
public class MyHolder extends RecyclerView.ViewHolder {
TextView category;
category = (TextView)itemView.findViewById(R.id.categoria);
TextView score;
score = (TextView)itemView.findViewById(R.id.punteggio);
TextView difficulty;
difficulty = (TextView) itemView.findViewById(R.id.difficolta);
TextView title;
title = (TextView)itemView.findViewById(R.id.titolo);
ImageView lock_state;
lock_state = (ImageView) itemView.findViewById(R.id.lock_state);
public MyHolder(View itemView) {
super(itemView);
}
protected void bind(Model model){
category.setText(model.getCategory());
score.setText(model.getScore());
difficulty.setText(model.getDifficulty());
title.setText(model.getTitle());
if(model.isLocked()){
lock_state.setImageResource(R.drawable.ic_lock_outline_black_24dp);
} else {
lock_state.setImageResource(R.drawable.ic_lock_open_black_24dp);
}
}
}
}
And for your class that contains the RecyclerView use the code as follows.
RecyclerView mRecyclerView;
mRecyclerView = (RecyclerView)root_view.findViewById(R.id.lista_domande);
private MyAdapter mAdapter;
private List<Model> mDataList;
private DataGestour gestour;
private void setupRecyclerView(){
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(linearLayoutManager);
mAdapter = new MyAdapter(getContext());
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setData(mDataList);
}
private void getDataDB(){
mDataList.add(gestour.getAllDatas);
}
Related
RecyclerView items not visible
I am using a string array name item test and pass it to String array list, I used the same idea before and was working correctly
listItemsTest = new ArrayList<>(Arrays.asList(itemsTest));
recyclerView = findViewById(R.id.RecycleViewTest);
recycleViewAdapterTest = new RecycleViewAdapterTest(this,listItemsTest);
recyclerView.setAdapter(recycleViewAdapterTest);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
My Adapter
public class RecycleViewAdapterTest extends
RecyclerView.Adapter<RecycleViewAdapterTest.MyViewHolder> {
private ArrayList<String> ListTest;
Context context;
public RecycleViewAdapterTest(Context context, ArrayList<String> listTest) {
this.ListTest = listTest;
this.context = context;
}
#NonNull
#Override
public RecycleViewAdapterTest.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull RecycleViewAdapterTest.MyViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return ListTest.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
TextView textView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
linearLayout = itemView.findViewById(R.id.LLTest);
textView = itemView.findViewById(R.id.textViewTest);
}
}
#Override
public void onBindViewHolder(#NonNull RecycleViewAdapterTest.MyViewHolder holder, int position) {
String value = this.ListTest.get(position);
holder.textView.setText(value);// Try to make the textView public
}
because you need to set list value to viewholder's textview in OnBindViewHolder. :(
I want to make a custom Adapter for my RecyclerView because I need to use a custom method inside who will init my List later when my presenter will be ready:
public void setList(List<Object> data){
this.data = data;
}
This is my not custom interface for my Adapter without implementation.
final class AdapterReviews extends RecyclerView.Adapter<AdapterReviews.ReviewViewHolder> {}
The question is how should be the interface for my custom Adapter?
**public class GetHolidaylistAdapter extends RecyclerView.Adapter<GetHolidaylistAdapter.ViewHolder> {
ArrayList<HashMap<String, String>> arrayList;
public GetHolidaylistAdapter(ArrayList<HashMap<String, String>> arrayList) {
this.arrayList = arrayList;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView holiyDay_Date_Tv, holidayName_tv, holidayType_tv, day_tv;
public ViewHolder(View itemView) {
super(itemView);
holiyDay_Date_Tv = (TextView) itemView.findViewById(R.id.holiyDay_Date_Tv);
holidayName_tv = (TextView) itemView.findViewById(R.id.holidayName_tv);
holidayType_tv = (TextView) itemView.findViewById(R.id.holidayType_tv);
}
}
#Override
public int getItemCount() {
return arrayList.size();
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
GetHolidaylistAdapter.ViewHolder viewHolder = null;
Log.d(TAG, "Rv_Child_Active.." + viewType);
viewHolder = new GetHolidaylistAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).
inflate(R.layout.holiday_child_rv, parent, false));
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
String date = arrayList.get(position).get(TAG_HOLIDAYDATE);
holder.holidayName_tv.setText(arrayList.get(position).get(TAG_HOLIDAYName));
holder.holiyDay_Date_Tv.setText(arrayList.get(position).get(TAG_HOLIDAYDATE));
holder.holidayType_tv.setText(arrayList.get(position).get(TAG_HOLIDAYtype));
***}
}
recycle_view=(RecyclerView)findViewById(R.id.recycle_view);
linearLayoutManager=new LinearLayoutManager(this);
recycle_view.setLayoutManager(linearLayoutManager);
adapter=new CustomAdapter(modelRecyclerArrayList);
recycle_view.setAdapter(adapter);
RecyclerView recycle_view;
LinearLayoutManager linearLayoutManager;
CustomAdapter adapter;
ArrayList<ModelRecycler> modelRecyclerArrayList = new ArrayList<>();*
A basic RecyclerView CustomAdapter looks like this
public class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context;
ArrayList<String> list = new ArrayList<>();
public CustomAdapter(Context context, ArrayList<String> list) { // you can pass other parameters in constructor
this.context = context;
this.list = list;
}
private class CustomAdapterItemView extends RecyclerView.ViewHolder {
final TextView textView;
CustomAdapterItemView(final View itemView) {
super(itemView);
textView = (TextView) itemView;
}
void bind(int position) {
textView.setText("");
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new CustomAdapterItemView(LayoutInflater.from(context).inflate(R.layout.item_color, parent, false));
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((CustomAdapterItemView) holder).bind(position);
}
#Override
public int getItemCount() {
return list.size();
}
// Add your other methods here
}
You can check this. Here you have complete example of RecyclerView. It is all made by Google.
I am using a RecyclerView to view many Progress Bars as a slide show and an adapter.
This my adapter class
public class RecAdapter extends RecyclerView.Adapter<RecAdapter.ViewHolder> {
private static final String TAG = "RecAdapter";
private ArrayList progressbars = new ArrayList<>();
private Context pcontex;
public RecAdapter(Context pcontex ,ArrayList progressbars) {
this.progressbars = progressbars;
this.pcontex = pcontex;
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.progress,viewGroup,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
Log.d(TAG, "onBindViewHolder: called");
}
#Override
public int getItemCount() {
return progressbars.size() ;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ProgressBar b ;
ConstraintLayout parent;
public ViewHolder(#NonNull View itemView) {
super(itemView);
b = itemView.findViewById(R.id.progressBar);
parent= itemView.findViewById(R.id.parent);
}
}
}
My MainActivity code-
Prog = new ArrayList<>(); b2 = findViewById(R.id.progressBar) ;
b = findViewById(R.id.progressBar);
probitmap();
private void probitmap(){
Log.d(TAG, "probitmap: preparing bit maps");
Prog.add(b);
Prog.add(b2);
Recycle();// this method is out of on create
}
private void Recycle(){
Log.d(TAG, "Recycle: init recycleview");
recyclerView =findViewById(R.id.Rec);
RecAdapter Adapter = new RecAdapter(this,Prog);
recyclerView.setAdapter(Adapter);
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
}
The code is working perfectly but I need to retrieve every progress bar that is in the recycle view and set progress for each one.
I tried multiple ways but it doesn't work. Here is what I tried-
for (int i = 0; i < recyclerView.getChildCount(); i++) {
RecAdapter.ViewHolder holder = (RecAdapter.ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
holder.b.setProgress(50);
}
Use OnBindViewHolder to manage each "row" generated by the recycler.
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
viewHolder.b.setProgress(progressbar.get(i)); //i is the position of the recycler
viewHolder.b.setListener....
}
I have a List<String> listOfNames that contains names. How to print them all using RecyclerView?
I was looking for a simple way of doing this and after some time of fiddling around, I think I have found a way.
Here I use an ArrayList of Strings.
Adapter Class
public class TagsAdapter extends RecyclerView.Adapter<TagsAdapter.TagsViewHolder> {
ArrayList<String> arrayList;
public TagsAdapter(ArrayList<String> arrayList) {
this.arrayList = arrayList;
}
#NonNull
#Override
public TagsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_holder_tags_chips, parent, false);
return new TagsAdapter.TagsViewHolder(layoutView);
}
#Override
public void onBindViewHolder(#NonNull final TagsViewHolder holder, final int position) {
holder.tagText.setText(arrayList.get(position));
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class TagsViewHolder extends RecyclerView.ViewHolder {
public TextView tagText;
public TagsViewHolder(View view) {
super(view);
tagText = view.findViewById(R.id.chipTextView);
}
}
}
Now in the Activity or fragment you want to show the recycler view, initialize your recycler view to the view in the xml
Tags.Java
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
In the same place add this method. I am assuming that the array list already has strings in it
private void getTags() {
recyclerView = view.findViewById(R.id.view);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);
layoutManager = new LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false);
//I have made my recycler view horizontal, if you want it vertical, just change the horizontal above to vertical.
recyclerView.setLayoutManager(layoutManager);
adapter = new TagsAdapter(tagsArray);
recyclerView.setAdapter(adapter);
}
I know this is an old question but might help someone who wants a simple and straightforward way of doing this.
Hi Simple example for recycler view.
Follow below steps:
Declare in global this variable
private RecyclerView recyclerview;
Use this where you get response from server and make one model class using GSON plugin just copy and paste your response.After use that.
Gson gson = new Gson();
CreateYourModel createYourModel = gson.fromJson(response_data, YourActivity.class);
RecyclerAdapter mAdapter = new RecyclerAdapter(createYourModel);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerview.setLayoutManager(mLayoutManager);
recyclerview.setItemAnimator(new DefaultItemAnimator());
recyclerview.setAdapter(mAdapter);
recyclerview.setNestedScrollingEnabled(false);
This is your Recycler adapter just use same.
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private CreateYourModel mList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tvdate;
public MyViewHolder(View view) {
super(view);
tvdate = (TextView) view.findViewById(R.id.tvdate);
}
}
public RecyclerAdapterCreateYourModel mList) {
this.mList = mList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.youritemview, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tvsummuryloan_repayment_date.setText(setyourdatafromlist);
}
#Override
public int getItemCount() {
return mList.size();
}
}
You can follow any tutorial on google.
Thanks
I'm trying to display my recyclerView on MainActivity, but can't seem to do it.
This is my code: (which compiles with no errors)
private RecyclerView recyclerView;
private MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) this.findViewById(R.id.recycler_view_example);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyAdapter(this, getData());
recyclerView.setAdapter(adapter);
// Add Code to display recyclerView on Main...
}
// This is not my actual data, just testing it out
public static List<Block> getData() {
List<Block> data = new ArrayList<>();
String[] ids = {"310", "313", "320"};
String[] names = {"name of three ten", "name of three thirteen", "name of three twenty"};
for (int i=0; i<ids.length; i++) {
data.add(new Block(ids[i], names[i]));
}
return data;
}
And myActivity Class:
private final LayoutInflater inflater;
// Data: (information)
List<Block> data = new ArrayList<>();
public MyAdapter(Context context, List<Block> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.block, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
MyViewHolder holder = new MyViewHolder(h.itemView);
Block current = data.get(position);
holder.id.setText(current.getId());
holder.name.setText(current.getName());
}
#Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView id;
TextView name;
Button button;
// Constructor
public MyViewHolder(View itemView) {
super(itemView);
id = (TextView) itemView.findViewById(R.id.the_course_id);
name = (TextView) itemView.findViewById(R.id.course_name);
button = (Button) itemView.findViewById(R.id.click);
}
}
}
All I'm trying to do now is that when I run the emulator, I will see the contents of the recyclerView. But, I've been stuck on this for a while as nothing seems to work.
Mind you I'm a beginner with Android, so forgive me if this is very trivial.
You should return the number of items in your list, within getItemCount as in:
#Override
public int getItemCount() {
return data.size();
}
Ok here you need to change the following: update -
recyclerView.setLayoutManager(new LinearLayoutManager(this));
to
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
and in adapter class change:
#Override
public int getItemCount() {
return data.size();
}
thats it. Happy coding :)
Change onBindViewHolder to:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
Block current = data.get(position);
h.id.setText(current.getId());
h.name.setText(current.getName());
}
also add #SelçukCihan solution to this
EDIT
Also if you want to use your MyViewHolder you have to change the following:
First change class signature:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
and also change the signature for onBindViewHolder:
public void onBindViewHolder(MyViewHolder h, int position)
and finally change the signature of your MyViewHolder class:
static class MyViewHolder extends RecyclerView.ViewHolder