Actually i am new to android development and working on an app and trying to implement viewpager to show the images like carousel in recyclerview but don't know how to set the viewpager in recyclerview onBindViewHolder function to show viewpager the images.
Actually i m trying to implement like this
and this is mine app's screenshot
so please guide me how can i implement it
my Adapter Class
public class RoomsAdapter extends RecyclerView.Adapter<RoomsAdapter.RoomsViewHolder> {
Context context;
ArrayList<Rooms> rooms;
public RoomsAdapter(Context context, ArrayList<Rooms> rooms) {
this.context = context;
this.rooms = rooms;
}
#NonNull
#Override
public RoomsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_product, parent, false);
RoomsViewHolder viewHolder = new RoomsViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RoomsViewHolder holder, int position) {
Rooms room = rooms.get(position);
holder.binding.roomsTitle.setText(room.getTitle());
holder.binding.roomsRent.setText("Rent ₹ " + room.getPrice() + " /month");
holder.binding.roomsLocation.setText(room.getLocation());
}
#Override
public int getItemCount() {
return rooms.size();
}
public class RoomsViewHolder extends RecyclerView.ViewHolder {
ItemProductBinding binding;
public RoomsViewHolder(#NonNull View itemView) {
super(itemView);
binding = ItemProductBinding.bind(itemView);
}
}
}
Actually, i didn't set the viewpager here coz i know how to the set the image view but not any idea how can I set the viewpager in recyclerview to show images from url or drawable.
Please guide me
Related
I am new in firebase android app development.
In this app i fetched the data from firebase database.
Here i want to implement show more functionality in recyclerview to save data loading.
I see the solution in many stackoverflow question. But in every question The adapter class is defined outside from main activity. so i cant understand properly.
And here my adapter is within main activty class.
class MainActivity extends AppCompatActivity {
private RecyclerView postList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PostRef=FirebaseDatabase.getInstance().getReference().child("Posts");
postList = (RecyclerView) findViewById(R.id.all_user_post_list);
postList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
postList.setLayoutManager(linearLayoutManager);
displayAllUserPost();
}
private void displayAllUserPost() {
Query sortPost=PostRef;
FirebaseRecyclerOptions<Post> options=new FirebaseRecyclerOptions.Builder<Post>().setQuery(sortPost,Post.class).build();
FirebaseRecyclerAdapter<Post, PostsViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Post, PostsViewHolder>(options)
#Override
protected void onBindViewHolder(#NonNull final PostsViewHolder holder, final int position, #NonNull final Post model) {
final String PostKey = getRef(position).getKey();
holder.postfullname.setText(model.getFirstname() + " " + model.getLastname());
}
#NonNull
#Override
public PostsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.all_post_layout,parent,false);
PostsViewHolder viewHolder=new PostsViewHolder(view);
return viewHolder;
}
}
}
public static class PostsViewHolder extends RecyclerView.ViewHolder
{
TextView postfullname;
public PostsViewHolder(View itemView)
{
super(itemView);
postfullname = itemView.findViewById(R.id.user_post_full_name);
}
}
}
So How i add show more functionality in this code when first load only 10 items and than after load more 10 items with rounded progressbar(show more button is not important).
Most important thing is that first load only 10 items. rest item should not be loaded until user scrolled down to last(10th) item
Thank you.
I want to populate recyclerview items when user click anywhere on the screen one by one. Eg on first touch load first item on second touch load second item on third touch load third item and so on. I have no idea how to implement this.
Here is my adapter code
Adapter
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder>{
List<MessageModel> list;
Context context;
public MessageAdapter(List<MessageModel> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#Override
public MessageAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context)
.inflate(R.layout.chatbubble, viewGroup, false);
ViewHolder myHolder = new ViewHolder(view);
return myHolder;
}
#Override
public void onBindViewHolder(#NonNull MessageAdapter.ViewHolder viewHolder, int i) {
MessageModel model = list.get(i);
if(model.getSender().equals("left")){
viewHolder.left.setText(model.getText());
viewHolder.right.setVisibility(View.GONE);
viewHolder.middle.setVisibility(View.GONE);
} else if(model.getSender().equals("right")){
viewHolder.right.setText(model.getText());
viewHolder.middle.setVisibility(View.GONE);
viewHolder.left.setVisibility(View.GONE);
} else {
viewHolder.middle.setText(model.getText());
viewHolder.left.setVisibility(View.GONE);
viewHolder.right.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView left, right, middle;
public ViewHolder(#NonNull View itemView) {
super(itemView);
left = itemView.findViewById(R.id.message_incoming);
right = itemView.findViewById(R.id.message_outgoing);
middle = itemView.findViewById(R.id.message_middle);
}
}
}
In the Activity which holds your RecyclerView, do the following code changes.
Add an onClickListener or onTouchListener whichever you prefer to your RecyclerView.
recyclerView.setOnClickListener() or recyclerView.setOnTouchListener()
Add the code to add the items into your RecyclerView in above event listeners.
yourList.add("Your Item");
adapter.notifyItemInserted(int positionAtWhichItemWasInserted);
adapter.notifyDatasetChanged();
//For some reason I'm unable to format the above code snippet.
Make sure you call notifyItemInserted() OR notifyDatasetChanged() after you add the item in your RecyclerView list.
Well i tried to implement the recyclerview into Android Studio like this :
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {
public String[] dataSet;
public Context context;
//---------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------//
public class ViewHolder extends RecyclerView.ViewHolder{
public ImageView image;
public TextView name;
public ViewHolder(View itemView) {
super(itemView);
this.image = itemView.findViewById(R.id.image);
this.name = itemView.findViewById(R.id.name);
}
public void bindData(Object data){
String extractedData = (String)data;
this.name.setText("");
this.name.setText(extractedData);
this.image.getLayoutParams().height = getRandomIntInRange(250, 100);
}
}
//---------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------//
public RecycleViewAdapter(Context context, String[] dataSet){
this.context = context;
this.dataSet = dataSet;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.food_view,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
// Resolves the messed up views after recycling.
viewHolder.setIsRecyclable(false);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bindData(dataSet[position]);
}
#Override
public int getItemCount() {
return dataSet.length;
}
// Custom method to get a random number between a range
protected int getRandomIntInRange(int max, int min){
Random rdm = new Random();
return rdm.nextInt((max-min)+min)+min;
}
}
Somehow i noticed that once i scroll up and down the recycled views get messed up...
Heres a Picture without Scrolling :
normal
And here is one after Scrolling... as you can see totally messed up :
messed up
Why does this happen and how can i prevent this ?
Does anyone got a solution ?
Well
Adapter is calling onBindViewHolder() all times when he must recreate view on the screen. You need to call getRandomIntInRange() outside of onBindViewHolder()
You could see it:
I have to develop a Recyclerview with PopulateViewHolder in an older version of Firebase. Now I'm trying to upgrade the version (not at the last one) and I don't know how to use onBindHolder or anything about this new kind of RecyclerView.
Here is how I had the setup:
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, EventViewHolder>(
Event.class,
R.layout.event_row,
EventViewHolder.class,
mDatabase.orderByChild("timeStamp")
) {
#Override
protected void populateViewHolder(final EventViewHolder viewHolder, final Event model, final int position) {
final String id = getRef(position).getKey().toString().trim();
viewHolder.setTitle(model.getTitle());
viewHolder.setDateWords(model.getDateWords());
viewHolder.setDesc(model.getDesc());
}
};
mEventList.setAdapter(firebaseRecyclerAdapter);
}
public static class EventViewHolder extends RecyclerView.ViewHolder {
View mView;
public EventViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
}
And here it's how I'm trying to do now:
Query searchQuery = mDatabase.orderByChild("timeStamp");
options = new FirebaseRecyclerOptions.Builder<Event>()
.setQuery(searchQuery, Event.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, EventViewHolder>(options) {
#Override
protected void onBindViewHolder(final EventViewHolder viewHolder, int position, Event model) {
final String id = getRef(position).getKey().toString().trim();
viewHolder.setTitle(model.getTitle());
viewHolder.setDateWords(model.getDateWords());
viewHolder.setDesc(model.getDesc());
}
#NonNull
#Override
public EventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
};
firebaseRecyclerAdapter.startListening();
mEventList.setAdapter(firebaseRecyclerAdapter);
The ViewHolder Class is the same.
Here is the error
Actually, I'm asking for tutorial, because I only find those that make the adapter separately, but I wanna have it in the same activity.
In your public EventViewHolder onCreateViewHolder method, instead of returning a view you are returning null. To solve this, please add the following lines of code inside this method:
#NonNull
#Override
public EventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new EventViewHolder(view);
}
In which item_layout is a custom layout for your item. If you want to use instead of this layout a built-in XML layout document that is part of the Android OS, you can use:
android.R.layout.simple_list_item_1
I want to add the Fast-Scroll-Feature (like in the Android contacts app or in Whatsapp contacts) to my RecyclerView?
I would also use a listview but I need Cards in my app on the same layout!
If implementing fast-scroll to Recyclerview is too difficult, using a ViewStub is also an option, or not?
My RecyclerViewAdapter:
public class SongRecyclerViewAdapter extends RecyclerView.Adapter<SongRecyclerViewAdapter.Holder> {
private Song[] sSongs;
public SongRecyclerViewAdapter(Song[] songs) {
sSongs = songs;
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_songview, parent, false);
Holder holder = new Holder(view);
return holder;
}
#Override
public void onBindViewHolder(Holder holder, int position) {
//holder.imvSong.setImageResource(R.drawable.standardartwork);
holder.txvSongTitle.setText(sSongs[position].getTitle());
holder.txvSongInfo.setText(sSongs[position].getArtists());
}
#Override
public int getItemCount() {
return sSongs != null ? sSongs.length : 0;
}
public class Holder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
ImageView imvSong;
TextView txvSongTitle;
TextView txvSongInfo;
public Holder(View layout) {
super(layout);
linearLayout = (LinearLayout) layout;
imvSong = (ImageView) layout.findViewById(R.id.imvSong);
txvSongTitle = (TextView) layout.findViewById(R.id.adap_txvSongtitle);
txvSongInfo = (TextView) layout.findViewById(R.id.txvSongInfo);
}
}
}
Thanks!
the problem in your code would be the commented line :
//holder.imvSong.setImageResource(R.drawable.standardartwork);
to handle this you have 2 solutions:
1) if u have limited small amount of known images, you can to create bitmaps/drawables once when initializing then you will only need to reference those.
2) create a bitmap cache, smart way to decode bitmaps along with reusable bitmap pool and pooling mechanism. This is not trivial and this what 3rd party libraries do for you. I would not suggest to implemented it yourself.