Values from RecyclerView item to other Activity - android

I'm trying to send values from a RecyclerView Item with X position, to other activity which displays those values, for example, in my RecyclerView my X item, has an Image, a Title and a Subtitle; and I want onClick to be displayed in other activity that Image, Title and Subtitle. By that way for the rest of my items.
I think it should be done with putExtra but I canĀ“t get it.
Here is my Adapter with ViewHolder:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
private LayoutInflater inflater;
private List<Data> info = Collections.emptyList();
private Context context;
public MyAdapter (Context context, List<Data> info){
this.context=context;
inflater = LayoutInflater.from(context);
this.info = info;
}
#Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.recycler_item, parent, false);
myViewHolder holder = new myViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(myViewHolder holder, int position) {
Data current = info.get(position);
holder.title.setText(current.title);;
holder.subtitle.setText(current.subtitle);
holder.image.setImageResource(current.imagenId);
}
#Override
public int getItemCount() {
return info.size();
}
class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView image;
TextView title;
TextView subtitle;
public myViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.Image1);
title = (TextView) itemView.findViewById(R.id.TextTitle);
subtitle = (TextView) itemView.findViewById(R.id.TextSubTitle);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent intent = new Intent(context,ItemSelection.class);
context.startActivity(intent);
}
}
}
Here is the activity of my fragment where the RecyclerView is shown:
public class ltfg0 extends Fragment {
private RecyclerView recyclerView;
private MyAdapter adapter;
public ltfg0() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_ltfg0, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.ltfg0);
adapter = new MyAdapter(getActivity(),getInfo());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
public static List<Data> getInfo(){
List<Data> info = new ArrayList<>();
int[] srcs = {R.mipmap.img1, R.mipmap.img2, R.mipmap.img3, R.mipmap.img4};
String[] titles = {"Title 1", "Title 2", "Title 3", "Title 4"};
String[] subtitles = {"Description 1", "Description 2", "Description 3", "Description 4"};
for (int i=0; i<srcs.length && i<titles.length && i<subtitles.length;i++){
Data current = new Data();
current.imagenId = srcs[i];
current.title = titles[i];
current.subtitle = subtitles[i];
info.add(current);
}
return info;
}
}
And my Data class:
public class Data {
public int imagenId;
public String title;
public String subtitle;
}

You can do it like this inside your ViewHolder:
#Override
public void onClick(View view) {
Intent intent = new Intent(context,ItemSelection.class);
Bundle bundle = new Bundle();
bundle.putSerializable("DATA",info.get(getAdapterPosition()));
intent.putExtras(bundle);
context.startActivity(intent);
}
Then retrieve your instance in your Activity#onCreate() via getIntent():
Data data = (Data)getIntent().getExtras().getSerializable("DATA");
//if you have a TextView, for example...
yourTextView.setText(data.getTitle());
Off course, your Data class must implement Serializable:
public class Data implements Serializale { ...

Related

How to set RecyclerView OnClickListener an OnLongClickListener in Fragments?

I'm trying to start a new Fragment that shows the details of the item that was clicked on in the RecyclerView. The RecyclerView is on a Fragments. I've tried this in Activities and it worked perfectly but I'm trying to convert it to use on Fragments. And Also, how to detect other gestures in the RecyclerView (swipe, etc...)
My Adapter :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private final ArrayList<Tip> dataSet;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewType;
//TextView textViewDetail;
ImageView imageViewImage;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewTitle = itemView.findViewById(R.id.title);
this.textViewType = itemView.findViewById(R.id.type);
//this.textViewDetail = itemView.findViewById(R.id.detail);
this.imageViewImage = itemView.findViewById(R.id.itemImage);
}
}
public MyAdapter(ArrayList<Tip> data) {
this.dataSet = data;
}
#NonNull
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate( R.layout.item, parent, false);
view.setOnClickListener(HomeFragment.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int listPosition) {
TextView textViewTitle = holder.textViewTitle;
TextView textViewType = holder.textViewType;
//TextView textViewDetail = holder.textViewDetail;
ImageView imageViewImage = holder.imageViewImage;
textViewTitle.setText( dataSet.get(listPosition).getTitle() );
textViewType.setText( dataSet.get(listPosition).getType() );
//textViewDetail.setText( dataSet.get(listPosition).getDetail() );
imageViewImage.setImageResource( dataSet.get(listPosition).getImage() );
}
#Override
public int getItemCount() {
return dataSet.size();
}
}
My Fragment :
public class HomeFragment extends Fragment {
public static RecyclerView.Adapter myAdapter;
private static RecyclerView.LayoutManager myLayoutManager;
private static RecyclerView myRecyclerView;
public static View.OnClickListener myOnClickListener;
private static ArrayList<Tip> myData;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Get the integer from the integers.xml resource file
Resources res = getResources();
int gridColumnCount = res.getInteger(R.integer.grid_column_count);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
// Add the following lines to create RecyclerView
myRecyclerView = view.findViewById(R.id.my_recycler_view);
myRecyclerView.setHasFixedSize(true);
//myLayoutManager = new LinearLayoutManager( view.getContext() );
myLayoutManager = new GridLayoutManager( view.getContext(),gridColumnCount );
myRecyclerView.setLayoutManager( myLayoutManager );
myRecyclerView.setItemAnimator( new DefaultItemAnimator() );
myData = new ArrayList<Tip>();
for (int i = 0; i < MyData.tipTitleArray.length; i++) {
myData.add(new Tip( MyData.tipTitleArray[i], MyData.tipTypeArray[i], MyData.tipDetailArray[i], MyData.id_[i], MyData.tipImageArray[i] ));
}
myAdapter = new MyAdapter(myData);
myRecyclerView.setAdapter(myAdapter);
//On retourne la View
return view;
}
#Override
public void onDestroyView() {
super.onDestroyView();
fr.cnam.smb116.mimi_tips_navdraw.databinding.FragmentHomeBinding binding = null;
}
}
Don't use static listener and recyclerview instance.
Better to add callBack function to handle this.
e.g
interface RecyclerViewItemClickListener<T> {
void onClick(int position, T data);
}
init property in your adapter
RecyclerViewItemClickListener<Tip> listener
constructor of your Adapter
MyAdapter(RecyclerViewItemClickListener<Tip> listener) {
this. listener = listener
}
in
onBindViewHolder() {
holder.setOnClickListener(v -> {
listener.onClick(position, dataSet.get(listPosition));
})
}
In Your Fragment
Implement this interface
public class HomeFragment extends Fragment implements RecyclerViewItemClickListener<Tip> {
}
your adapter instance
myAdapter = new MyAdapter(myData,this);
implement onClick Method in your fragment
void onClick(int position, Tip tip){
// your listener
}

How to send data from adapter to activity and display it in a recyclerview?

I try to pass data from an adapter to an activity and display it in a recyclerview. I am getting the NullPointerException when I run the app. What am I doing wrong?
This is the activity where I want to display data:
public class ListOfChatsActivity extends AppCompatActivity {
Toolbar lToolbar;
RecyclerView lRV;
BottomNavigationView lBnv;
ListOfChatsAdapter adapter;
LinearLayoutManager layoutManager;
ArrayList<String> chatList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_chats);
lToolbar = findViewById(R.id.lToolbar);
lRV = findViewById(R.id.lRV);
lBnv = findViewById(R.id.lBnv);
Intent intent = this.getIntent();
if(intent!=null){
String string = intent.getStringExtra("username");
chatList.add(string);}
adapter = new ListOfChatsAdapter(chatList, getApplicationContext());
layoutManager = new LinearLayoutManager(getApplicationContext());
lRV.setAdapter(adapter);
lRV.setLayoutManager(layoutManager);
}
This is the adapter from where I want to send data:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
ArrayList<User> friendsSearchedList = new ArrayList<>();
Context mContext;
public SearchAdapter(ArrayList<User> friendsSearchedList, Context mContext){
this.friendsSearchedList = friendsSearchedList;
this.mContext = mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_user_item,
parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
final User username = friendsSearchedList.get(position);
holder.text.setText(username.getUsername());
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String iUsername = holder.text.getText().toString();
Intent intent = new Intent();
intent.putExtra("username", iUsername);
}
});
}
#Override
public int getItemCount() {
return friendsSearchedList == null ? 0:friendsSearchedList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView text;
Button button;
RelativeLayout relativeLayout;
public ViewHolder(View itemView){
super(itemView);
text = itemView.findViewById(R.id.sText);
button = itemView.findViewById(R.id.addFriend);
relativeLayout = itemView.findViewById(R.id.relativeLayout);
}
}
}
I don't open the Activity from this Adapter. I just want to send extras. I tried more options, but I don't get it right.
Change 1
adapter = new ListOfChatsAdapter(chatList, getApplicationContext());
Why is the ListOfChatsAdapter being used here? You have included SearchAdapter as the adapter to use in the RecyclerView.
Change 2
The ViewHolder object that is returned by onCreateViewHolder(), should be of type SearchAdapter.ViewHolder and not just ViewHolder. The same applies to the first arguement of onBindViewHolder().
Try these and check

Android master detail with recycler view

I'm learning android and I'm just new to this concept.
This is my adapter class. It shows an error saying that the CategoryList is not an enclosing class. I'm unable to understand the error. Please help me in uderstanding it. Thanks in advance.
public class CategoryAdapter extends RecyclerView.Adapter<CategoryHolder> {
private final Context context;
private final List<Category> categories;
public CategoryAdapter(Context context, List<Category> categories) {
this.context = context;
this.categories = categories;
}
#Override
public CategoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
// Here it's showing an error: CategoryList is not an enclosing class.
return new CategoryHolder(layoutInflater, parent);
}
#Override
public void onBindViewHolder(CategoryHolder holder, int position) {
final Category category = categories.get(position);
holder.bind(category);
}
#Override
public int getItemCount() {
return categories.size();
}
}
This is my first fragment. It contains the list of categories such as google, facebook etc. On click on any of these items, it should open another fragment with some detail regarding the category.
public class CategoryList extends Fragment {
private RecyclerView recyclerView;
public CategoryList() { }
private void updateUI() {
List<Category> categories = new ArrayList<>();
categories.add(new Category(1L, "Google", "Hello world!! This is Google."));
categories.add(new Category(2L, "Facebook", "Hello world!! This is Facebook"));
categories.add(new Category(3L, "WhatsApp", "Hello world!! This is WhatsApp"));
categories.add(new Category(4L, "LinkedIn", "Hello world!! This is LinkedIn"));
CategoryAdapter categoryAdapter = new CategoryAdapter(getActivity(), categories);
recyclerView.setAdapter(categoryAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_category_list, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.category_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
public class CategoryHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private Category category;
private final TextView name;
private final TextView description;
public CategoryHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.category_layout, parent, false));
this.name = (TextView) itemView.findViewById(R.id.category_name);
this.description = (TextView) itemView.findViewById(R.id.category_description);
}
public void bind(final Category category) {
this.category = category;
this.name.setText(category.getName());
this.description.setText(category.getDescription());
}
#Override
public void onClick(View v) {
Fragment fragment = CategoryDetail.getInstance(category);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.detail_container, fragment)
.commit();
}
}
}
This is the fragment that contains the detail of the category. For now I've not written anything in updateUI() method.
public class CategoryDetail extends Fragment {
private Category category;
public CategoryDetail() { }
public static CategoryDetail getInstance(Category category) {
CategoryDetail categoryDetail = new CategoryDetail();
categoryDetail.category = category;
return categoryDetail;
}
private void updateUI() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.category_detail, container, false);
updateUI();
return view;
}
}
You didn't create a ViewHolder class. Based on your code, you would want to create
public class CategoryHolder extends RecyclerView.ViewHolder {
public CategoryHolder(View view) {
//code for the constructor for the ViewHolder goes here.
}
}

Android RecyclerView GridLayoutManager onClick not working

