Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp with Android UI ListView problems
    text
    copied!<p>To understand this question, <a href="http://developer.android.com/reference/android/widget/AbsListView.html#onInterceptTouchEvent%28android.view.MotionEvent%29" rel="noreferrer">first read how this method works.</a></p> <p>I am trying to implements a drag and drop <a href="http://developer.android.com/reference/android/widget/ListView.html" rel="noreferrer">ListView</a>, it's going alright but have run into a road block. So I don't have to handled everything, I am intercepting(but returning false) <a href="http://developer.android.com/reference/android/view/MotionEvent.html" rel="noreferrer">MotionEvents</a> sent to the ListView, letting it handle scrolling and stuff. When I want to start dragging a item, I then return true and handled all the dragging stuff. Everything is working fine except for one thing. The drag(drag and drop) is started when it is determined that a long press as a occurred(in onInterceptTouchEvent). I get the <a href="http://developer.android.com/reference/android/graphics/Bitmap.html" rel="noreferrer">Bitmap</a> for the image that I drag around like so. itemPositition being the index of the item that was selected.</p> <p>(omitting irrelevant parts)</p> <pre><code>... View dragItem = mListView.getChildAt(itemPosition); dragItem.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(dragItem.getDrawingCache()); mDragImage = new ImageView(mContext); mDragImage.setImageBitmap(bitmap); ... </code></pre> <p>The problem is, mDragImage is a solid black like this. <img src="https://i.stack.imgur.com/RbqY2.jpg" alt="A solid back bitmap"></p> <p>But, if I don't let ListView handle anything. As in, I start the drag on <a href="http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN" rel="noreferrer">ACTION_DOWN</a> and stop on <a href="http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP" rel="noreferrer">ACTION_UP</a>, mDragImage looks has expected(but I obviously lose scrolling abilities).</p> <p><img src="https://i.stack.imgur.com/820Cc.jpg" alt="A good bitmap"></p> <p>Since the drag is started with a long press, the ListView is given the opportunity to do things before the long press occurs. This is my guess as to why this is happening. When a item is pressed, it is highlighted by the ListView. Somewhere in doing so, it is messing with the bitmap. So when I go to get it, it's in a weird state(all black). </p> <p>I see two options for fixing this, neither of which I know how to do.</p> <ol> <li><p>Create a image from scratch.</p></li> <li><p>Handle the highlighting myself(if that is the problem).</p></li> </ol> <p>Option two seems a better one to me, except that I looked at the documentation and the source code and could not find out how to do so. Here are some things I have done/tried.</p> <ul> <li><p>I set <a href="http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener%29" rel="noreferrer">setOnItemClickListener(...)</a> and <a href="http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemSelectedListener%28android.widget.AdapterView.OnItemSelectedListener%29" rel="noreferrer">setOnItemSelectedListener</a>(...) with a empty method(highlighting still happens). (Before anyone suggests it, calling <a href="http://developer.android.com/reference/android/widget/AdapterView.html#setOnClickListener%28android.view.View.OnClickListener%29" rel="noreferrer">setOnClickListener</a> results in a runtime error.)</p></li> <li><p>I also looked into trying to get the ListView to make a new item (for option 2), but could not find a way.</p></li> <li><p>Spent 45ish minutes looking through the source code and documentation trying to pinpoint where the highlighting was happening(I never found it).</p></li> </ul> <p>Any help fixing this would be appreciated.</p> <p>(EDIT1 START)</p> <p>So I don't actually know if onLongClickListener is working, I made an error before thinking it was. I am trying to set it up right now, will update when I find out if it does.</p> <p>(EDIT1 END)</p> <p>Last minute edit before post. I tried using <a href="http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html" rel="noreferrer">onLongClickListener</a> just now, and the image is good. I would still like to know if there is another way. How I have to use onLongClickListener to get things working is ugly, but it works. I also spent so much time trying to figure this out, it would be nice to find out the answer. I still want to be able to change/handle the highlight color, the default orangeish color is not pretty. Oh and sorry about the length of the post. I could not think of way of making it shorter, while supplying all the information I thought was needed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload