Drag-drop not working in nhaarman's ListviewAnimation library - android

I am using nhaarman's ListviewAnimation library https://github.com/nhaarman/ListViewAnimations which works great.
But I am facing following issues:
The main problem I am facing is, I am not able to debug my code. I have directly copy/pasted the four required libraries into libs folder. Placing a debug point inside any of the listview methods like onItemLongClick() does not work.
The second problem is, drag-drop listView is not working in my code. Whenever I try to drag any list item, on dropping the list item, the item takes the same position from which it was dragged.
Here's the code I have used:
listview.enableDragAndDrop();
listview.setDraggableManager(new TouchViewDraggableManager(
R.id.list_row_draganddrop_textview));
listview.setOnItemMovedListener(this);
listview.setOnItemLongClickListener(this);
#Override
public void onItemMoved(final int originalPosition, final int newPosition) {
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(getApplicationContext(), "Moved"
+ swingBottomInAnimationAdapter.getItem(newPosition)
+ newPosition, Toast.LENGTH_SHORT);
mToast.show();
}
#Override
public boolean onItemLongClick(final AdapterView<?> parent,
final View view, final int position, final long id) {
if (listview != null) {
listview.startDragging(position - listview.getHeaderViewsCount());
}
return true;
}

Whenever I try to drag any list item, on dropping the list item, the item takes the same position from which it was dragged.
Of course. Handling the change in position is your responsibility, and you should take care of it inside the onItemMoved callback:
#Override
public void onItemMoved(final int originalPosition, final int newPosition) {
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(getApplicationContext(), "Moved"
+ swingBottomInAnimationAdapter.getItem(newPosition)
+ newPosition, Toast.LENGTH_SHORT);
mToast.show();
// Adapt the following to your implementation
if (originalPosition != newPosition) {
YourObject item = (YourObject) yourAdapter.getItem(originalPosition);
yourAdapter.moveItem(item, newPosition);
}
}
The method mentioned above would look something like:
public void moveItem(YourObject item, int newIndex) {
if (mEntries != null) {
mEntries.remove(item);
mEntries.add(newIndex, item);
notifyDataSetChanged();
}
}
If you go through the source code, you'll see that what you are dragging around is a Bitmap. The list item is sitting at its original position.

For others having the same problem - Niek Haarman has answered this question on GitHub here.
Don't see GitHub going down soon, but as it is good tone to paste the answer too, here it is:
#Override
public long getItemId(int position) {
return position;
}
#Override
public boolean hasStableIds() {
return true;
}
position is not a stable id here. You need a stable id for the item
which does not depend on the position.

use
import com.nhaarman.listviewanimations.ArrayAdapter;
instead of
import android.widget.ArrayAdapter;
that is the reason it doesn't calling onItemMoved

Related

RecyclerView item removal behaves aberrantly