I ran into situation where the RecyclerView with GridLayoutManager is not working on onClick(). Like ListView, when I tap/click on a picture in a GridView, the onClick is not returning the single item details. When I click on a poster of a movie, it should only display the details about the particular movie, but instead it is displaying multiple movies details in a scroll view.
Look at the code below:
public class MoviesGridAdapter extends RecyclerView.Adapter<MoviesGridAdapter.PosterViewHolder> {
private Context context;
private List<Movies> movieItems = new ArrayList<>();
CustomItemClickListener listener;
public MoviesGridAdapter(Context context, List<Movies> movieItems) {
this.context = context;
this.movieItems = movieItems;
}
#Override
public PosterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_grid_view_item, parent, false);
final MoviesGridAdapter.PosterViewHolder vh = new MoviesGridAdapter.PosterViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Context zContext = context;
Intent intent = new Intent(zContext, MoviesDetailActivity.class);
Movies movies = movieItems.get(vh.getAdapterPosition());
intent.putExtra("Items Selection", movies.getPosterPath());
intent.putExtra("Items Selection 1", movies.getTitle());
intent.putExtra("Items Selection 2", movies.getReleaseDate());
intent.putExtra("Items Selection 3", movies.getVoteAverage());
intent.putExtra("Items Selection 4", movies.getOverview());
zContext.startActivity(intent);
}
});
return vh;
}
public void setMoviesData(List<Movies> moviesData) {
movieItems = moviesData;
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(PosterViewHolder holder, final int position) {
Context mcontext = context;
Movies movies = movieItems.get(position);
String posterUrlPath = movies.getPosterPath();
Picasso.with(mcontext)
.load(Constants.MOVIES_POSTER_URL + posterUrlPath)
.into(holder.poster);
}
#Override
public int getItemCount() {
return movieItems.size();
}
public class PosterViewHolder extends RecyclerView.ViewHolder {
ImageView poster;
public PosterViewHolder(final View itemView) {
super(itemView);
poster = (ImageView) itemView.findViewById(R.id.movies_image_view_retrofit);
}
}
}
And following is the Fragment:
public class MoviesFragment extends Fragment {
private static final int COLUMN_COUNT = 2;
private List<Movies> movieItems = new ArrayList<>();
private RecyclerView recyclerView;
private MoviesGridAdapter adapter;
private Subscription subscription;
public MoviesFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.movies_fragment_content, container, false);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), COLUMN_COUNT);
recyclerView = (RecyclerView) view.findViewById(R.id.movies_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
adapter = new MoviesGridAdapter(getActivity(), movieItems);
recyclerView.setAdapter(adapter);
return view;
}
#Override
public void onStart() {
super.onStart();
if (movieItems != null) {
adapter.setMoviesData(movieItems);
}
loadDate();
}
private void loadDate() {
MoviesAPI api = RetrofitManager.getMoviesClient().create(MoviesAPI.class);
subscription = api.getPopularMovies(APIKeys.MOVIES_DB_API_KEY)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
new Action1<MoviesResponse>() {
#Override
public void call(MoviesResponse response) {
movieItems = response.getMovies();
adapter.setMoviesData(response.getMovies());
}
}, new Action1<Throwable>() {
#Override
public void call(Throwable throwable) {
}
}
);
}
}
Thank you guys for the attention. The solution I am looking is: when I click a picture from the grid view, I need the details about the particular movie like title, overview, releaseDate etc.
Make your model parcelable or send your model fields one by one but using a different keyword. You're adding same keyword with different values in here:
intent.putExtra("Items Selection", movies.getPosterPath());
intent.putExtra("Items Selection", movies.getTitle());
intent.putExtra("Items Selection", movies.getReleaseDate());
intent.putExtra("Items Selection", movies.getVoteAverage());
intent.putExtra("Items Selection", movies.getOverview());
Here's a tutorial link how you can make your model parcelable. And after that you can update your adapter as following:
public class MoviesGridAdapter extends RecyclerView.Adapter<MoviesGridAdapter.PosterViewHolder> {
private Context context;
private List<Movies> movieItems = new ArrayList<>();
CustomItemClickListener listener;
public MoviesGridAdapter(Context context, List<Movies> movieItems) {
this.context = context;
this.movieItems = movieItems;
}
#Override
public PosterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_grid_view_item, parent, false);
return new PosterViewHolder(view);
}
public void setMoviesData(List<Movies> moviesData) {
movieItems = moviesData;
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(PosterViewHolder holder, final int position) {
Movies movie = movieItems.get(position);
String posterUrlPath = movie.getPosterPath();
Picasso.with(mcontext)
.load(Constants.MOVIES_POSTER_URL + posterUrlPath)
.into(holder.poster);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, MoviesDetailActivity.class);
intent.putExtra("movie", movie)
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return movieItems.size();
}
public class PosterViewHolder extends RecyclerView.ViewHolder {
ImageView poster;
public PosterViewHolder(View itemView) {
super(itemView);
poster = (ImageView) itemView.findViewById(R.id.movies_image_view_retrofit);
}
}
}
Don't set your click listener in onCreateViewHolder. Instead set your listener in onBindViewHolder method

