Android ListView drag & drop: issue adapting TouchInterceptor - android

One of the requirements of the application I am currently working requires that the ListView should support drag and drop of child views. I am using the TouchInterceptor class from the Android Music app as reference, and while I have managed to adapt this to my app needs there is one issue which I am still struggling to resolve. Would really appreciate some comments from anyone who has used/adapted the TouchInterceptor class and has experience/knowledge of how the ListView works in conjunction with the adapter.
My ListView has circular edges at the top and bottom and also can have different color for each row depending on the type of data that row contains. This means that my adapter getView() method supplies the appropriate background resource for that view. Also, for the first and last rows, I supply the background resource that have the curved top and bottom edges.
Now, anyone familiar with the TouchInterceptor row expansion mechanism (doExpand() method) would know that when a row is dragged out, it's height is set to 1 and the row below that is expanded to give an illusion that space is being made for a new item. My problem is that when the first item (which has a curved edge) is moved below, the expansion logic causes its height to become 1 so that it becomes invisible and the next item becomes the first one (or appears to be the first one.) In this case, I would now like to supply the correct background resource for this item from the adapter. In terms of logic I was trying to do this is roughly in the getView() method of the adapter:
//do the row inflation logic and data setting
;
;
;
if ( position == 1 && mListView.getChildAt(0).getHeight()==1) {
//set background resource with curved top edge
}
However getHeight() never returns 1. It always seems to return 0. Is anyone able to point out if calling getChildAt() and getHeight from the getView() method of adapter is valid? Anyone familiar with the ListView and TouchInterceptor class could plese provide any guidance on when getView()is exactly called by the ListView in the context of above and what would be a good way to set the correct background resource?

Related

Empty gridlines in android gridview?

I want a gridview with empty grid lines as follows
Here is what i got till now, gridview with gridlines in the occupied cells only. I want gridlines in empty cells also. Is this possible?
I'm almost positive a GridView was never intended for this, so you have two options.
-Write your own GridView that supports a defaulted view.
OR
-If this is not a dynamic view that is changing all the time in real time:
In your adapter, set a minimum in your getCount method (return Math.max(actualSize, minimumCount)). And set the views with no data to your empty boxes.
Make sure the count is always some mod of 4 to ensure each row will be filled beyond that.
That's just what I'm coming up with on the top of my head, there's most likely a better way to do it, but hopefully I'm moving you in the right direction.
Try putting an empy View within the empty cells of the grid.
For example, use a TextView with no text, or simply a new View().

How to get all children (visible and invisible) from a ListView?

My problem is similar to ListView getChildAt returning null for visible children, but despite searching I cannot find a solution.
I have a ListView with a Scroll. The ListView has 10 items, 7 of which are visible and 3 are hidden by scroll. I also have an external method (out of adapter) that must get all of the children from this ListView (e.g. using getChildAt()).
I need all 10 of the items, but the last 3 are null objects. I've tried code like the following:
getListView().smoothScrollToPosition();
But this doesn't work.
I think that I don't need to post the rest of my code, as the description says everything?
As you have already seen you can't get all the child row views from a ListView simply because a ListView holds only the views for the visible rows(plus some recycled rows but you can't reach those). The correct way to do what you want is to store whatever data in the adapter's data and retrieve it from there.
But the ListView doesn't keep the current values from RadioGroup in
running time.
I've seen that you have some problems with this so I've adapted some old code to build a basic example, code that you can find here.
I don't think so you need to add scroll view for a listView. Scroll automatically works on ListView. Try your application without adding scroll view and I'm sure it'll work as you needed.
The reason those children are null it's because they really do not exist and they will never exist, if only 7 children are on the screen at one time, the system will only create 7 and re-use by passing the convertView back to the adapter getView() method.
If you want to grab information regarding your whole dataset you should search on the dataset itself, instead of the views on the screen. E.g. if it's an ArrayAdapter, loop the array; if it's a CursorAdapter, loop the cursor; etc.
The non-visible children of a listView don't actually exist. When they become visible, one of the redundant views is recycled or a new view is generated. So you can't actually access all the views. Why do you want to? Whatever changes you want to make should be made to the data that populates the views rather than the views themselves.
There are a few point that you need to take care of:
1. List view provides inbuilt scroll functionality, So don't use Scroll view. It will only mess up things.
2. List view doesn't contain ALL the children. When you scroll it, it creates only visible items on run time.
3. If you want to get all the children altogether, Better keep an ArrayList of the child objects that your list has. You can add or remove children to this ArrayList as per requirement.