I am using this tutorial to build an itemTouchListener for my RecyclerView. The recyclerview is filled with more items than the screen fits (more than 10), so recycling gets into action. The itemtouchHelper handles both up-down and left-right movement. After 2 days of struggle (had setStableIds to true which caused flickering of the viewholder views when the were moved up-down), I finally got a better behaviour. My code of the crucial features:
#Override
public int getItemCount() {
return questionlist.size();
}
#Override
public void onViewMoved(int oldPosition, int newPosition) {
targetqueobj = questionlist.get(oldPosition);
this.fromPosition = oldPosition;
this.toPosition = newPosition;
questionlist.remove(targetqueobj);
questionlist.add(newPosition, targetqueobj);
// targetqueobj.setinturn(toPosition+1);
}
#Override
public void onViewSwiped(final RecyclerView.ViewHolder thisviewholder,final int position, int direction) {
targetqueobj = questionlist.get(position);
if (direction == ItemTouchHelper.LEFT){
// saveqset();
Intent intent = new Intent(context, QuestionEditActivity.class);
intent.putExtra("com.logictop.mqapp.QuestionObjParcelable",targetqueobj);
context.startActivity(intent);
}
if (direction == ItemTouchHelper.RIGHT) {
// DIALOG
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.remove_question);
builder.setNegativeButton(R.string.No, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// thisviewholder.itemView.setAlpha(1);
notifyDataSetChanged();
}
});
builder.setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
questionlist.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount()-position);
}
});
Dialog dialog = builder.create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
}
if (direction == ItemTouchHelper.RIGHT) {
// DIALOG
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.remove_question);
builder.setNegativeButton(R.string.No, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
thisviewholder.itemView.setAlpha(1);
notifyDataSetChanged();
}
});
builder.setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
questionlist.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount()-position);
}
});
Dialog dialog = builder.create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
}
The problem is this. Though the recyclerview runs smoothly with the items very nicely changing positions, when an item is swiped away, sometimes another item looks also removed when the recyclerview is scrolled down (so it seems that the other item that gets removed fills the same "screen" position in the recyclerview after it is scrolled). It doesn't happen every time and it mostly happens when a view in some specific positions are swiped away. That "gap" can be mended if I move a neighbor view up or down.
I have tried every solution I found in here (notifyDataChanged, notifyItemRangeChanged (with every parameter compination). But nothing could get me a stable behaviour. AFter a lot of stackoverflow searching I decided to follow the advice of holder.setIsRecyclable(false)
even though I didn't want to do that (as it eliminates the point of having a recyclerview after all). But in that case, other problems appear. You'll see below that when most of the views are swiped away, they lower ones won't leave the screen, even though they apparently have "left the adapter" (cannot swipe them away). And in the end, the last views stay stuck.
I have tested both ways on a completely new project with nothing external coming into the way. I have tried putting notifyDataChanged() in an overriden function of clearView. Nothing seems to provide a rock solid stable recyclerview that won't get at some point a gap in itself.
The question now: is there a way to make a recyclerview with working recycling behave like it is supposed to behave or should I accept that situation?
Thank you very much for your attention!
UPDATE 1----------------------------
As I was told there could be an issue with this thisviewholder.itemView.setAlpha(1); I commented it out along with the corresponding overriden onChildDraw (the whole of it) that is used to fade the view out when it is swiped out. So now nothing gets invisible. Still the problem persists.
UPDATE 2----------------------------
I have enforced stableIds (had already done that once and it didn't work)
#Override
public long getItemId(int position){
return questionlist.get(position).getquestionid();
}
and the adapter contructor
public QSetEditAdapter(ArrayList<QuestionObj_prcl> questionlist, Context context) {
this.context = context;
this.questionlist = questionlist;
setHasStableIds(true);
}
Still the problem persists.
UPDATE 3----------------------------
I ended up using a different tutorial and now everything works as expected. Thanks a lot!
Change your adapter this way:
Set hasStableIds() to true.
Implement getItemId(int) and return unique ids for your items.
I don't know why this is happening
Exactly in the method below the list, it keeps the value you gave it for the first time and does not update this value !!
public void onViewSwiped (int position)
But what is the solution???
The solution is to use the -> touchHelper.startSwipe(holder) method
For example :
holder.itemView.setOnTouchListener (new View.OnTouchListener () {
VerOverride
public boolean onTouch (View view, MotionEvent event) {
if (event.getAction () == MotionEvent.ACTION_DOWN) {
Update = false;
touchHelper.startSwipe (holder);
}
return true;
}
});
The job of this method is to reload the deleted animations and views
Good luck

Changing infoWindow view depending on click listener

I am trying to implement a Google Map marker with infoWindow that if someone clicks on this infoWindow, it plays a song and if clicks again, it stops. To visualize this, I write a custom infoWindow layout. There is, in infoWindow, you can see user and track info with a button. This button shows play icon if the track does not begin to play yet, and if it pressed (press on infoWindow, not the button), I hope it changes its icon from "play" to "stop". However, I cannot change my custom infoWindow's view depending on infoWindowClickListener activity. I tried to change infoWindowAdapter especially but I do not want to change view of all other infoWindows and also I want to see the change immediately. In this way, the infoWindow refreshes its view after I click on the marker again. In other words, it does not change the view simultaneously with my click action.
Here you can see what I am talking about. Stop status on left, play status on right:
Here is my futile effort for adapter:
public class OrangeInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
Context context;
ImageButton playButton;
boolean onPlay;
public OrangeInfoWindowAdapter(Context context, boolean onPlay) {
this.context = context;
this.onPlay = onPlay;
}
#Override
public View getInfoWindow(Marker arg0) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.orange_infowindow, null);
v.setMinimumWidth(280);
v.setMinimumHeight(120);
TextView tvUsername = (TextView) v.findViewById(R.id.tv_username);
TextView tvTrack = (TextView) v.findViewById(R.id.tv_track);
int index = arg0.getTitle().indexOf("*");
try {
tvUsername.setText(arg0.getTitle().substring(0, index - 1) + "\n" + arg0.getTitle().substring(index + 2));
} catch (StringIndexOutOfBoundsException e) {
}
tvUsername.setTextSize(10);
tvUsername.setTextColor(Color.rgb(70, 70, 70));
index = arg0.getSnippet().indexOf("*");
try {
tvTrack.setText(arg0.getSnippet().substring(0, index - 1) + "\n" + arg0.getSnippet().substring(index + 2));
} catch (StringIndexOutOfBoundsException e) {
}
tvTrack.setTextSize(10);
tvTrack.setTextColor(Color.rgb(230, 92, 1));
playButton = (ImageButton) v.findViewById(R.id.playButton);
if (onPlay)
onPlay();
return v;
}
public void onPlay() {
playButton.setBackgroundResource(R.drawable.info_stop_button);
}
public void onStop() {
playButton.setBackgroundResource(R.drawable.info_play_button);
}
#Override
public View getInfoContents(Marker arg0) {
return null;
}
}
And this is my onInfoWindowClick():
#Override
public void onInfoWindowClick(Marker marker) {
if (!infoWindowPlayerActive) {
int index = findMarkerIndex(marker);
OrangeInfoWindowAdapter infoWindowAdapter2 = new OrangeInfoWindowAdapter(getActivity().getApplicationContext(), true);
googleMap.setInfoWindowAdapter(infoWindowAdapter2);
new InfoWindowPlayerTask(mainActivity).execute(activities.get(index).getTrackId());
infoWindowPlayerActive = true;
}
else {
// same thing...
infoWindowPlayerActive = false;
}
}
If you want more information to understand the problem clearly, please ask me.
The GoogleMap API v.2 does not support any interaction on InfoWindow, besides opening and closing it.
However, there is an amazing hack implemented in this answer, on how you should create an interactive View inside your InfoWindow. Keep in mind that the same technique applies for Fragments too.
From the official documentation:
Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (e.g., after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.
I found a sloppy but working solution:
#Override
public void onInfoWindowClick(Marker marker) {
if (!infoWindowPlayerActive) {
googleMap.setInfoWindowAdapter(infoWindowAdapterOnPlay);
marker.showInfoWindow();
newClickedInfoWindowIndex = findMarkerIndex(marker);
if (lastClickedInfoWindowIndex != newClickedInfoWindowIndex) {
new InfoWindowPlayerTask(mainActivity).execute(activities.get(newClickedInfoWindowIndex).getTrackId());
}
else {
mainActivity.getPlayerManager().clickPlayPause();
}
lastClickedInfoWindowIndex = newClickedInfoWindowIndex;
infoWindowPlayerActive = true;
}
else {
googleMap.setInfoWindowAdapter(infoWindowAdapter);
marker.showInfoWindow();
mainActivity.getPlayerManager().clickPlayPause();
infoWindowPlayerActive = false;
}
}
public int findMarkerIndex(Marker marker) {
for (int i = 0; i < markers.size(); i++) {
if (marker.getPosition().equals(markers.get(i).getPosition())) {
return i;
}
}
return -1;
}
Of course, assume that infoWindowPlayerActive, lastClickedInfoWindowIndex, newClickedInfoWindowIndex are defined in the class above.

Android Dev - Spinner

I am stuck in a situation. I'm trying to use spinners in order for users to select locations. I am populating the spinners via sqlite. The idea is sometimes there is a country, province, city and sub-areas, however any field can be blank (if not populated in database).
I would like the spinners to be "hidden" if they do not have the values stored in the database. However, only the "next step down" (ex: country to province) is hidden and not all the "other steps down" (city and sub-area)
I hope I am being clear enough, if not I can clarify.
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if (parent.getId() == R.id.spinner_query_country)
{
country.selected = (class_location)country.spinner.getSelectedItem();
get_province();
}
else if (parent.getId() == R.id.spinner_query_province)
{
province.selected = (class_location)province.spinner.getSelectedItem();
get_city();
}
else if (parent.getId() == R.id.spinner_query_city)
{
city.selected = (class_location)city.spinner.getSelectedItem();
get_sub_area();
}
else if (parent.getId() == R.id.spinner_query_sub_area)
{
sub_area.selected = (class_location)sub_area.spinner.getSelectedItem();
}
}
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
void get_country()
{
make_spinner(country, db.getAll("SELECT * FROM country"));
}
void get_province()
{
make_spinner(province, db.getAll("SELECT * FROM province WHERE country_key=" + country.selected._key));
}
void get_city()
{
make_spinner(city, db.getAll("SELECT * FROM city WHERE province_key=" + province.selected._key));
}
void get_sub_area()
{
make_spinner(sub_area, db.getAll("SELECT * FROM sub_area WHERE city_key=" + city.selected._key));
}
void make_spinner(_structure structure, List<class_location> location_list)
{
if (location_list.size() > 0)
{
class_location location_array[] = location_list.toArray(new class_location[location_list.size()]);
ArrayAdapter<?> adapter = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, location_array);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
structure.spinner.setAdapter(adapter);
structure.spinner.setVisibility(View.VISIBLE);
structure.textview.setVisibility(View.VISIBLE);
}
else
{
structure.spinner.setAdapter(null);
structure.spinner.setVisibility(View.GONE);
structure.textview.setVisibility(View.GONE);
}
}
You are hidding only one spinner, not all of them simply because you are setting the visibility at make_spinner() and you only check one step down on your onItemSelected(if else strict). To make it clear, your code sets the visibility or invisibility of the components on a method that is never called for the "others steps down".
P.S.: Your code is kinda messy and do not follow the conventions or guidelines of Android. This is bad. Try to checkout some guidelines, help your collaborators :)