Send data from CardView in RecyclerView to new fragment?

So I have a CardView in a RecyclerView which has some data inside it. I would like to click on a specific card and launch a new fragment that displays specific data from the data in the CardView (more details about the data in the CardView). Here is the relevant code.
I don't have the most experience in programming and Android in general, any help would be appreciated!
DetailsFragment: `
public class DetailsFragment extends Fragment {
private CardView cardView;
private RecyclerView rv;
private List<Detail> detailList = new ArrayList<>();
public DetailsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_details, container, false);
Context context = getActivity();
cardView = (CardView) rootView.findViewById(R.id.cv);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
final LinearLayoutManager llm = new LinearLayoutManager(context);
rv.setLayoutManager(llm);
RVDetailAdapter adapter = new RVDetailAdapter(detailList);
rv.setAdapter(adapter);
initializeData();
initializeAdapter();
return rootView;
}
private void initializeData() {
detailList = new ArrayList<>();
// not sure here
detailList.add(new Detail(" "));
}
private void initializeAdapter(){
RVDetailAdapter adapter = new RVDetailAdapter(detailList);
rv.setAdapter(adapter);
}
}
RVNewsAdapater:
public class RVNewsAdapter extends RecyclerView.Adapter<RVNewsAdapter.NewsViewHolder> {
public static final String KEY_LINK ="link";
public static class NewsViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private static final String TAG = "Hello";
CardView cv;
TextView date;
TextView link;
TextView title;
TextView today;
public NewsViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
date = (TextView) itemView.findViewById(R.id.date);
link = (TextView) itemView.findViewById(R.id.link);
title = (TextView) itemView.findViewById(R.id.heading);
today = (TextView) itemView.findViewById(R.id.today);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Context context = view.getContext();
Intent intent = null;
for(int postion = 0; postion<42; postion++) {
intent = new Intent(context, Details.class);
}
context.startActivity(intent);
}
}
List<News> news;
RVNewsAdapter(List<News> news) {
this.news = news;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public RVNewsAdapter.NewsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemnewsfeed, viewGroup, false);
NewsViewHolder nvh = new NewsViewHolder(v);
return nvh;
}
#Override
public void onBindViewHolder(NewsViewHolder newsViewHolder, int i) {
newsViewHolder.date.setText(news.get(i).date);
newsViewHolder.link.setText(news.get(i).link);
newsViewHolder.title.setText(news.get(i).title);
newsViewHolder.today.setText(news.get(i).today);
}
#Override
public int getItemCount() {
if (news != null) {
return news.size();
}
return 0;
}
}
RVDetailAdapter:
public class RVDetailAdapter extends RecyclerView.Adapter<RVDetailAdapter.DetailViewHolder> {
public static class DetailViewHolder extends RecyclerView.ViewHolder {
TextView link;
public DetailViewHolder(View itemView) {
super(itemView);
link = (TextView) itemView.findViewById(R.id.link);
}
}
List<Detail> detail;
RVDetailAdapter(List<Detail> detail) {
this.detail = detail;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public RVDetailAdapter.DetailViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemnewsfeed, viewGroup, false);
DetailViewHolder dvh = new DetailViewHolder(v);
return dvh;
}
#Override
public void onBindViewHolder(DetailViewHolder detailViewHolder, int i) {
detailViewHolder.link.setText(detail.get(i).link);
}
#Override
public int getItemCount() {
if (detail != null) {
return detail.size();
}
return 0;
}
}
Not sure if you're still looking for an answer, but I just saw the post and thought I'd give you one option.
When I'm using a RecyclerView (doesn't matter if it's using the CardView or not, it's the same for any list item), I create a a click listener for the view item in the Adapter. When the user clicks the item/cardview, I dispatch an item click event. It's your choice on how you respond - you can use a standard callback interface (and pass in your listener when you create the adapter) or use something like EventBus.
Here's an example of the code you would add to your adapter class:
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemClick(view);
}
});
return new ViewHolder(view);
}
private void itemClick(View view) {
//Handle item event here
//EventBus.getDefault().post(new ItemSelectedEvent(/*send apprpopriate data*/));
}

Categories

Resources