thanks for helping. I was trying to implement an add to favorite function in my recycler view, everything is working fine except for one thing. I am unable to commit the changed image button in my recycler view, every time I press the button, the post is added to the favorites and the image button turns yellow, but as soon as I move to some other fragment, the yellow button goes back to its initial stage. Can anyone of you help me on how can I make my button commit to the changes in recycler view. Below is my relevant code.
Initialization of buttons in holder class:
class UsersViewHolder1 extends RecyclerView.ViewHolder {
View mView;
private ImageButton mFavouritesBlack, mFavouritesYellow;
private GestureDetector mGestureDetector;
private Heart mHeart;
public UsersViewHolder1(View itemView) {
super(itemView);
mView = itemView;
mFavouritesBlack = mView.findViewById(R.id.ad_start_fav);
mFavouritesYellow = mView.findViewById(R.id.ad_start_fav1);
}
}
OnBindViewHOlder class(I omitted the irrelevant code):
protected void onBindViewHolder(#NonNull final UsersViewHolder1 Holder, final int position, #NonNull
Ad ad) {
Holder.setTitle(ad.getTitle());
Holder.setPrice(ad.getPrice());
Holder.setCategory(ad.getCategory());
Holder.setImage(ad.getImage(), getContext());
Holder.setTime(ad.getTime());
String user_id = getRef(position).getKey();
final String kk = user_id.toString();
Holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mew = new Intent(getActivity(), ExpandActivity.class);
mew.putExtra("user_id", kk);
startActivity(mew);
}
});
Holder.mFavouritesBlack.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).child("fav_status").setValue("Added as fav").addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(),"Added to Fav",Toast.LENGTH_SHORT).show();
if (Holder.mFavouritesBlack.isShown())
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}
});
return true;
}
});
Holder.mFavouritesYellow.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(),"Removed from Fav",Toast.LENGTH_SHORT).show();
if (Holder.mFavouritesYellow.isShown())
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
});
return true;
}
});
}
What you are trying to implement is selection inside a recyclerview which requires saving state, that is you will have to save the favourite items say in a SparseBooleanArray which can be used to save favourite adapter positions, and inside onBindViewHolder check if the favourites array contains the adapter position and toggle imageview (There is no need to use two images instead you can change the color of drawable), please refer this artcle
That is because you are saving the flags when a click happened,but when the views are redrawn you are not resetting the images accordingly. Inside the onBindViewHolder add the following code.
//Replace the if condition statement with the state value check(Abstract code shown below)
if(mFav.child(puid).child(kk).child("fav_status").getValue(),equals("Added as fav")){
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}else{
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
After 2 days of messed up searches and work I finally removed my the problem was that recycler view isn't able to hold the state by itself after change, that's why we need to provide a way to store the state (or the image in my case) so that it can remember it everytime it recycles the view, but instead I added the direct statement through which it checks whether the favourite node is present in database or not and based on that the buttons behaved. Below is the code.
mFav.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(puid).hasChild(kk)){
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}else{
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Holder.mFavouritesBlack.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).child("fav_status").setValue("Added as fav").addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Added to Fav", Toast.LENGTH_SHORT).show();
if (!itemStateArray.get(adapterPosition, false))
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}
});
return true;
}
});
Holder.mFavouritesYellow.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Removed from Fav", Toast.LENGTH_SHORT).show();
if (itemStateArray.get(adapterPosition, false))
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
});
return true;
}
});
But huge thanks to everyone who tried to help.
Related
I use a GestureListener on an image to detect a Double Tap.
holder.image.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return holder.myDoubleTapDetector.onTouchEvent(event);
}
});
And I use the library Zoomy to make the images zoomable.
Zoomy.Builder builder = new Zoomy.Builder(mActivity).target(holder.image);
builder.register();
Both work perfectly fine for them alone.
But adding both at the same time does not work! Only the one that was added last works.
How can I have both of them working?
Zoomy provides a solution for that:
Because Zoomy works by attaching a View.OnTouchListener to the registered View, View.OnClickListener can not be set along with Zoomy, so a TapListener, LongPressListener and DoubleTapListener are provided to ensure the View still can listen for gestures. A ZoomListener is also provided if you are interested in zoom events.
In Code:
Zoomy.Builder builder = new Zoomy.Builder(this)
.target(mZoomableView)
.tapListener(new TapListener() {
#Override
public void onTap(View v) {
//View tapped, do stuff
}
})
.longPressListener(new LongPressListener() {
#Override
public void onLongPress(View v) {
//View long pressed, do stuff
}
}).doubleTapListener(new DoubleTapListener() {
#Override
public void onDoubleTap(View v) {
//View double tapped, do stuff
}
})
.zoomListener(new ZoomListener() {
#Override
public void onViewStartedZooming(View view) {
//View started zooming
}
#Override
public void onViewEndedZooming(View view) {
//View ended zooming
}
});
This issue is driving me crazy. I've tried 20 different things by now. So let's see if someone here can help me.
I am making a ToDo list APP using RecyclerView that stores the data in Firebase.
The app has a TasksActivity where all the tasks appear in the RecyclerView. I have a button that goes to a Task Creation dialogue. And I can create tasks, they appear then in the RecyclerView (Tasks Activity) and are updated in the firebase with no issue. I can close the APP and come back later and everything works well, all the entries appear in the app again when I load it. I can also swipe to delete an entry, and the entry also gets eliminated from the Database in firebase.
The problem, is when I create a TASK and without closing the app, I try to delete the task that I just created. It doesn't allow me to do that. When I create a new task, and instantly delete it before closing the app, it appears again. But If I then close the app, and load it again, that entry can be deleted normally, but if I am in the same session in which I created the entry there's no way I can delete it.
I was using some Log.d arguments to see how it changes. I think for various reasons that the problem is OnDataChange(). But so far I haven't been able to come to the root of the issue. This is the TaskActivity class, and after this I will paste the TasksCreation (I don't think pasting the Adapter is necessary).
public class TasksActivity extends AppCompatActivity {
DatabaseReference reference;
RecyclerView myTasks;
ArrayList<TaskItems> myTasksList;
TasksAdapter tasksAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasks);
myTasks = findViewById(R.id.my_tasks); // RecyclerView that I defined as part of the layout. This is the id of it
myTasks.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
myTasksList = new ArrayList<>();
Button openCreateTask = findViewById(R.id.openCreateTask);
tasksAdapter = new TasksAdapter(this,myTasksList);
myTasks.setAdapter(tasksAdapter);
new ItemTouchHelper(itemTouchHelper).attachToRecyclerView(myTasks);
openCreateTask.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent_task = new Intent(getApplicationContext(), TasksCreation.class);
startActivity(intent_task);
}
});
reference = FirebaseDatabase.getInstance().getReference().child("MotApp"); // Name of the App in the database .child("MotApp")
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) { // It gets the info from the database
Log.d("data Changed called", "onDataChange: is called");
Log.d("whatever", "onDataChange BEGIN Array of myTasksList size is "+myTasksList.size());
myTasksList.clear(); // Added later to avoid duplication
for(DataSnapshot elements: dataSnapshot.getChildren()){
TaskItems p = elements.getValue(TaskItems.class);
myTasksList.add(p);
}
tasksAdapter.notifyDataSetChanged(); // If this is put outside of onDataChange, it displays a blank list.
Log.d("whatever", "onDataChange END Array of myTasksList size is "+myTasksList.size());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "No data", Toast.LENGTH_SHORT).show();
}
});
}
ItemTouchHelper.SimpleCallback itemTouchHelper = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
Log.d("ARRAY SIZE", "onSwiped BEGIN Array of myTasksList size is "+myTasksList.size());
String key = myTasksList.get(position).getKey();
reference= FirebaseDatabase.getInstance().getReference().child("MotApp").child(key);
Toast.makeText(getApplicationContext(),"This is key "+key,Toast.LENGTH_LONG).show();
reference.removeValue();
myTasksList.remove(position);
tasksAdapter.notifyItemRemoved(position);
Log.d("ARRAY SIZE", "onSwiped END Array of myTasksList size is "+myTasksList.size());
}
};
}
And this is for TasksCreation Activity:
public class TasksCreation extends AppCompatActivity {
DatabaseReference referenceCreation;
ArrayList<String> list;
EditText taskName;
EditText taskDescr;
Button selectDates;
TextView taskDate;
Button createTask;
Button cancel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasks_creation);
taskName = findViewById(R.id.et_TaskName);
taskDescr = findViewById(R.id.et_TaskDescr);
selectDates = findViewById(R.id.selectDates);
taskDate= findViewById(R.id.tv_Dates);
createTask = findViewById(R.id.createTask);
cancel = findViewById(R.id.cancelButton);
createTask.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!TextUtils.isEmpty(taskName.getText()) && !TextUtils.isEmpty(taskDate.getText())) {
referenceCreation = FirebaseDatabase.getInstance().getReference().child("MotApp").push(); //saves it with custom key created by Firebase
final String key = referenceCreation.getKey();
referenceCreation.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
dataSnapshot.getRef().child("key").setValue(key);
dataSnapshot.getRef().child("taskTitle").setValue(taskName.getText().toString());
dataSnapshot.getRef().child("taskDescription").setValue(taskDescr.getText().toString());
dataSnapshot.getRef().child("taskDate").setValue(taskDate.getText().toString());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Intent intent = new Intent(getApplicationContext(), TasksActivity.class);
startActivity(intent);
} else{
Toast.makeText(getApplicationContext(), "You haven't filled all the fields", Toast.LENGTH_SHORT).show();
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), TasksActivity.class);
startActivity(intent);
}
});
selectDates.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createAlertDialogue();
}
});
}
private void createAlertDialogue(){
list = new ArrayList<String>();
AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyDialogTheme);
builder.setTitle("Select days");
builder.setMultiChoiceItems(R.array.Days, null, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
String arr[] = getResources().getStringArray(R.array.Days);
if(isChecked){
list.add(arr[which]);
}else if(list.contains(arr[which])){
list.remove(arr[which]);
}
}
});
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
String data = "";
#Override
public void onClick(DialogInterface dialog, int which) {
for(String elements: list){
elements = elements.substring(0,3);
data= elements+" "+data;
}
taskDate.setText(data);
}
});
builder.create();
builder.show();
}
}
I really appreciate any light to the issue.
Thanks a lot
P:S: This is the code for the TaskAdapter (Ignore the dayoftheweek part is just for a part of the code that I commented )
public class TasksAdapter extends RecyclerView.Adapter<TasksAdapter.MyViewHolder> { // V 1.3 added OnClickListener
Context context;
ArrayList<TaskItems> tasks;
DatabaseReference reference;
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date();
final String dayOfTheWeek = sdf.format(d).substring(0,3);
public TasksAdapter(Context context, ArrayList<TaskItems> tasks) {
this.context = context;
this.tasks = tasks;
}
public class MyViewHolder extends RecyclerView.ViewHolder { // V1.3 Added implements View.OnClickListener
TextView taskTitle;
TextView taskDate;
TextView taskDescription;
CheckBox taskCheckBox;
ConstraintLayout constraintLayout; // Added to change background of each RecyclerView item.
public MyViewHolder(#NonNull View itemView) {
super(itemView);
taskTitle = itemView.findViewById(R.id.taskTitle);
taskDate = itemView.findViewById(R.id.taskDate);
taskDescription = itemView.findViewById(R.id.taskDescription);
taskCheckBox = itemView.findViewById(R.id.taskCheckBox);
constraintLayout = (ConstraintLayout) itemView.findViewById(R.id.item_task_layout);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = getAdapterPosition();
// if(pos != RecyclerView.NO_POSITION){//Checks if item still exists
TaskItems clickedDataItem = tasks.get(pos);
Toast.makeText(v.getContext(), "You clicked " + clickedDataItem.getTaskTitle(), Toast.LENGTH_SHORT).show();
// }
}
});
}
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) { // standard code for onCreateViewHolder
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_task,parent,false));
}
#Override
public void onBindViewHolder(final #NonNull MyViewHolder holder, final int position) { // This method is called once for each item on the list.
holder.taskTitle.setText(tasks.get(position).getTaskTitle());
holder.taskDescription.setText(tasks.get(position).getTaskDescription());
holder.taskDate.setText(tasks.get(position).getTaskDate());
holder.taskCheckBox.setChecked(tasks.get(position).isChecked());
holder.taskCheckBox.setTag(tasks.get(position).getKey());
holder.taskCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
String post = (String) holder.taskCheckBox.getTag();
Toast.makeText(context,"This is checkbox number: "+post,Toast.LENGTH_SHORT).show(); // Working Well. //change for new logic
reference = database.getReference("MotApp").child(post);
boolean checkboxStatus = holder.taskCheckBox.isChecked();
Log.d("Checked", "onClick: The taskcheckbox checked is "+holder.taskCheckBox.isChecked());
TaskItems value = new TaskItems(tasks.get(position).getKey(),tasks.get(position).getTaskTitle(), tasks.get(position).getTaskDate(),tasks.get(position).getTaskDescription(),checkboxStatus);
reference.setValue(value);
Toast.makeText(context,"This is a checkbox belonging to item "+tasks.get(position).getTaskTitle(),Toast.LENGTH_LONG).show();
}
});
}
#Override
public int getItemCount() { //tells the adapter the size . if it's zero then it won't create anything
return tasks.size();
}
}
You need to rebuild the RecyclerView data in your TasksActivity when you return back from the TasksCreation; that is because the onCreate() callback of the TasksActivity is called only when you open your app; and that is why the deletion works only when you close your app and reopen it.
And onCreate() is not called when you back from the the TasksCreate, because when you transfer from TasksActivity to TasksCreation, the TasksActivity is not destroyed, but just stopped, and therefore when you come back to TasksActivity it will start, and resumed; so transfer your code on the onCreate() to onResume() in order to allow the list to be updated with the recent changes.
So change your TasksActivity to the below
public class TasksActivity extends AppCompatActivity {
DatabaseReference reference;
RecyclerView myTasks;
ArrayList<TaskItems> myTasksList;
TasksAdapter tasksAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasks);
}
#Override
protected void onResume() {
super.onResume();
myTasks = findViewById(R.id.my_tasks); // RecyclerView that I defined as part of the layout. This is the id of it
myTasks.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
myTasksList = new ArrayList<>();
Button openCreateTask = findViewById(R.id.openCreateTask);
tasksAdapter = new TasksAdapter(this,myTasksList);
myTasks.setAdapter(tasksAdapter);
new ItemTouchHelper(itemTouchHelper).attachToRecyclerView(myTasks);
openCreateTask.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent_task = new Intent(getApplicationContext(), TasksCreation.class);
startActivity(intent_task);
}
});
reference = FirebaseDatabase.getInstance().getReference().child("MotApp"); // Name of the App in the database .child("MotApp")
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) { // It gets the info from the database
Log.d("data Changed called", "onDataChange: is called");
Log.d("whatever", "onDataChange BEGIN Array of myTasksList size is "+myTasksList.size());
myTasksList.clear(); // Added later to avoid duplication
for(DataSnapshot elements: dataSnapshot.getChildren()){
TaskItems p = elements.getValue(TaskItems.class);
myTasksList.add(p);
}
tasksAdapter.notifyDataSetChanged(); // If this is put outside of onDataChange, it displays a blank list.
Log.d("whatever", "onDataChange END Array of myTasksList size is "+myTasksList.size());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "No data", Toast.LENGTH_SHORT).show();
}
});
}
ItemTouchHelper.SimpleCallback itemTouchHelper = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
Log.d("ARRAY SIZE", "onSwiped BEGIN Array of myTasksList size is "+myTasksList.size());
String key = myTasksList.get(position).getKey();
reference= FirebaseDatabase.getInstance().getReference().child("MotApp").child(key);
Toast.makeText(getApplicationContext(),"This is key "+key,Toast.LENGTH_LONG).show();
reference.removeValue();
myTasksList.remove(position);
tasksAdapter.notifyItemRemoved(position);
Log.d("ARRAY SIZE", "onSwiped END Array of myTasksList size is "+myTasksList.size());
}
};
}
I finally found how to solve this.
First I changed all addValueEventListener with addListenerforSingleValueEvent in TaskActivity and TaskCreation. With addValueEventListener I realized that it was making several loops everytime a new task was created which caused all kind of problems.
And I also changed the way the startIntent was set up in TaskCreation. I had set it up initially after the Listener block. But the asynchronous nature of onDataChange was making that everytime I loaded the intent the updated information wasn't set up with the new task that was just added.
By putting it inside the onDataChange it was solved. I have made several tests and now it's working flawlessly.
I wasted many days trying to solve this. But I learned a lot of lessons in the process. So I guess they weren't wasted days :) .
Thanks to all who chipped in to help me with this issue.
I have created a bottom sheet using the newly updated support library. Basically, whenever an item in my recyclerview is long-clicked, a bottom sheet is shown (containing a linearlayout which contains a listview). But whenever I tap the items in the listview nothing happens. I added toasts and it is never triggered when I tap on the sheet items. Any ideas? Thanks in advance!
Here is the listview initialization method:
public void initSheet() {
bottomSheet = findViewById(R.id.list_sheet);
ListAdapter adapter = new ListAdapter(this, R.layout.custom_sheet_row, getSheetInfo(), "Sheet");
list = (ListView) findViewById(R.id.list_sheet_list);
list.setAdapter(adapter);
behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setHideable(true);
}
Here is the recyclerview long click:
#Override
public void onLongClick(View view, int position) {
final String itemText = ((TextView) view.findViewById(R.id.textRow)).getText().toString();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListActivity.this, "sdjakfjs", Toast.LENGTH_SHORT).show();
switch (position) {
case 0:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, itemText);
startActivity(Intent.createChooser(sharingIntent, "Share the item"));
break;
case 1:
final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setPrimaryClip(ClipData.newPlainText("New", itemText));
break;
case 2:
editText(itemText, position);
break;
case 3:
listAdapter.deleteItem(position);
break;
}
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
try this.
implement custom clicklistener it will work pefectly.
// creating interface to handle clicks for Recycler view items
public interface ClickListener
{
void onClick(View view,int position);
void onLongClick(View view,int position);
}
public static class CustomRecyclerTouchListener implements RecyclerView.OnItemTouchListener
{
private GestureDetector gestureDetector;
private MainActivity.ClickListener clickListener;
public CustomRecyclerTouchListener(Context context,final RecyclerView recyclerView,final MainActivity.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 recyclerView, MotionEvent motionEvent) {
View child=recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());
if(child!=null && clickListener!=null && gestureDetector.onTouchEvent(motionEvent))
{
clickListener.onClick(child,recyclerView.getChildPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean b) {
}
}
}
Then implement like this.
recyclerView.addOnItemTouchListener(new CustomRecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
#Override
public void onClick(View view, int position) {
User user=users.get(position);
Toast.makeText(MainActivity.this,user.getName()+" is clicked",Toast.LENGTH_SHORT).show();
}
#Override
public void onLongClick(View view, int position) {
User user=users.get(position);
Toast.makeText(MainActivity.this,user.getName()+" is long clicked",Toast.LENGTH_SHORT).show();
}
}));
For more details refer here:- http://coderzpassion.com/android-working-with-recycler-view/
Ok, what I ended up doing was creating 4 textviews inside of the linear layout instead of using the listview. Then I just added an onclick listener for all of the textviews to do their respective commands
I'm new to android, started it about a month ago, and now I'm trying to make a "Shopping List" app for the sake of practice. In this app I have a ListView, where user can insert items via EditText above that ListView. When user longClick on item, ContextMenu with "Edit", "Delete" and "Mark" fields appears. I have already made "Delete" button work, but I still have problems with "Edit" function. To make this function work I created DialogFragment class, so when user presses the "Edit" button, this DialogFragment appears. This DF has EditText field, where we enter data we want to change. Here is DialogFragment class code:
public class AlertEdit extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder bd = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
bd.setView(inflater.inflate(R.layout.alert, null))
.setTitle("Edit")
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((MyActivity)getActivity()).doPositiveClick();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((MyActivity)getActivity()).doNegativeClick();
}
});
return bd.create();
}
as you can see, we have positive button here, which calls doPositiveClick method from MyActivity, which appears to be the main activity.
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((MyActivity)getActivity()).doPositiveClick();
}
So, here is the MyActivity class code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
lw = (ListView) findViewById(R.id.listView);
edtxt = (EditText) findViewById(R.id.editText);
alertEd = (EditText) findViewById(R.id.alertEdit);
goods = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, goods);
lw.setAdapter(adapter);
lw.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v,
int position, long id) {
}
});
registerForContextMenu(lw);
edtxt.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction()== KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
goods.add(0, edtxt.getText().toString());
adapter.notifyDataSetChanged();
edtxt.setText("");
return true;
}
}
return false;
}
});
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo info){
super.onCreateContextMenu(menu, v, info);
getMenuInflater().inflate(R.menu.actions, menu);
}
public boolean onContextItemSelected(MenuItem item) {
position = (int) info.id;
switch (item.getItemId()) {
case R.id.cnt_mnu_delete:
goods.remove(position);
adapter.notifyDataSetChanged();
return true;
case R.id.cnt_mnu_edit:
}
return super.onContextItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void doPositiveClick()
{
}
public void doNegativeClick()
{
}
public void showDialog()
{
DialogFragment frag = new AlertEdit();
frag.show(getFragmentManager(), "edit");
}
}
My problem is that I have no idea how to create that Edit function. I tryied to use AdapterContextMenuInfo, but it works only in onContextItemSelected method, because it requires and Item to work with. Hope you help me and sorry for the possible lack of information, ask me any additional questions please.
P.S. I'm trying to make this dialog for almost two weeks already and I'm really frustrated because of that.
I'm using this method - it's simple and you may adapt it to your needs:
First of all make an interface to handle your result, for example:
public interface OnDialogResultListener {
public void onDialogResult(String result);
}
Then use your dialog with additional view, like this:
public void showDialogAndGetResult(final int title, final String message, final String initialText, final OnDialogResultListener listener) {
// additional View - use appropriate View to your needs:
final EditText editText = new EditText(this);
editText.setText(initialText);
new AlertDialog.Builder(MainActivity.this)//
.setTitle(title)//
.setMessage(message)//
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (listener != null) {
listener.onDialogResult(editText.getText().toString());
}
}
})//
.setNegativeButton(android.R.string.cancel, null)//
.setView(editText)//
.show();
}
At last implement this interface in your activity:
public class YourActivity Extends Activity implements OnDialogResultListener{
...
#Override
public void onDialogResult(String result) {
//do what you need
}
...
}
Edit:
You may replace EditText by any View, including Layouts.
Still you may use the same scheme to return result from your DialogFragment descendant - just pass OnDialogResultListener in constructor or initializing method. I would say AlertDialog is more lightweight and DialogFragment allows more control and you may use both according to your needs.
In the code below, onItemLongClick works only when I comment out the onTouchListener. I need to be able to register swipes as well as long clicks. What am I doing wrong?
public class MainPageActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
...
getListView().setLongClickable(true);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "long click", Toast.LENGTH_SHORT).show();
return true;
}
});
getListView().setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeBottom() {
Toast.makeText(MainPageActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}
});
}
}
If you are overriding onTouch, onDown, or other methods like that, in OnTouchListener, SimpleGestureListener or the like, you need to make sure that your are returning false, in order for the event to propagate correctly.