i have followed this tutorial of a RecyclerView with Cardviews and now i would like to open a new activity to show the detailed information of the cardview the user clicks on.
To this action you need to implement an Interface and implement it in your MainActivity , like a listener.
OR
You can use a onClickListener for any particular view. But if you want to start activity for the itemView adapter layout use the holder.itemView clicklistener in the onBindViewHolder method.
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
context.startActivity(new Intent(context,YOUR_ACTICITY.class));
}
});
Related
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
try {
Timeline timeline = dataSnapshot.getValue(Timeline.class);
timelineArrayList.add(timeline);
adapter= new TimelineAdapter(timelineArrayList);
timelineRecyclerView.setLayoutManager(layoutManager);
timelineRecyclerView.setAdapter(adapter);
adapter.notify();
} catch (Exception ex) {
System.out.print(ex.toString());
}
timelineRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(getContext(), ImageViewer.class);
intent.putExtra("img", timelineArrayList.get(position).getPhoto());
startActivity(intent);
}
}));
addOnItemTouchListener starting activity multiple times what should i do. I tired by adding flags, by finishing activity once clicked.
Nothing worked for me so i decided to use list view and listview.onItemClickListner()
it worked for me. Thank you everyone
https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener#summary
Use OnClickListener instead of addOnItemTouchListener
RecyclerView.OnItemTouchListener is meant to be used when you want to detect a touch to an item while the list is scrolling, not for regular or direct touches
An OnItemTouchListener functions a bit differently than the normal OnItemClickListener. Using the OnItemTouchListener, it is possible to allow the application to intercept touch events from the View hierarchy. What this basically means is that you can implement various forms of gesture manipulation like swipe straight into the Views of your RecyclerView.
When should you use it?
An OnItemClickListener should be used when you
need to determine what happens when the user clicks on a View in your
RecyclerView. This could be deleting something or starting up a new
activity. The OnItemTouchListener Is generally used to create gestural
interactivity to certain Views in your RecyclerView.
Try to provide onClickListener from ViewHolder to UI(Activity/Fragment) with use of Interface pattern and then start activity
Please Add a OnclickListener to a view in your View Holder It will Work
Here is An example
public class CustomViewHolder extends RecyclerView.ViewHolder{
Button add;
public CustomViewHolder(View itemView) {
super(itemView);
add = itemView.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
Why don't you use Onclick listener
I am implementing an app that display list of data on a RecycleView. After swipe a recycleview item it display a specific LinearLayout view. In that view I have implemented an onClickListner. But it's not calling on click.
You must create an OnItemClickListener() interface.
Then, put this in your onBindViewHolder method:
holder.bind(items.get(position), listener);
Next time, put the bind method inside of your Adapter:
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick(item);
}
});
And finally, in your MainActivity inside of your adapter declaration open the listener like this:
recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
#Override public void onItemClick(ContentItem item) {
Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show();
}
}));
If you want to read more check out this documentation
I have an Activity a with a ListView lv inside.
I have set the lv.setOnItemClickListener().
When the user clicks on a list item i want another list view (ListView lv_2) in the Activity a to be refreshed.
Problem is that i cannot access the Parent Activity a inside lv.setOnItemClickListener() am i right?
I studied some of custom event listeners but i don't understand how to use them in this particular case.
So how can i do this?
lv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
lv2Adapter.notifyDataSetChanged();
lv2.setAdapter(lv2Adapter);
lv2.notify();
}
});
item of my recycler view has some different views which has to have a click listener if i add a click listener to recycler view with view and position parameters it always take the layout behind the views on click. Therefore, i am setting my click listeners in onBindViewHolder like this:
holder.myTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// stuff
}
});
holder.myImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// stuff
}
});
I wonder whether it is a correct method?
Where should a click listener be in a recycler view?
There are many ways for achieve this, but ;
Both is correct as i see on your codes.Means, if you set those items with myTextView, the users will be able to click on TextView (f.g) for showing that RecyclerView item content or whatever and other stuffs like ImageView won't work for listener.
And this is correct for myImageView too, if you set those items with it for listener, the users will be able to click only in this myImageView.
By the way, i think it is better to use implements View.OnClickListener for whole of the Holder.
Something like this:
https://stackoverflow.com/a/24471410/4409113
or Jacob's answer
I am implementing common header in all activity.I want to implement click event for header.can anybody tell how to implement click event in BaseActivity(Parent Activity) in android.I am getting reference in parent activity .Is it possible to implement click using setOnClickListener in parent activity?
txtHeading =(TextView)findViewById(R.id.txtHeading);
I want to implement click event for textview in parent activity
Any help would be highly appreciated
I usually create some helper method on the parent activity
public void setHeaderOnClick(View.onClickListener clickListener){
txtHeading =(TextView)findViewById(R.id.txtHeading);
txtHeading.setOnClickListener(clickListener);
}
when on the fragment, you can use
((YourActivityName)getActivity()).setHeaderOnClick(new View.OnClickListener() {
#Override
public void onClick(View v) {
//method here
}
});
hope it helps
yes you can do it like...
1.)in your header a xml in the textView set these properties.
android:clickable="true"
android:onClick="onClick"
2.) write the direct public void onClick() in your BaseActivity Like
public void onClick(View v){
if(v.getId() == R.id.txtHeading){
}
}
it will working charm yes after doing these steps don't find your text view component in BaseActivity.