I'm working in the application of Android approached to finish it but I found the problem in recyclerView, and i didn't know how to get the text of an Item of the recyclerView ? (when i click in the item)
First you need to create a listener that gives as argument the view and the position. It dosent exist directly like for the listView. For that you can create a class like this :
public class RecyclerClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
GestureDetector mGestureDetector;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public RecyclerClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildLayoutPosition(childView));
return true;
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
Then within your activity :
recyclerView.addOnItemTouchListener( // and the click is handled
new RecyclerClickListener(getActivity(), new RecyclerClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
Log.d("click item", String.valueOf(position));
TextView textView = (TextView) view.findViewById(R.id.YourTextViewID);
Log.d("test",textView.getText().toString());
}
}
}));
Related
My question is asked before in many threads such as
How do I make RecyclerView update its layout?.
But unfortunately none of them fixed my issue.
I am using recycleview in my android project. for each item I have hidden checkbox and I am trying to make it visible for all items when long pressing. I am trying to call: adapter.notifyItemChanged() in such case but for nothing. sometimes It updates and sometimes does not and in case of updating it updadtes some items and left others.
Here is my code:
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemLongClick(View view, int position) {
CheckBox checkBox = findViewById(R.id.checkBox);
checkBox.setVisibility(View.VISIBLE);
adapter.notifyItemChanged(position);
}
}));
and my listener class is:
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
private OnItemClickListener mListener;
private GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null) {
mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
I have solved my issue by adding a boolean flag in the adapter. Then, In the method onBindViewHolder I check this flag and depending on it I show or hide Items' checkboxes as following:
if(flag){
holder.itemView.findViewById(R.id.checkBox).setVisibility(View.VISIBLE);
}
Finally in the long click method I set that flag and call notifyDataSetChanged method to update RecycleView's layout as following:
adapter.flag = true;
adapter.notifyDataSetChanged();
i am showing json data in recyclerview. when i click any item of the recyclerview it should showing some new data. its working fine but the problem is, when i click its getting queued, like if i click tow item one after another, recyclerview will show items for twice.how to solve this issue?
i have tried by setting addOnItemTouchListener null after completion the click, but its showing null pointer exception on next click.
my click listener class below:
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
}
You can also use recycle view click listener below is the class and you can esaily use it.
public class RecyclerOnClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerOnClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
#Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
#Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
#Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}
In Activity also you can use it like this
recyclerview_productList.addOnItemTouchListener(
new RecyclerOnClickListener(AgentCustomerActivity.this, recyclerview_productList, new RecyclerOnClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(AgentCustomerActivity.this, AgentCutomerOrderDetailActivity.class);
intent.putExtra("agent_cutomerId", list.get(position).getCustomerId());
intent.putExtra("agent_cutomerName", list.get(position).getCustomerName());
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
#Override
public void onLongItemClick(View view, int position) {
}
}));
I have a list view of songs with a play button
..
Now I need to click on this play buttons based on their positions and after click change the selected one into pause button .. but i have to do this inside my activity not in the adapter(getView).. Any suggestions !?
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//productList is my ArrayList<Product> and
//Product is my Serializable class that holds
// items that is in mysql database
Product selectedProduct = productList.get(position);
}
});
Firstly, use a recyclerview instead of a listview. Afterwards, inside the adapter in onBindViewHolder method, handle clicks by addind a click listener to holder :
holder.setOnClickListener(...);
interface:
public interface OnItemClicked{
void onItemClicked(/*HERE YOUR VARIABLES TO PASS FROM ADAPTER TO MAINACTIVITY);
}
in adapter:
OnItemClicked listener;
public void setOnItemClickedListener(OnItemClicked listener){
this.listener = listener;
}
downwards, on onBindViewHolder method :
#Override
public ViewHolder onBindViewHolder(... params){
...your code
holder.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View v){
if(listener != null)
listener.onItemClicked()
}
});
}
and, finally, in your MainActivity:
adapter.setOnItemClickedListener(new OnItemClicked ... ) <- here will pop up autocomplete
use this in your activity
Declare RecyclerView and set Layout Manager
set Adapter
view.setAdapter(mAdapter);
perform on click
v
view.addOnitemTouchListener(
new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
//do something
}
})
);
RecyclerItemClickListener :
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
I am using android recyclerview with cardview, which populates with the data from server. I want to set itemclick event for each item and need to display a dialog with a text. this text also getting from server. it will be different for diferent items.
when I am trying like given,
mItem=lists.getRemarks();
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new SweetAlertDialog(v.getContext())
.setTitleText("Remark")
.setContentText(mItem)
.show();
}
});
it only gives the last details
somehow i solved it by setting setContentText(mItem) to setContentText(lists.getRemarks())
Recyclerview does not have its item click listener so I have one custom class to detect it
RecyclerItemClickListner
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener
{
public static interface OnItemClickListener
{
public void onItemClick(View view, int position);
public void onItemLongClick(View view, int position);
}
private OnItemClickListener mListener;
private GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener)
{
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener()
{
#Override
public boolean onSingleTapUp(MotionEvent e)
{
return true;
}
#Override
public void onLongPress(MotionEvent e)
{
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if(childView != null && mListener != null)
{
mListener.onItemLongClick(childView, recyclerView.getChildPosition(childView));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e)
{
View childView = view.findChildViewUnder(e.getX(), e.getY());
if(childView != null && mListener != null && mGestureDetector.onTouchEvent(e))
{
mListener.onItemClick(childView, view.getChildPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent){}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
Use it
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
// do whatever you want on position clicked
}
#Override
public void onItemLongClick(View view, int position) {
// do whatever you want on position long press
}
}));
I'm developping an app with a RecyclerView to show different "posts", each item has a like button which works perfectly fine but I'm now having a hard time because I want to show the details of a post in a seperate activity if the user clicks on it.
How can I start a new activity if the user clicks on anything except the like button ?
Try this,
recycleview.addOnItemTouchListener(
new RecyclerItemClickListener(Activity, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View v, int position) {
startActivity(new Intent(Activity,otherclass.class));
}
})
);
ADD this Class,
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}