How to open new activity in recyclerview with firebase database - java

Im creating a recipe app in android studios with categories(chicken,pork,beef etc) and subcategories. But I still can't figure out how to open new activity for my sub categories when i click the categories without passing the same data. BTW im using recyclerview and firebase for my database. Hope someone can help! Thanks
MainActivity.class
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
MainAdapter mainAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<MainModel> options =
new FirebaseRecyclerOptions.Builder<MainModel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("category"), MainModel.class)
.build();
mainAdapter = new MainAdapter(options);
recyclerView.setAdapter(mainAdapter);
}
#Override
protected void onStart() {
super.onStart();
mainAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
mainAdapter.stopListening();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search,menu);
MenuItem item = menu.findItem(R.id.search);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
txtSearch(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
txtSearch(query);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
private void txtSearch(String str)
{
FirebaseRecyclerOptions<MainModel> options =
new FirebaseRecyclerOptions.Builder<MainModel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("category").orderByChild("name").startAt(str).endAt(str+"~"), MainModel.class)
.build();
mainAdapter = new MainAdapter(options);
mainAdapter.startListening();
recyclerView.setAdapter(mainAdapter);
}
}
MainAdapter.class
public class MainAdapter extends FirebaseRecyclerAdapter<MainModel,MainAdapter.myViewHolder> {
/**
* Initialize a {#link RecyclerView.Adapter} that listens to a Firebase query. See
* {#link FirebaseRecyclerOptions} for configuration options.
*
* #param options
*/
public MainAdapter(#NonNull FirebaseRecyclerOptions<MainModel> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull MainAdapter.myViewHolder holder, int position, #NonNull MainModel model) {
holder.categoryName.setText(model.getName());
holder.categoryDesc.setText(model.getDescription());
Glide.with(holder.categoryImage.getContext())
.load(model.getImage())
.placeholder(R.drawable.common_google_signin_btn_icon_dark)
.error(R.drawable.common_google_signin_btn_icon_dark_normal)
.into(holder.categoryImage);
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item,parent,false);
return new myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder{
ImageView categoryImage;
TextView categoryName, categoryDesc;
public myViewHolder(#NonNull View itemView) {
super(itemView);
categoryImage = (ImageView)itemView.findViewById(R.id.categoryImage);
categoryName = (TextView)itemView.findViewById(R.id.categoryName);
categoryDesc = (TextView)itemView.findViewById(R.id.categoryDesc);
}
}
}
MainModel.class
public class MainModel {
String name,description,image;
MainModel()
{
}
public MainModel(String name, String description, String image) {
this.name = name;
this.description = description;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}

Add this in your OnBindviewHolder method of Adapter
context = holder.itemView.getContext();
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, YourActivity.class);
context.startActivity(i);
}
});

Related

When I click on the RecyclerView Item in MainActivity I want the TextView in MainActivity to display Item's title

