Getting Position of Image on GalleryView while swiping? - android

How can I get the position of current image displaying in Gallery view while it is swiping?

So I don't think that is possible because of the way adapter views work. Basically most if not all calls to the adapter view's adapter have to be on the UI thread to work properly. And the animation is definitely on the UI thread. So since they both can't be happening at the same time on the same thread, you can't actually (accurately) read the position until the swiping animation is over.
If you ask the adapter getSelectedItemPosition() on any other thread it might give you the current position but might also be stale for some time since I am guessing the UI thread is going to update the value without checking for an exclusive lock.

We can get the position when we are swiping the image in gallery view,you overwrite Gallery View's setOnItemSelectedListener.
We have two methods from that listener like
avatar_gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view,
int position,
long id){
Log.v("Selected", ""+position);
}
#Override
public void onNothingSelected(AdapterView<?> parent){
}
}

Related

The GridView does not update immediately after calling method notifyDataSetChanged()

I am trying to build Tic-Tac-Toe game( just for human play with android device, I have implemented a smart algorithm called minimax for my bot) by using android GridView. I have built everything successfully( UI, minimax algorithm for my bot) but there is still a hard to understand problem, that is when I click on Gridview item, I want GridView must update immediately, then my bot start it calculating.So, I have implement my code to set on GridView item click as followings:
gridviewBoard.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
//if it is a legal moves of player
if (isLegal(false, i)){
//put X into board
put(false, i);
//I want to update gridview immediately by using notifyDataSetChanged()
chessAdapter.notifyDataSetChanged();
//then start my bot calculating
// findBestMove is a method to find the best move for my bot
// by using minimax algorithm
int nextMove = findBestMove();
}
}
}
Howerver, the gridviewBoard did not update immediately, the gridviewBoard only updates when the findBestMove function is computed while it takes a long time to calculate. Summary, I want to update gridviewBoard immediately after item click and before my bot calculating. Can someone help me, and sorry for my bad English grammar.

Change image in gallery

I have a gallery where I'm displaying a number of ID cards as images.
When the user clicks on an image, I want to change the image to another one which shows the 'back' of the card.
I have bound an arraylist of objects to the gallery, each object contains the front and back images.
I have two image views, one for the front and one for the back image. The gallery will initially show all front images ( the 'back' image view visibility is set to GONE). My idea was to change the visibility of the relevant image view when the user clicks.
That's about as far as I've got, buy I'm not sure how to implement the click functionality to change the views.
Any assistance would be greatly appreciated.
A Gallery is a specialization of AdapterView, which also contains ListView, Spinner, GridView... All of these have in common to display n times the same 'kind' of view with different data.
These particular views use an Adapter to handle the view creation and population process. The getView method is called on the adapter, providing this class with the data for creating the view for a particular row (cell, gallery item ...).
The getView method is called when the AdapterView deems it necessary, and the developer has no control on which view gets created or when it does.
Therefore, it is practically impossible to modify a child of the AdapterView directly. It would result it the modification being arbitrarily overwritten, or even transferred to another row of the view.
Instead, to act on the child views, it is necessary to act on the data that are used to populate the view. (Note that this enforce the MVC model, as the view remains a reflection of the model, rather than being manipulated by the controller.)
In this particular example, the view is to display a different image based on the checked status of the data. To achieve that, add a checked member to the model :
public class MyModel {
// ... Other members
public boolean checked = false;
}
Use that value in getView :
#Override
public View getView(int position, View convertView, View parent) {
MyModel current = getItem(position);
// ... Inflate and populate
if (current.checked) {
// Specific checked behavior
} else {
// Not checked behavior
}
}
The checked value is to be modified in the controller, probably a OnItemClickListener:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Here, there can be several ways to access the clicked item.
// I assume myAdapter is the adapter used for the gallery
MyModel current = myAdapter.get(position);
current.checked = !current.checked;
myAdapter.notifyDataSetChanged();
}

Android gridView/listView get requested elements

I know the methods getFirstVisiblePosition() and getLastVisiblePosition() but these are only for the visible grid, if you check for the visibility for the element at position in the getView(int position, View convertView, ViewGroup parent) you will see that it lies outside the range of visible elements(which makes perfect sense). What can I do to check if an element at a position is visible or has been requested by the gridView/listView. Something which returns true in the getView(...) method itself.
Why I am trying to do it:
I have a bunch of images that I am showing in a gridView. The images have to be fetched via the network(I am caching them once they have been downloaded), so I have created an asyncTask to download the image and then show it in the corresponding grid. Since there might be a lot of images and the user might quickly scroll down, I want to insert some check in the onPreExecute() method which can tell me if it visible, otherwise I cancel the task for now.
if (!requested()) {
this.cancel(true);
}
I am looking for appropriate code for requested().
Really sorry for such a long question, finding it really difficult to explain. Any solutions or modifications to the question are most welcome.
I'm not sure if I understand your question. Do you want to know if, given a certain position, that view has ever been shown?
If that's what you want, you should have a boolean array in your list/grid adapter. In the getView function you set that position to true. Then you only have to implement a function to check if a certain position is true.
public class MyAdapter extends BaseAdapter {
private boolean[] hasBeenSeen;
public View getView (int position, View convertView, ViewGroup parent) {
hasBeenSeen[position] = true;
...
}
public boolean checkPosition(int position){
return hasBeenSeen[position];
}
}
The boolean array has to be initialized when you set the list/grid content.
You can try using View.getLocationOnScreen(); If the x and y positions are within your screen bounds then it should be on your screen. I'm not sure what this will return if it isn't on your screen, though.

"Synchronizing" two spinners

I have two spinners that I want to be "tied" to one another in a mutually-exclusive sense: If you select an item in one, that item's text turns red and appears at the top, while the other one goes back to displaying the initial ("title") selection (if another item was previously selected), and its text turns white.
This is all done via the onItemSelected listeners:
sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
issueSpin.setSelection(0);
((TextView) issueSpin.getChildAt(0)).setTextColor(Color.parseColor("#FFFFFF"));
((TextView) arg1).setTextColor(Color.parseColor("#E3170D"));
....
and vice versa for the "issue spinner". My problem is that, if I'm going from one spinner to the other and I select the top item, the onItemSelectedListener doesn't register because the item being selected is already selected.
I've been told that this is not possible. Or, rather, I've been told that it is impossible for an onItemSelected listener to fire on an item that is already selected. While I realize this is technically true, this problem seems relatively simple and I'm sure there must be a workaround to produce the desired effect.
I have a few questions regarding some that I'm pondering:
Is there any way to set all items in a spinner as unselected, while still displaying one of them?
Could I utilize a different type of event (i.e. 'setOnTouchListener', 'setOnClickListener', etc), presumably on the top item, in conjunction with the onItemSelectedListener?
Should I utilize a different type of event by itself, perhaps on the Views inflated in the spinner themselves, without the onItemSelectedListener?
Can you help me find a better strategy than those alluded to in the bulletpoints above?
Instead of using a Spinner, use a Button that visually resembles a non-active Spinner. When the button is clicked, show a custom Dialog containing a ListView. You can use the setSelection method and/or the position in the Adapter's getView to show which item is currently selected. The ListView's OnItemClickListener will get notified that something got clicked regardless of whether the item was selected or not.
When you find which item was clicked, hide the Dialog and notify the button-containing Activity of this, so that it can update both Buttons if needed.
I have implemented something really similar to this using DialogFragments and a DialogFragmentParent and it works fine.
Have you consider something like below,
sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
updateView("sectionSpin");
}
}
And for other one
issueSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
updateView("issueSpin");
}
}
and now
private void updateView(String spinner){
if (spinner.equals(sectionSpin)){
//update spinner per requirement
}
else{
//update spinner per requirement
}
}
You can also consider some additional variable or parameters to keep track if the item selected for first time or not.
So, you could make the first item in the spinners not valid selections like "[Choose one]" or "[Select Title]" forcing the listener to fire every time.
This would have the added benefit that you will know user did select something before the next step.
Of course, this would depend on desired application flow.

View that extends Gallery + Adapter: How to inform view of changes?

I have a view that extends Gallery (In order to create a cover flow).
Now I need the title of the currently selected (speak: The one that's shown in the front) to be displayed at the top.
So far, that's no problem since I can just use:
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(mSpinnerAdapter.getItem(position)!=null) {
mSelectedTextView.setText((CharSequence) ((MyDataItem)mSpinnerAdapter.getItem(arg2)).getName());
}
}
The data comes from a web service and is therefore slow, so as long as the data is not loaded, I am displaying a progress bar. I am doing this by telling the adapter to deliver a progress bar in "getView(..)"¨and a "1" in "getCount()" as long as the data structure is not fully loaded (Hence the if(mSpinnerAdapter.getItem(position)!=null) in the code above).
Now the problem is this: When the data from the web is loaded, I call onDatasetChanged() on the adapter and the gallery view is being updated (which works), but without onItemSelected() being called. The consequence of this is that the title is not set for the first item.
How can I solve this? Or is my technique to display the progress bar wrong alltogether?
Thank you!

Categories

Resources