Adding items to ListView, maintaining scroll position and NOT seeing a scroll jump

I'm building an interface similar to the Google Hangouts chat interface. New messages are added to the bottom of the list. Scrolling up to the top of the list will trigger a load of previous message history. When the history comes in from the network, those messages are added to the top of the list and should not trigger any kind of scroll from the position the user had stopped when the load was triggered. In other words, a "loading indicator" is shown at the top of the list:
Which is then replaced in-situ with any loaded history.
I have all of this working... except one thing that I've had to resort to reflection to accomplish. There are plenty of questions and answers involving merely saving and restoring a scroll position when adding items to the adapter attached to a ListView. My problem is that when I do something like the following (simplified but should be self-explanatory):
public void addNewItems(List<Item> items) {
final int positionToSave = listView.getFirstVisiblePosition();
adapter.addAll(items);
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
}
Then what the user will see is a quick flash to the top of the ListView, then a quick flash back to the right location. The problem is fairly obvious and discovered by many people: setSelection() is unhappy until after notifyDataSetChanged() and a redraw of ListView. So we have to post() to the view to give it a chance to draw. But that looks terrible.
I've "fixed" it by using reflection. I hate it. At its core, what I want to accomplish is reset the first position of the ListView without going through the rigamarole of the draw cycle until after I've set the position. To do that, there's a helpful field of ListView: mFirstPosition. By gawd, that's exactly what I need to adjust! Unfortunately, it's package-private. Also unfortunately, there doesn't appear to be any way to set it programmatically or influence it in any way that doesn't involve an invalidate cycle... yielding the ugly behavior.
So, reflection with a fallback on failure:
try {
Field field = AdapterView.class.getDeclaredField("mFirstPosition");
field.setAccessible(true);
field.setInt(listView, positionToSave);
}
catch (Exception e) { // CATCH ALL THE EXCEPTIONS </meme>
e.printStackTrace();
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
}
}
Does it work? Yes. Is it hideous? Yes. Will it work in the future? Who knows? Is there a better way? That's my question.
How do I accomplish this without reflection?
An answer might be "write your own ListView that can handle this." I'll merely ask whether you've seen the code for ListView.
EDIT: Working solution with no reflection based on Luksprog's comment/answer.
Luksprog recommended an OnPreDrawListener(). Fascinating! I've messed with ViewTreeObservers before, but never one of these. After some messing around, the following type of thing appears to work quite perfectly.
public void addNewItems(List<Item> items) {
final int positionToSave = listView.getFirstVisiblePosition();
adapter.addAll(items);
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
listView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if(listView.getFirstVisiblePosition() == positionToSave) {
listView.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
else {
return false;
}
}
});
}
Very cool.
As I said in my comment, a OnPreDrawlistener could be another option to solve the problem. The idea of using the listener is to skip showing the ListView between the two states(after adding the data and after setting the selection to the right position). In the OnPreDrawListener(set with listViewReference.getViewTreeObserver().addOnPreDrawListener(listener);) you'll check the current visible position of the ListView and test it against the position which the ListView should show. If those don't match then make the listener's method return false to skip the frame and set the selection on the ListView to the right position. Setting the proper selection will trigger the draw listener again, this time the positions will match, in which case you'd unregister the OnPreDrawlistener and return true.
I was breaking up my head until I found a solution similar to this.
Before adding a set of items you have to save top distance of the firstVisible item and after adding the items do setSelectionFromTop().
Here is the code:
// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// for (Item item : items){
mListAdapter.add(item);
}
// restore index and top position
mList.setSelectionFromTop(index, top);
It works without any jump for me with a list of about 500 items :)
I took this code from this SO post: Retaining position in ListView after calling notifyDataSetChanged
The code suggested by the question author works, but it's dangerous.
For instance, this condition:
listView.getFirstVisiblePosition() == positionToSave
may always be true if no items were changed.
I had some problems with this aproach in a situation where any number of elements were added both above and below the current element. So I came up with a sligtly improved version:
/* This listener will block any listView redraws utils unlock() is called */
private class ListViewPredrawListener implements OnPreDrawListener {
private View view;
private boolean locked;
private ListViewPredrawListener(View view) {
this.view = view;
}
public void lock() {
if (!locked) {
locked = true;
view.getViewTreeObserver().addOnPreDrawListener(this);
}
}
public void unlock() {
if (locked) {
locked = false;
view.getViewTreeObserver().removeOnPreDrawListener(this);
}
}
#Override
public boolean onPreDraw() {
return false;
}
}
/* Method inside our BaseAdapter */
private updateList(List<Item> newItems) {
int pos = listView.getFirstVisiblePosition();
View cell = listView.getChildAt(pos);
String savedId = adapter.getItemId(pos); // item the user is currently looking at
savedPositionOffset = cell == null ? 0 : cell.getTop(); // current item top offset
// Now we block listView drawing until after setSelectionFromTop() is called
final ListViewPredrawListener predrawListener = new ListViewPredrawListener(listView);
predrawListener.lock();
// We have no idea what changed between items and newItems, the only assumption
// that we make is that item with savedId is still in the newItems list
items = newItems;
notifyDataSetChanged();
// or for ArrayAdapter:
//clear();
//addAll(newItems);
listView.post(new Runnable() {
#Override
public void run() {
// Now we can finally unlock listView drawing
// Note that this code will always be executed
predrawListener.unlock();
int newPosition = ...; // Calculate new position based on the savedId
listView.setSelectionFromTop(newPosition, savedPositionOffset);
}
});
}