Android - ListView items maintaining style when scrolling

I know that similar questions have already been asked but I do not understand what would be the correct approach of solving the issue, yet.
I would like to change the background color of a ListView row when the user clicks it. However due to Android reusing the row layouts when scrolling, the background color gets repeated for other rows. I am wondering what would be the correct approach of maintaining the original layout for all rows except the one changed programmaticaly and also maintain the changed layout information for that row for scrolling back. I am using a SimpleAdapter which is passed the rows layout's XML.
Regards
Your rows' capabilities within your ListView largely depend on the kind of Adapter you are using. In any Adapter where you manually construct or inflate the View per item, you can change the layout properties per item, as long as you do so within the Adapter. Simply add your background color code to when the item's View is built, and it will work like a charm. If you are not able to do so with the current Adapter, consider extending the current one or using a different adapter.
Note: I haven't placed code directly within this answer because where you add it depends upon your own implementation. For instance, I would add .setBackgroundDrawable() to bindView() in an extended CursorAdapter.
Hope this helps,
FuzzicalLogic

Rows and Columns layout in Android Widget

Things are simple, I want to make a widget with 4 lines and 4 rows in it, on each cell there would be a click-able image and an action set by the user via the Settings page.
The layout is like this:
What layout element is recommended to be used for this scenario ? Should I use a GridView, TableLayout, more Linearlayouts ? Keep in mind that the spacing between items must be the same. I want to make it as light as possible. So, what layout ?
If I decide to use a GridView do you have any simple tutorial about
this? I can't manage to find a way of accessing the GridView from
AppWidgetProvider and set it's Adapter. Thank you.
LE: It seems that GridView is supported starting from Android 3.0.. please correct me if I'm wrong. In this case the only remaining thing to do is add 16 images and for each image add a onClickListener ? Brrr...
If you use a GridView then half of your work is done for you - The only layout and formatting elements you need to consider are on a Global (GridView) and Item level.
Using a GridView will also give your Scrolling functionality and the ability to change your row/columns count based on your device (4x4 on tablet, perhaps 2x8 on a phone).
Creating an extension of BaseAdapter to attach the Grid's children will also give you the flexibility to check items, multi select and will allow you to quickly modify the implementation in future by adding and removing items at will.
If this is simply a 4x4 grid which will always ALWAYS remain the same independent of device and each "Item" will always be the same, Use a RelativeLayout as it will be the most lightweight and efficient ViewGroup.

How to use custom color for each textview in listview that extends SimpleAdapter in Android?

I have a listview with custom rows and that extends SimpleAdapter.
Each row consist of two linear layouts : 1st having two textviews of which one is hidden in horizontal orientation, second having two textviews in horizontal orientation.
Now depending on the value in hidden textview , I want to setcolor for the remaining items for the row.
To put it as simple:
each listview item has some custom colors the value of which comes from the hidden field.
I have done this by overriding getview() for the simpleadapter and returning view for each, but this makes list very slow to render (and that I think is obvious as so much of work for each view before showing it).
Can I do this in some more efficient way ? like making views and then add up to list instead of using xml layout maybe one solution OR any other ? Any help ? Thanks.
If you use convertView in your adapter, I would not expect you to have any particular speed issues. Creating and garbage collecting rows is expensive -- setting some colors on a set of TextViews is not. So, make sure you are using the convertView parameter to recycle your rows.
Here is a free excerpt from one of my books that covers row recycling.

Categories

Resources