>>>>>>>MAIN ACTIVITY XML<<<<<<<
As you can see the TextView is on the top, that's where I want to display the title of selected RecyclerView item. Most of the tutorials and blog teach to navigate to next activity when an item is clicked, but that isn't something I am looking for.
MainActivity.java
public class MainActivity extends AppCompatActivity implements ItemAdapterCallback {
private RecyclerView recyclerView;
private List<Item> itemList;
private ItemAdapter itemAdapter;
private SearchView searchView;
TextView itemname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize(savedInstanceState);
initializeLogic();
}
private void initialize(Bundle savedInstanceState) {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchView = (SearchView) findViewById(R.id.searchView);
searchView.clearFocus();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
filterList(newText);
return true;
}
});
}
private void filterList(String newText) {
List<Item> filteredList = new ArrayList<>();
for (Item item : itemList) {
if (item.getItemName().toLowerCase().contains(newText.toLowerCase())){
filteredList.add(item);
}
}
if (filteredList.isEmpty()) {
Toast.makeText(this, "No Match", Toast.LENGTH_SHORT).show();
} else {
itemAdapter.setFilteredList(filteredList);
}
}
private void initializeLogic() {
RecyclerView();
}
private void RecyclerView () {
itemList = new ArrayList<>();
// Adding Data to Item List
itemList.add(new Item("Black", R.drawable.android_black));
itemList.add(new Item("Blue", R.drawable.android_blue));
itemList.add(new Item("Green", R.drawable.android_green));
itemList.add(new Item("Pink", R.drawable.android_pink));
itemList.add(new Item("Red", R.drawable.android_red));
itemList.add(new Item("Yellow", R.drawable.android_yellow));
itemList.add(new Item("Black", R.drawable.android_black));
itemList.add(new Item("Blue", R.drawable.android_blue));
itemList.add(new Item("Green", R.drawable.android_green));
itemList.add(new Item("Pink", R.drawable.android_pink));
itemList.add(new Item("Red", R.drawable.android_red));
itemList.add(new Item("Yellow", R.drawable.android_yellow));
itemAdapter = new ItemAdapter(itemList, this);
recyclerView.setAdapter(itemAdapter);
}
#Override
public void onItemClicker() {
//????????????????
}
}
Item.java
public class Item {
private String itemName;
private int itemImage;
public Item(String itemName, int itemImage) {
this.itemImage = itemImage;
this.itemName = itemName;
}
public int getItemImage() {
return itemImage;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
ItemAdapter.java
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
private List<Item> itemList;
int row_index = -1;
private ItemAdapterCallback callback;
public ItemAdapter(List<Item> itemList, ItemAdapterCallback callback) {
this.itemList = itemList;
this.callback = callback;
}
public void setFilteredList(List<Item> filteredList) {
this.itemList = filteredList;
notifyDataSetChanged();
}
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_item, parent, false );
return new ItemViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, int position) {
Item item = itemList.get(position);
holder.itemNameTv.setText(item.getItemName());
holder.itemImageView.setImageResource(item.getItemImage());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
////////////////////////////
//Toast.makeText(view.getContext(), "Item is clicked" + item.getItemName(), Toast.LENGTH_SHORT).show();
}
});
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
row_index = position;
notifyDataSetChanged();
Toast.makeText(v.getContext(), "Item is clicked " + position + " " + item.getItemName(), Toast.LENGTH_SHORT).show();
callback.onItemClicker();
}
});
if (row_index == position) {
holder.cardView.setBackgroundColor(Color.LTGRAY);
} else {
holder.cardView.setBackgroundColor(Color.WHITE);
}
}
#Override
public int getItemCount() {
if (itemList == null) {
return 0;
} else {
return itemList.size();
}
}
public class ItemViewHolder extends RecyclerView.ViewHolder{
public TextView itemNameTv;
public ImageView itemImageView;
public CardView cardView;
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.eachCardView);
itemNameTv = itemView.findViewById(R.id.eachItemTextView);
itemImageView = itemView.findViewById(R.id.eachItemImageView);
}
}
}
ItemAdapterCallback (Interface)
public interface ItemAdapterCallback {
void onItemClicker();
}
You are just missing few things here.
1- if you want to get the title from the adapter you will need to pass the title in your Callback Interface
public interface ItemAdapterCallback {
void onItemClicker(String title);}
2- after this change you will override the onItemClickerCallback in your mainActivity
it will look like this
#Override
public void onItemClicker(String title) {
// your title ,
itemname.setText(title);
}
3- in onClick function of the adapter add this line
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(callback!=null){
callback.onItemClicker( item.getItemName());
}
}
});
that's all you need to do to show the title in your mainActivity from adapter.

Show the latest item firest in Firebase ui Realtime database [duplicate]