Android Gallery visible ImageView is null?

I want to draw a check mark for the image view I click on and uncheck the imageview I clicked on before using the following code snip. I store last checked position in mDeviceAdapter. When I try to uncheck old position, the image view always gives null even for the partial visible image view. I am really confused because I thought only invisible one is recycled... Newbie in Android and any comment is appreciated.
public void CheckableImageView#setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
invalidate();
}
}
mDeviceGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CheckableImageView viewToCheck = (CheckableImageView) view;
if (!viewToCheck.isChecked()) {
int oldCheckedPosition = mDeviceAdapter
.getCheckedPosition();
mDeviceAdapter.setCheckedPosition(position);
View checkedView = mDeviceGallery
.getChildAt(oldCheckedPosition);
Log.d(TAG, "old position="+oldCheckedPosition + "old view="+checkedView);
if (checkedView != null) {
((CheckableImageView) checkedView)
.setChecked(false);
Log.d(TAG, "uncheck position="
+ oldCheckedPosition);
}
viewToCheck.setChecked(true);
That's not the right approach.
You need to add to your data type a boolean field (i.e mIsChecked).
On the onItemClick method set the value of that variable to true and keep its INDEX as a member of the adapter. When another item is clicked set the value of that item to true and set the value of the saved one to false (change the value of the datatype in you ArrayList in the INDEX you stored in the previous click).
Now, in the getView() method, you must have if/else statement. Something like:
if (item.isChecked())
{
checkedView.setChecked(true);
}
else
{
checkedView.setChecked(false);
}
Example to the onClick method: (just a general direction)
if (item.isChecked())
{
checkedView.setChecked(false);
yourList.get(position).setChecked(true);
yourList.get(mLastCheckedIndex).setChecked(false);
mLastCheckedIndex = position;
notifyDataSetChanged();
}
else
{
//same but opposite.
}
Hope this helps!

Categories

Resources