I try to use the FirebaseUI-Realtime Database Android lib to simply display a data set in a viepager2 with recycler-view adapter
When I get the data, I got the oldest data first, and i need to show the latest first . So I need a reverse order. How i can do it in RecyclerView adapter?
MainActivity.java
public class MainActivity extends AppCompatActivity {
ViewPager2 viewPager2;
videoadapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
viewPager2 = (ViewPager2) findViewById(R.id.vpager);
FirebaseRecyclerOptions<videomodel> options = new FirebaseRecyclerOptions.Builder<videomodel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("videos"), videomodel.class).build();
adapter = new videoadapter(options);
viewPager2.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
}
videoadapter.java
public class videoadapter extends
FirebaseRecyclerAdapter<videomodel, videoadapter.myviewholder> {
private Context context;
private AppCompatActivity activity;
private Timer _timer = new Timer();
private double positionc;
private TimerTask check;
private ProgressBar progressbar11;
private ViewPager2 viewPager;
public videoadapter(#NonNull FirebaseRecyclerOptions<videomodel> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myviewholder holder, int position, #NonNull videomodel model) {
holder.setdata(model);
holder.simpleExoPlayer.prepare();
}
#NonNull
#Override
public myviewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_video_row, parent, false);
return new myviewholder(view);
}
#Override
public void onViewAttachedToWindow(videoadapter.myviewholder holder) {
super.onViewAttachedToWindow(holder);
holder.playerView.getPlayer().seekTo(0);
holder.playerView.getPlayer().setPlayWhenReady(true);
holder.playerView.getPlayer().prepare();
}
#Override
public void onViewDetachedFromWindow(videoadapter.myviewholder holder) {
super.onViewDetachedFromWindow(holder);
holder.playerView.getPlayer().setPlayWhenReady(false);
holder.playerView.getPlayer().stop();
}
class myviewholder extends RecyclerView.ViewHolder {
PlayerView playerView;
SimpleExoPlayer simpleExoPlayer;
LinearLayout sec_mid;
ProgressBar progressbar1;
DefaultTrackSelector trackSelector;
ImageView play,pause;
TextView title,desc;
public myviewholder(#NonNull View view) {
super(view);
playerView = (PlayerView) view.findViewById(R.id.statusSliderVideo);
sec_mid = (LinearLayout) view.findViewById(R.id.sec_controlvid1);
progressbar1 = (ProgressBar) view.findViewById(R.id.progressbar11);
play = (ImageView) view.findViewById(R.id.exo_play);
pause = (ImageView) view.findViewById(R.id.exo_pause);
title = (TextView) view.findViewById(R.id.textVideoTitle);
desc = (TextView) view.findViewById(R.id.textVideoDescription);
}
void setdata(videomodel obj) {
progressbar1.getProgressDrawable().setColorFilter(Color.WHITE, android.graphics.PorterDuff.Mode.SRC_IN);
progressbar1.setIndeterminate(true);
title.setText(obj.getTitle());
desc.setText(obj.getDesc());
// get data
Uri videoUri = Uri.parse(obj.getUrl());
simpleExoPlayer = new SimpleExoPlayer.Builder(playerView.getContext()).build();
playerView.setPlayer(simpleExoPlayer);
playerView.setKeepScreenOn(true);
MediaItem mediaItem = MediaItem.fromUri(videoUri);
simpleExoPlayer.setMediaItem(mediaItem);
simpleExoPlayer.prepare();
playerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View _view) {
if (simpleExoPlayer.isPlaying()) {
simpleExoPlayer.pause();
} else {
simpleExoPlayer.play();
}
}
});
simpleExoPlayer.addListener(new Player.Listener() {
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == Player.STATE_BUFFERING) {
progressbar1.setIndeterminate(true);
} else {
progressbar1.setIndeterminate(false);
}
if (playbackState == Player.STATE_ENDED) {
simpleExoPlayer.seekTo(0);
simpleExoPlayer.setPlayWhenReady(true);
}
}
});
Activity activity = new Activity();
check = new TimerTask() {
#Override
public void run() {
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
progressbar1.setMax((int) simpleExoPlayer.getDuration());
progressbar1.setProgress((int) simpleExoPlayer.getCurrentPosition());
}
});
}
};
_timer.scheduleAtFixedRate(check, (int) (0), (int) (1));
}
}
}
videomodel
public class videomodel {
String desc,title,url;
public videomodel(String desc, String title, String url) {
this.desc = desc;
this.title = title;
this.url = url;
}
videomodel()
{
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}

Having problem to fetching file from firestore recycle view [duplicate]

This question already has an answer here:
Firebase Android ListView not being displayed
(1 answer)
Closed 1 year ago.
I am an android developer beginner, I am having a problem fetch files from firestore recycle view in the main activity but I can fetch files from fragments.
This is my main activity
public class FoodInfoActivity extends AppCompatActivity {
RecyclerView recyclerView1,recyclerView2;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef;
private NoteAdapter adapter;
FirebaseAuth fAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_info);
fAuth = FirebaseAuth.getInstance();
setUpRecyclerView();
}
private void setUpRecyclerView() {
notebookRef = db.collection("FoodInside");
Query query = notebookRef;
FirestoreRecyclerOptions<FoodAdapter> options = new FirestoreRecyclerOptions.Builder<FoodAdapter>()
.setQuery(query, FoodAdapter.class)
.build();
adapter = new NoteAdapter(options);
RecyclerView recyclerView = findViewById(R.id.fi);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}
This is my Note adapter where i use model class to fetch file
public class NoteAdapter extends FirestoreRecyclerAdapter<FoodAdapter, NoteAdapter.NoteHolder> {
public NoteAdapter(#NonNull FirestoreRecyclerOptions<FoodAdapter> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull NoteHolder holder, int position, #NonNull FoodAdapter foodAdapter) {
holder.textViewTitle.setText(foodAdapter.getRestaurant());
holder.textViewDescription.setText(foodAdapter.getItem());
holder.textViewPriority.setText(String.valueOf(foodAdapter.getPrice()));
}
#NonNull
#Override
public NoteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_recycle_layout,
parent, false);
return new NoteHolder(v);
}
class NoteHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewDescription;
TextView textViewPriority;
public NoteHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.textView95a);
textViewDescription = itemView.findViewById(R.id.textView123a);
textViewPriority = itemView.findViewById(R.id.textView124a);
}
}
}
This is my adapter class ,
public class FoodAdapter {
String Restaurant;
String Item;
String Price;
String item_id;
public FoodAdapter() {
}
public FoodAdapter(String restaurant, String item, String price,String item_id) {
Restaurant = restaurant;
Item = item;
Price = price;
this.item_id = item_id;
}
public String getRestaurant() {
return Restaurant;
}
public void setRestaurant(String restaurant) {
Restaurant = restaurant;
}
public String getItem() {
return Item;
}
public void setItem(String item) {
Item = item;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getItem_id() {
return item_id;
}
public void setItem_id(String item_id) {
this.item_id = item_id;
}
}
Why it is not working in main activity but work fragment. I am just afraid. and tired...working on the project. so need solution of this problem
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
Add this line to main activity.

How to pause ExoPlayer playerView when RecyclerView.SCROLL_STATE_DRAGGING and release player on Destroy

I want to implement a RecyclerView that works similar to Instagram feeds, that contains both Image and Video Items. I have successful implemented the recyclerView to display both image and video items, but I am having issues with pausing the video items when the onScrollListener is on RecyclerView.SCROLL_STATE_DRAGGING I am using ExoPlayer library for the video player"
So, if a video is played and the recyclerView is not pause it keeps on playing that video even if another video is played from the list. Making multiple video playing at the same time.
Below is my implementation Adapter
public class RecyclerAdapter extends RecyclerView.Adapter {
private ArrayList<ListModel> listModelList;
private Context context;
SimpleExoPlayer exoPlayer;
public RecyclerAdapter(ArrayList<ListModel> listModelList, Context context) {
this.listModelList = listModelList;
this.context = context;
}
#Override
public int getItemViewType(int position) {
if (listModelList.get(position).getDescription().equalsIgnoreCase("video")) {
return 1;
} else {
return 0;
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view;
if (viewType == 0) {
view = inflater.inflate(R.layout.recycler_item, parent, false);
return new AdsViewHolder(view);
} else {
view = inflater.inflate(R.layout.video_item, parent, false);
return new VideoVH(view);
}
}
#Override
public void onBindViewHolder(#NonNull final RecyclerView.ViewHolder holder, int position) {
ListModel listModel = listModelList.get(position);
if (listModel.getDescription().equalsIgnoreCase("video")) {
VideoVH videoVH = (VideoVH) holder;
initPlayer(((VideoVH) holder).playerView, listModel.getMedia_url(), ((VideoVH) holder).progressBar);
((VideoVH) holder).titleText.setText(listModel.getTitle());
((VideoVH) holder).descText.setText(listModel.getDescription());
//Play video
} else {
AdsViewHolder adsViewHolder = (AdsViewHolder) holder;
((AdsViewHolder) holder).titleText.setText(listModel.getTitle());
((AdsViewHolder) holder).descText.setText(listModel.getDescription());
Glide.with(context)
.load(listModel.getMedia_url())
.into(((AdsViewHolder) holder).imageView);
}
}
private void initPlayer(final PlayerView playerView, String uri, final ProgressBar progressBar) {
TrackSelector trackSelector = new DefaultTrackSelector();
exoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
playerView.setPlayer(exoPlayer);
exoPlayer.setPlayWhenReady(false);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, "ExoRecyclerView"));
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(uri));
exoPlayer.prepare(videoSource);
playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
exoPlayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
exoPlayer.seekTo(500);
exoPlayer.addListener(new Player.EventListener() {
#Override
public void onTimelineChanged(Timeline timeline, #Nullable Object manifest, int reason) {
}
#Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
#Override
public void onLoadingChanged(boolean isLoading) {
}
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
switch (playbackState) {
case Player.STATE_BUFFERING:
// Log.e(TAG, "onPlayerStateChanged: Buffering video.");
if (progressBar != null) {
progressBar.setVisibility(VISIBLE);
}
break;
case Player.STATE_ENDED:
break;
case Player.STATE_IDLE:
break;
case Player.STATE_READY:
// Log.e(TAG, "onPlayerStateChanged: Ready to play.");
if (progressBar != null) {
progressBar.setVisibility(GONE);
}
break;
default:
break;
}
}
#Override
public void onRepeatModeChanged(int repeatMode) {
}
#Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
}
#Override
public void onPlayerError(ExoPlaybackException error) {
}
#Override
public void onPositionDiscontinuity(int reason) {
}
#Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
#Override
public void onSeekProcessed() {
}
});
}
#Override
public int getItemCount() {
return listModelList.size();
}
class AdsViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView titleText, descText;
public AdsViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.thumbnail);
descText = itemView.findViewById(R.id.desc);
titleText = itemView.findViewById(R.id.title);
}
}
class VideoVH extends RecyclerView.ViewHolder {
PlayerView playerView;
TextView titleText, descText;
ConstraintLayout VV;
ProgressBar progressBar;
ImageView volumeControl;
public VideoVH(#NonNull View itemView) {
super(itemView);
playerView = itemView.findViewById(R.id.playerView);
descText = itemView.findViewById(R.id.desc);
titleText = itemView.findViewById(R.id.title);
volumeControl = itemView.findViewById(R.id.volume_control);
VV = itemView.findViewById(R.id.parent);
progressBar = itemView.findViewById(R.id.progressBar);
}
}
}
MainActivity imoplementation
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
ArrayList<ListModel> listModelList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recyclerView);
ListModel listModel = new ListModel();
listModelList.add(new ListModel("One test",
"https://res.cloudinary.com/dqrs5xew4/image/upload/v1591991924/sample.jpg", "image"));
listModelList.add(new ListModel("One test",
"https://res.cloudinary.com/dqrs5xew4/video/upload/v1595538345/HNG/b5bc3f170b7d5c388410bcca0a501ecd_lnhzwn.mp4", "video"));
listModelList.add(new ListModel("One test",
"https://res.cloudinary.com/dqrs5xew4/image/upload/v1591991924/sample.jpg", "image"));
listModelList.add(new ListModel("One test",
"https://res.cloudinary.com/dqrs5xew4/video/upload/v1595538331/HNG/VID-20180713-WA0009_esbmff.mp4", "video"));
listModelList.add(new ListModel("One test",
"https://res.cloudinary.com/dqrs5xew4/image/upload/v1591991924/sample.jpg", "image"));
initRecyclerView();
}
private void initRecyclerView() {
// use a linear layout manager
LinearLayoutManager layoutManager =
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);
RecyclerAdapter mAdapter = new RecyclerAdapter(listModelList, MainActivity.this);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
}
Model Class
public class ListModel {
private String title;
private String media_url;
private String description;
public ListModel(String title, String media_url, String description) {
this.title = title;
this.media_url = media_url;
this.description = description;
}
public ListModel() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMedia_url() {
return media_url;
}
public void setMedia_url(String media_url) {
this.media_url = media_url;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
I have gone through several question here and on youtube, But they all implemeted just a single view type in the recyclerView.
Any suggestion how I could control the Video Player when the recyclerView is been scrolled? Or a better way I could implement the Instagram feeds like RecyclerView

How to implement onClick in FirestoreRecyclerAdapter to open a new Fragment?

I am trying to implement OnClick in my recyclerview,but i failed miserably with all the searches how to solve this problem.
I want by clicking on my cardview to go to another activity/even better fragment with another expandable lists,but i can't even do this for now.
Any help is highly appreciated
MainActivity code
package com.oleg.firestoretest;
public static final String TAG = "FireLog";
private CollectionReference noteBookRef;
private FirebaseFirestore mFirestore;
private NoteAdapter adapter;
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirestore = FirebaseFirestore.getInstance();
noteBookRef = mFirestore.collection("Notebook");
setupRecyclerView();
}
private void setupRecyclerView() {
Query query = noteBookRef.orderBy("priority", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>()
.setQuery(query,Note.class)
.build();
adapter = new NoteAdapter(options);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
public Note() {
}
public Note(String title, String content, int priority) {
this.title = title;
this.content = content;
this.priority = priority;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public int getPriority() {
return priority;
}
}
public class NoteAdapter extends FirestoreRecyclerAdapter<Note, NoteAdapter.NoteHolder> {
private Context mContext;
public NoteAdapter(#NonNull FirestoreRecyclerOptions<Note> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull NoteHolder noteHolder, int i, #NonNull Note note) {
noteHolder.title.setText(note.getTitle());
noteHolder.content.setText(note.getContent());
noteHolder.priority.setText(String.valueOf(note.getPriority()));
}
#NonNull
#Override
public NoteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item,
parent, false);
return new NoteHolder(v);
}
class NoteHolder extends RecyclerView.ViewHolder {
TextView title;
TextView content;
TextView priority;
public NoteHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title_text);
content = itemView.findViewById(R.id.content_text);
priority = itemView.findViewById(R.id.priority_text);
}
}
}
Here the Code u need:
class NoteHolder extends RecyclerView.ViewHolder {
TextView title;
TextView content;
TextView priority;
CardView cardView;
public NoteHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title_text);
content = itemView.findViewById(R.id.content_text);
priority = itemView.findViewById(R.id.priority_text);
cardView = itemView.findViewById(R.id.cardView);
}
private void bind(){
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(FirstActivity.this, SecoundActivity.class);
startActivity(intent);
}
});
}
}
Fixed it with this code!!!
public NoteHolder(#NonNull final View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.cardview);
title = itemView.findViewById(R.id.title_text);
content = itemView.findViewById(R.id.content_text);
priority = itemView.findViewById(R.id.priority_text);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemView.getContext().startActivity(new Intent(itemView.getContext(),Empty.class));
}
});
}

Categories