Related
Here I need a gallery like view with only three images to be shown at a time on screen. In this the middle image will be larger than the two other images on its sides.
If the user scrolls the view next images will slide on screen as it does in gallery and at a time only three images will be shown out of which the center image should automatically zoom when it is shown on screen and remaining two should be smaller than it.
Here I can't use gallery because it is depreciated in android.
I was able to make a gallery like view with help of viewpager using code on this link. It shows only three images on screen at a time, which fits my one requirement. But i am not able to get the central image that is visible on screen and zoom it. Although I was able to get the clicked image on screen.
Can someone please tell me where do I need to modify this code and what I need to add in it to get the image that is in center from the images shown on screen and zoom it.
I know that there is no center image on screen according to viewpager and it is just showing three images on screen at a time because of modifications in code.
I have also tried:-
GridView with horizontal scroll
HorizontalScrollView with horizontal linear layout
but viewpager seems to be a better solution, because it stops the scrolling with only three items on screen because of viewpager's inherent properties.
and If someone knows any other method to achieve it, please tell me and I'll try it.
P.S. For anyone who wants the full code, I have added it as an answer, which has zoom capability also. Just few additions in accepted answer. :)
Following code will help you to make a gallery like view which will have center lock. It responds to touch and swipe both. It shows three images on the screen at a time and the center image is zoomed.
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class CenteringHorizontalScrollView extends HorizontalScrollView implements View.OnTouchListener {
private Context mContext;
private static final int SWIPE_PAGE_ON_FACTOR = 10;
private int mActiveItem;
private float mPrevScrollX;
private boolean mStart;
private int mItemWidth;
View targetLeft, targetRight;
ImageView leftImage, rightImage;
public CenteringHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext=context;
mItemWidth = 100; // or whatever your item width is.
setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getRawX();
boolean handled = false;
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (mStart) {
mPrevScrollX = x;
mStart = false;
}
break;
case MotionEvent.ACTION_UP:
mStart = true;
int minFactor = mItemWidth / SWIPE_PAGE_ON_FACTOR;
if ((mPrevScrollX - (float) x) > minFactor) {
if (mActiveItem < getMaxItemCount() - 1) {
mActiveItem = mActiveItem + 1;
}
}else if (((float) x - mPrevScrollX) > minFactor) {
if (mActiveItem > 0) {
mActiveItem = mActiveItem - 1;
}
}
scrollToActiveItem();
handled = true;
break;
}
return handled;
}
private int getMaxItemCount() {
return ((LinearLayout) getChildAt(0)).getChildCount();
}
private LinearLayout getLinearLayout() {
return (LinearLayout) getChildAt(0);
}
/**
* Centers the current view the best it can.
*/
public void centerCurrentItem() {
if (getMaxItemCount() == 0)
return;
int currentX = getScrollX();
View targetChild;
int currentChild = -1;
do {
currentChild++;
targetChild = getLinearLayout().getChildAt(currentChild);
} while (currentChild < getMaxItemCount() && targetChild.getLeft() < currentX);
if (mActiveItem != currentChild) {
mActiveItem = currentChild;
scrollToActiveItem();
}
}
/**
* Scrolls the list view to the currently active child.
*/
private void scrollToActiveItem() {
int maxItemCount = getMaxItemCount();
if (maxItemCount == 0)
return;
int targetItem = Math.min(maxItemCount - 1, mActiveItem);
targetItem = Math.max(0, targetItem);
mActiveItem = targetItem;
// Scroll so that the target child is centered
View targetView = getLinearLayout().getChildAt(targetItem);
ImageView centerImage = (ImageView)targetView;
int height=300;//set size of centered image
LinearLayout.LayoutParams flparams = new LinearLayout.LayoutParams(height, height);
centerImage.setLayoutParams(flparams);
//get the image to left of the centered image
if((targetItem-1)>=0){
targetLeft = getLinearLayout().getChildAt(targetItem-1);
leftImage = (ImageView)targetLeft;
int width=250;//set the size of left image
LinearLayout.LayoutParams leftParams = new LinearLayout.LayoutParams(width,width);
leftParams.setMargins(0, 30, 0, 0);
leftImage.setLayoutParams(leftParams);
}
//get the image to right of the centered image
if((targetItem+1)<maxItemCount){
targetRight = getLinearLayout().getChildAt(targetItem+1);
rightImage = (ImageView)targetRight;
int width=250;//set the size of right image
LinearLayout.LayoutParams rightParams = new LinearLayout.LayoutParams(width,width);
rightParams.setMargins(0, 30, 0, 0);
rightImage.setLayoutParams(rightParams);
}
int targetLeft = targetView.getLeft();
int childWidth = targetView.getRight() - targetLeft;
int width = getWidth() - getPaddingLeft() - getPaddingRight();
int targetScroll = targetLeft - ((width - childWidth) / 2);
super.smoothScrollTo(targetScroll, 0);
}
/**
* Sets the current item and centers it.
* #param currentItem The new current item.
*/
public void setCurrentItemAndCenter(int currentItem) {
mActiveItem = currentItem;
scrollToActiveItem();
}
}
In your xml add the horizontal scroll view like follow:-
<com.yourpackagename.CenteringHorizontalScrollView
android:id="#+id/HSVImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Horizontalalternative">
<LinearLayout
android:id="#+id/linearImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</com.yourpackagename.CenteringHorizontalScrollView>
Define a Linear layout in your activity.
LinearLayout imageGallery;
Then get it as follows:-
imageGallery=(LinearLayout)findViewById(R.id.linearImage);
Now you have to add imageView to your LinearLayout. Here I assume that you have images in your drawable folder and you have made an array of ids of your images that you want to add to gallery. So you can do it via following method in your activity:-
for(int i=0; i<lengthOfImageIdArray; i++){
ImageView image=new ImageView(YourActivityName.this);
image.setBackgroundResource(yourArrayName[i]);
imageGallery.addView(image);
}
You can also set the width of images dynamically, so that they fit every screen, with only little extra effort.
Override setPrimaryItem in your ViewPager and make the center item bigger.
What was the issue with using a HorizontalScrollView with a LinearLayout? If it's centering you may be able to do something similar to this (assuming you've
/**
* A centering HSV loosely based on http://iotasol.blogspot.com/2011/08/creating-custom-horizontal-scroll-view.html
*/
public class CenteringHorizontalScrollView extends HorizontalScrollView implements View.OnTouchListener {
private static final int SWIPE_PAGE_ON_FACTOR = 10;
private int mActiveItem;
private float mPrevScrollX;
private boolean mStart;
private int mItemWidth;
public CenteringHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mItemWidth = 100; // or whatever your item width is.
setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getRawX();
boolean handled = false;
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (mStart) {
mPrevScrollX = x;
mStart = false;
}
break;
case MotionEvent.ACTION_UP:
mStart = true;
int minFactor = mItemWidth / SWIPE_PAGE_ON_FACTOR;
if ((mPrevScrollX - (float) x) > minFactor) {
if (mActiveItem < getMaxItemCount() - 1) {
mActiveItem = mActiveItem + 1;
}
}
else if (((float) x - mPrevScrollX) > minFactor) {
if (mActiveItem > 0) {
mActiveItem = mActiveItem - 1;
}
}
scrollToActiveItem();
handled = true;
break;
}
return handled;
}
private int getMaxItemCount() {
return ((LinearLayout) getChildAt(0)).getChildCount();
}
private LinearLayout getLinearLayout() {
return (LinearLayout) getChildAt(0);
}
/**
* Centers the current view the best it can.
*/
public void centerCurrentItem() {
if (getMaxItemCount() == 0) {
return;
}
int currentX = getScrollX();
View targetChild;
int currentChild = -1;
do {
currentChild++;
targetChild = getLinearLayout().getChildAt(currentChild);
} while (currentChild < getMaxItemCount() && targetChild.getLeft() < currentX);
if (mActiveItem != currentChild) {
mActiveItem = currentChild;
scrollToActiveItem();
}
}
/**
* Scrolls the list view to the currently active child.
*/
private void scrollToActiveItem() {
int maxItemCount = getMaxItemCount();
if (maxItemCount == 0) {
return;
}
int targetItem = Math.min(maxItemCount - 1, mActiveItem);
targetItem = Math.max(0, targetItem);
mActiveItem = targetItem;
// Scroll so that the target child is centered
View targetView = getLinearLayout().getChildAt(targetItem);
int targetLeft = targetView.getLeft();
int childWidth = targetView.getRight() - targetLeft;
int width = getWidth() - getPaddingLeft() - getPaddingRight();
int targetScroll = targetLeft - ((width - childWidth) / 2);
super.smoothScrollTo(targetScroll, 0);
}
/**
* Sets the current item and centers it.
* #param currentItem The new current item.
*/
public void setCurrentItemAndCenter(int currentItem) {
mActiveItem = currentItem;
scrollToActiveItem();
}
}
How can i open new activity(at listview element click) keeping the same drawer on the right/left?
If you specify a drawer layout for your new activity (in the XML) and copy over the same Navigation Drawer code from your original Activity in to your new Activity, it should work. If you were to do this, I would recommend moving all common code (such as any custom list adapters you may be using to populate the navigation list within the drawer, your onItemClickListener(), etc.) into its own class and having your two activities access the common methods from there. However, if you have the option of using fragments, as #Matt_9.0 suggested, it will make the navigation drawer more maintainable down the road.
Check This
is tutorial for what u want
Then too with bummi's suggestions , here is the code
XML layout
<!-- This holds our menu -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/activity_main_menu_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/menu"
android:cacheColorHint="#00000000" >
</ListView>
</LinearLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main_content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White" >
</FrameLayout>
this fragment is used to contain the different layouts whichever you want
com.package.appname.layout package MainLayout.class
public class MainLayout extends LinearLayout {
// Duration of sliding animation, in miliseconds
private static final int SLIDING_DURATION = 500;
// Query Scroller every 16 miliseconds
private static final int QUERY_INTERVAL = 16;
// MainLayout width
int mainLayoutWidth;
// Sliding menu
private View menu;
// Main content
private View content;
private View subContent = findViewById(R.id.activity_main_content_fragment);
// menu does not occupy some right space
// This should be updated correctly later in onMeasure
private static int menuRightMargin = 150;
// The state of menu
private enum MenuState {
HIDING,
HIDDEN,
SHOWING,
SHOWN,
};
// content will be layouted based on this X offset
// Normally, contentXOffset = menu.getLayoutParams().width = this.getWidth - menuRightMargin
private int contentXOffset;
// menu is hidden initially
private MenuState currentMenuState = MenuState.HIDDEN;
// Scroller is used to facilitate animation
private Scroller menuScroller = new Scroller(this.getContext(),
new EaseInInterpolator());
// Used to query Scroller about scrolling position
// Note: The 3rd paramter to startScroll is the distance
private Runnable menuRunnable = new MenuRunnable();
private Handler menuHandler = new Handler();
// Previous touch position
int prevX = 0;
// Is user dragging the content
boolean isDragging = false;
// Used to facilitate ACTION_UP
int lastDiffX = 0;
// Constructor
// 3 parameters constructor seems to be unavailable in 2.3
/*
public MainLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
*/
public MainLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MainLayout(Context context) {
super(context);
}
// Overriding LinearLayout core methods
// Ask all children to measure themselves and compute the measurement of this
// layout based on the children
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mainLayoutWidth = MeasureSpec.getSize(widthMeasureSpec);
menuRightMargin = mainLayoutWidth * 10 / 100;
// Nothing to do, since we only care about how to layout
}
// This is called when MainLayout is attached to window
// At this point it has a Surface and will start drawing.
// Note that this function is guaranteed to be called before onDraw
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
// Get our 2 child View
menu = this.getChildAt(0);
content = this.getChildAt(1);
content.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return MainLayout.this.onContentTouch(v, event);
}
});
// Initially hide the menu
menu.setVisibility(View.GONE);
}
// Called from layout when this view should assign a size and position to each of its children
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
//Log.d("MainLayout.java onLayout()", "left " + left + " top " + top + " right " + right + " bottom " + bottom);
//Log.d("MainLayout.java onLayout()", "getHeight " + this.getHeight() + " getWidth " + this.getWidth());
// True if MainLayout 's size and position has changed
// If true, calculate child views size
if(changed) {
// Note: LayoutParams are used by views to tell their parents how they want to be laid out
//Log.d("MainLayout.java onLayout()", "changed " + changed);
// content View occupies the full height and width
LayoutParams contentLayoutParams = (LayoutParams)content.getLayoutParams();
contentLayoutParams.height = this.getHeight();
contentLayoutParams.width = this.getWidth();
// menu View occupies the full height, but certain width
LayoutParams menuLayoutParams = (LayoutParams)menu.getLayoutParams();
menuLayoutParams.height = this.getHeight();
menuLayoutParams.width = this.getWidth() - menuRightMargin;
}
// Layout the child views
menu.layout(left, top, right - menuRightMargin, bottom);
content.layout(left + contentXOffset, top, right + contentXOffset, bottom);
}
// Custom methods for MainLayout
// Used to show/hide menu accordingly
public void toggleMenu() {
// Do nothing if sliding is in progress
if(currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
return;
switch(currentMenuState) {
case HIDDEN:
currentMenuState = MenuState.SHOWING;
menu.setVisibility(View.VISIBLE);
menuScroller.startScroll(0, 0, menu.getLayoutParams().width,
0, SLIDING_DURATION);
break;
case SHOWN:
currentMenuState = MenuState.HIDING;
menuScroller.startScroll(contentXOffset, 0, -contentXOffset,
0, SLIDING_DURATION);
break;
default:
break;
}
// Begin querying
menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
// Invalite this whole MainLayout, causing onLayout() to be called
this.invalidate();
}
// Query Scroller
protected class MenuRunnable implements Runnable {
#Override
public void run() {
boolean isScrolling = menuScroller.computeScrollOffset();
adjustContentPosition(isScrolling);
}
}
// Adjust content View position to match sliding animation
private void adjustContentPosition(boolean isScrolling) {
int scrollerXOffset = menuScroller.getCurrX();
//Log.d("MainLayout.java adjustContentPosition()", "scrollerOffset " + scrollerOffset);
// Translate content View accordingly
content.offsetLeftAndRight(scrollerXOffset - contentXOffset);
contentXOffset = scrollerXOffset;
// Invalite this whole MainLayout, causing onLayout() to be called
this.invalidate();
// Check if animation is in progress
if (isScrolling)
menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
else
this.onMenuSlidingComplete();
}
// Called when sliding is complete
private void onMenuSlidingComplete() {
switch (currentMenuState) {
case SHOWING:
currentMenuState = MenuState.SHOWN;
break;
case HIDING:
currentMenuState = MenuState.HIDDEN;
menu.setVisibility(View.GONE);
break;
default:
return;
}
}
// Make scrolling more natural. Move more quickly at the end
// See the formula here http://cyrilmottier.com/2012/05/22/the-making-of-prixing-fly-in-app-menu-part-1/
protected class EaseInInterpolator implements Interpolator {
#Override
public float getInterpolation(float t) {
return (float)Math.pow(t-1, 5) + 1;
}
}
// Is menu completely shown
public boolean isMenuShown() {
return currentMenuState == MenuState.SHOWN;
}
// Handle touch event on content View
public boolean onContentTouch(View v, MotionEvent event) {
// Do nothing if sliding is in progress
if(currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
return false;
// getRawX returns X touch point corresponding to screen
// getX sometimes returns screen X, sometimes returns content View X
int curX = (int)event.getRawX();
int diffX = 0;
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Log.d("MainLayout.java onContentTouch()", "Down x " + curX);
prevX = curX;
return true;
case MotionEvent.ACTION_MOVE:
//Log.d("MainLayout.java onContentTouch()", "Move x " + curX);
// Set menu to Visible when user start dragging the content View
if(!isDragging) {
isDragging = true;
menu.setVisibility(View.VISIBLE);
}
// How far we have moved since the last position
diffX = curX - prevX;
// Prevent user from dragging beyond border
if(contentXOffset + diffX <= 0) {
// Don't allow dragging beyond left border
// Use diffX will make content cross the border, so only translate by -contentXOffset
diffX = -contentXOffset;
} else if(contentXOffset + diffX > mainLayoutWidth - menuRightMargin) {
// Don't allow dragging beyond menu width
diffX = mainLayoutWidth - menuRightMargin - contentXOffset;
}
// Translate content View accordingly
content.offsetLeftAndRight(diffX);
contentXOffset += diffX;
// Invalite this whole MainLayout, causing onLayout() to be called
this.invalidate();
prevX = curX;
lastDiffX = diffX;
return true;
case MotionEvent.ACTION_UP:
//Log.d("MainLayout.java onContentTouch()", "Up x " + curX);
Log.d("MainLayout.java onContentTouch()", "Up lastDiffX " + lastDiffX);
// Start scrolling
// Remember that when content has a chance to cross left border, lastDiffX is set to 0
if(lastDiffX > 0) {
// User wants to show menu
currentMenuState = MenuState.SHOWING;
// No need to set to Visible, because we have set to Visible in ACTION_MOVE
//menu.setVisibility(View.VISIBLE);
//Log.d("MainLayout.java onContentTouch()", "Up contentXOffset " + contentXOffset);
// Start scrolling from contentXOffset
menuScroller.startScroll(contentXOffset, 0, menu.getLayoutParams().width - contentXOffset,
0, SLIDING_DURATION);
} else if(lastDiffX < 0) {
// User wants to hide menu
currentMenuState = MenuState.HIDING;
menuScroller.startScroll(contentXOffset, 0, -contentXOffset,
0, SLIDING_DURATION);
}
// Begin querying
menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
// Invalite this whole MainLayout, causing onLayout() to be called
this.invalidate();
// Done dragging
isDragging = false;
prevX = 0;
lastDiffX = 0;
return true;
default:
break;
}
return false;
}
}
Now create fragment layouts and call them in the MainACtivity.class
like
public class HomeActivity extends FragmentActivity {
ImageButton ibMenu;
View view;
MotionEvent events;
boolean doubleBackToExitPressedOnce=false;
ImageButton iv,ivabout_content;
MainLayout mainLayout;
// ListView menu
private ListView lvMenu;
private String[] lvMenuItems;
TextView tvTitle;
public View.OnTouchListener gestureListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout = (MainLayout) this.getLayoutInflater().inflate(R.layout.activity_home, null);
setContentView(mainLayout);
// Init menu
lvMenuItems = getResources().getStringArray(R.array.menu_items);
lvMenu = (ListView) findViewById(R.id.activity_main_menu_listview);
ArrayAdapter<String> menuAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, lvMenuItems);
lvMenu.setAdapter(menuAdapter);
iv = (ImageButton) findViewById(R.id.ibHomeLatestEvent);
ivabout_content = (ImageButton) findViewById(R.id.ibAboutLearn);
lvMenu.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
onMenuItemClick(parent,view,position,id);
}
});
ibMenu = (ImageButton) findViewById(R.id.activity_main_content_button_menu);
ibMenu.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
toggleMenu(v);
}
});
tvTitle = (TextView) findViewById(R.id.activity_main_content_title);
FragmentManager fm = HomeActivity.this.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment = new Fragment();
ft.add(R.id.activity_main_content_fragment, fragment);
ft.commit();
gestureListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mainLayout.onContentTouch(v, event);
}
};
iv.setOnTouchListener(gestureListener);
}
public void toggleMenu(View v) {
// TODO Auto-generated method stub
mainLayout.toggleMenu();
}
protected void onMenuItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String selectedItem = lvMenuItems[position];
String currentItem = tvTitle.getText().toString();
// Do nothing if selectedItem is currentItem
if(selectedItem.compareTo(currentItem)==0){
mainLayout.toggleMenu();
return;
}
FragmentManager fm = HomeActivity.this.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment = null;
if(selectedItem.compareTo("Home") == 0) {
fragment = new FragmentHome();
} else if(selectedItem.compareTo("About") == 0) {
fragment = new FragmentAbout();
} else if(selectedItem.compareTo("What's new") == 0) {
fragment = new Fragmentnew();
} else if(selectedItem.compareTo("Things to do") == 0) {
fragment = new FragmentThingsToDo();
} else if(selectedItem.compareTo("Holiday in Lavasa") == 0) {
fragment = new FragmentHolidayInLavasa();
} else if(selectedItem.compareTo("Offers") == 0) {
fragment = new FragmentOffers();
} else if(selectedItem.compareTo("Getting to Lavasa") == 0) {
fragment = new FragmentGettingLavasa();
} else if(selectedItem.compareTo("Map") == 0){
fragment = new FragmentMap();
} else if(selectedItem.compareTo("Downloads") == 0) {
fragment = new FragmentDownloads();
} else if(selectedItem.compareTo("Help Desk") == 0) {
}
if(fragment != null) {
// Replace current fragment by this new one
ft.replace(R.id.activity_main_content_fragment, fragment).addToBackStack( "tag" ).commit();
// Set title accordingly
tvTitle.setText(selectedItem);
}
// Hide menu anyway
mainLayout.toggleMenu();
}
#Override
public void onBackPressed() {
if (mainLayout.isMenuShown()) {
mainLayout.toggleMenu();
}
else if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
}
If you want further help let me know..
To understand this question, first read how this method works.
I am trying to implements a drag and drop ListView, 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) MotionEvents 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 Bitmap for the image that I drag around like so. itemPositition being the index of the item that was selected.
(omitting irrelevant parts)
...
View dragItem = mListView.getChildAt(itemPosition);
dragItem.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(dragItem.getDrawingCache());
mDragImage = new ImageView(mContext);
mDragImage.setImageBitmap(bitmap);
...
The problem is, mDragImage is a solid black like this.
But, if I don't let ListView handle anything. As in, I start the drag on ACTION_DOWN and stop on ACTION_UP, mDragImage looks has expected(but I obviously lose scrolling abilities).
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).
I see two options for fixing this, neither of which I know how to do.
Create a image from scratch.
Handle the highlighting myself(if that is the problem).
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.
I set setOnItemClickListener(...) and
setOnItemSelectedListener(...) with a empty method(highlighting
still happens). (Before anyone suggests it, calling
setOnClickListener results in a runtime error.)
I also looked into trying to get the ListView to make a new item
(for option 2), but could not find a way.
Spent 45ish minutes looking through the source code and
documentation trying to pinpoint where the highlighting was
happening(I never found it).
Any help fixing this would be appreciated.
(EDIT1 START)
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.
(EDIT1 END)
Last minute edit before post. I tried using onLongClickListener 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.
use this code, it's allows operation drug and drop in ListView:
public class DraggableListView extends ListView {
private static final String LOG_TAG = "tasks365";
private static final int END_OF_LIST_POSITION = -2;
private DropListener mDropListener;
private int draggingItemHoverPosition;
private int dragStartPosition; // where was the dragged item originally
private int mUpperBound; // scroll the view when dragging point is moving out of this bound
private int mLowerBound; // scroll the view when dragging point is moving out of this bound
private int touchSlop;
private Dragging dragging;
private GestureDetector longPressDetector;
public DraggableListView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.listViewStyle);
}
public DraggableListView(final Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
longPressDetector = new GestureDetector(getContext(), new SimpleOnGestureListener() {
#Override
public void onLongPress(final MotionEvent e) {
int x = (int) e.getX();
final int y = (int) e.getY();
int itemnum = pointToPosition(x, y);
if (itemnum == AdapterView.INVALID_POSITION) {
return;
}
if (dragging != null) {
dragging.stop();
dragging = null;
}
final View item = getChildAt(itemnum - getFirstVisiblePosition());
item.setPressed(false);
dragging = new Dragging(getContext());
dragging.start(y, ((int) e.getRawY()) - y, item);
draggingItemHoverPosition = itemnum;
dragStartPosition = draggingItemHoverPosition;
int height = getHeight();
mUpperBound = Math.min(y - touchSlop, height / 3);
mLowerBound = Math.max(y + touchSlop, height * 2 / 3);
}
});
setOnItemLongClickListener(new OnItemLongClickListener() {
#SuppressWarnings("unused")
public boolean onItemLongClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) {
// Return true to let AbsListView reset touch mode
// Without this handler, the pressed item will keep highlight.
return true;
}
});
}
/* pointToPosition() doesn't consider invisible views, but we need to, so implement a slightly different version. */
private int myPointToPosition(int x, int y) {
if (y < 0) {
return getFirstVisiblePosition();
}
Rect frame = new Rect();
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
child.getHitRect(frame);
if (frame.contains(x, y)) {
return getFirstVisiblePosition() + i;
}
}
if ((x >= frame.left) && (x < frame.right) && (y >= frame.bottom)) {
return END_OF_LIST_POSITION;
}
return INVALID_POSITION;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
if (longPressDetector.onTouchEvent(ev)) {
return true;
}
if ((dragging == null) || (mDropListener == null)) {
// it is not dragging, or there is no drop listener
return super.onTouchEvent(ev);
}
int action = ev.getAction();
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
dragging.stop();
dragging = null;
if (mDropListener != null) {
if (draggingItemHoverPosition == END_OF_LIST_POSITION) {
mDropListener.drop(dragStartPosition, getCount() - 1);
} else if (draggingItemHoverPosition != INVALID_POSITION) {
mDropListener.drop(dragStartPosition, draggingItemHoverPosition);
}
}
resetViews();
break;
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
int x = (int) ev.getX();
int y = (int) ev.getY();
dragging.drag(x, y);
int position = dragging.calculateHoverPosition();
if (position != INVALID_POSITION) {
if ((action == MotionEvent.ACTION_DOWN) || (position != draggingItemHoverPosition)) {
draggingItemHoverPosition = position;
doExpansion();
}
scrollList(y);
}
break;
}
return true;
}
private void doExpansion() {
int expanItemViewIndex = draggingItemHoverPosition - getFirstVisiblePosition();
if (draggingItemHoverPosition >= dragStartPosition) {
expanItemViewIndex++;
}
// Log.v(LOG_TAG, "Dragging item hovers over position " + draggingItemHoverPosition + ", expand item at index "
// + expanItemViewIndex);
View draggingItemOriginalView = getChildAt(dragStartPosition - getFirstVisiblePosition());
for (int i = 0;; i++) {
View itemView = getChildAt(i);
if (itemView == null) {
break;
}
ViewGroup.LayoutParams params = itemView.getLayoutParams();
int height = LayoutParams.WRAP_CONTENT;
if (itemView.equals(draggingItemOriginalView)) {
height = 1;
} else if (i == expanItemViewIndex) {
height = itemView.getHeight() + dragging.getDraggingItemHeight();
}
params.height = height;
itemView.setLayoutParams(params);
}
}
/**
* Reset view to original height.
*/
private void resetViews() {
for (int i = 0;; i++) {
View v = getChildAt(i);
if (v == null) {
layoutChildren(); // force children to be recreated where needed
v = getChildAt(i);
if (v == null) {
break;
}
}
ViewGroup.LayoutParams params = v.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
v.setLayoutParams(params);
}
}
private void resetScrollBounds(int y) {
int height = getHeight();
if (y >= height / 3) {
mUpperBound = height / 3;
}
if (y <= height * 2 / 3) {
mLowerBound = height * 2 / 3;
}
}
private void scrollList(int y) {
resetScrollBounds(y);
int height = getHeight();
int speed = 0;
if (y > mLowerBound) {
// scroll the list up a bit
speed = y > (height + mLowerBound) / 2 ? 16 : 4;
} else if (y < mUpperBound) {
// scroll the list down a bit
speed = y < mUpperBound / 2 ? -16 : -4;
}
if (speed != 0) {
int ref = pointToPosition(0, height / 2);
if (ref == AdapterView.INVALID_POSITION) {
//we hit a divider or an invisible view, check somewhere else
ref = pointToPosition(0, height / 2 + getDividerHeight() + 64);
}
View v = getChildAt(ref - getFirstVisiblePosition());
if (v != null) {
int pos = v.getTop();
setSelectionFromTop(ref, pos - speed);
}
}
}
public void setDropListener(DropListener l) {
mDropListener = l;
}
public interface DropListener {
void drop(int from, int to);
}
class Dragging {
private Context context;
private WindowManager windowManager;
private WindowManager.LayoutParams mWindowParams;
private ImageView mDragView;
private Bitmap mDragBitmap;
private int coordOffset;
private int mDragPoint; // at what offset inside the item did the user grab it
private int draggingItemHeight;
private int x;
private int y;
private int lastY;
public Dragging(Context context) {
this.context = context;
windowManager = (WindowManager) context.getSystemService("window");
}
/**
* #param y
* #param offset - the difference in y axis between screen coordinates and coordinates in this view
* #param view - which view is dragged
*/
public void start(int y, int offset, View view) {
this.y = y;
lastY = y;
this.coordOffset = offset;
mDragPoint = y - view.getTop();
draggingItemHeight = view.getHeight();
mDragView = new ImageView(context);
mDragView.setBackgroundResource(android.R.drawable.alert_light_frame);
// Create a copy of the drawing cache so that it does not get recycled
// by the framework when the list tries to clean up memory
view.setDrawingCacheEnabled(true);
mDragBitmap = Bitmap.createBitmap(view.getDrawingCache());
mDragView.setImageBitmap(mDragBitmap);
mWindowParams = new WindowManager.LayoutParams();
mWindowParams.gravity = Gravity.TOP;
mWindowParams.x = 0;
mWindowParams.y = y - mDragPoint + coordOffset;
mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
mWindowParams.format = PixelFormat.TRANSLUCENT;
mWindowParams.windowAnimations = 0;
windowManager.addView(mDragView, mWindowParams);
}
public void drag(int x, int y) {
lastY = this.y;
this.x = x;
this.y = y;
mWindowParams.y = y - mDragPoint + coordOffset;
windowManager.updateViewLayout(mDragView, mWindowParams);
}
public void stop() {
if (mDragView != null) {
windowManager.removeView(mDragView);
mDragView.setImageDrawable(null);
mDragView = null;
}
if (mDragBitmap != null) {
mDragBitmap.recycle();
mDragBitmap = null;
}
}
public int getDraggingItemHeight() {
return draggingItemHeight;
}
public int calculateHoverPosition() {
int adjustedY = (int) (y - mDragPoint + (Math.signum(y - lastY) + 2) * draggingItemHeight / 2);
// Log.v(LOG_TAG, "calculateHoverPosition(): lastY=" + lastY + ", y=" + y + ", adjustedY=" + adjustedY);
int pos = myPointToPosition(0, adjustedY);
if (pos >= 0) {
if (pos >= dragStartPosition) {
pos -= 1;
}
}
return pos;
}
}
}
I have to make an application in which the user moves around a screen, which is like our android home screen.
There is a list of images that come, and we can scroll through the images in a horizontal scroll er.
The user cannot change the location of images, it's just like a few thumbnails arranged over a horizontal screen
This is like the paging control in iPhone app development.
I have tried to find ways to do this, but I am fairly new to android and I wanted to know the best way to achieve the above?
I have heard of the gallery control, but I am not sure if it will suit my purposes.
Also if you can give links to the answers you suggest.. it would be great as if there is a new controller involved , I will be able to understand it better as I am still a fresher.
Thank you in advance.
Edit: For those who are unfamiliar with the iPhone paging view,here is a video example.
In my search to implement this, I found many implementation like below
http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/
https://github.com/ysamlan/horizontalpager
http://code.google.com/p/deezapps-widgets/
Well, you could look at the source code for the android homescreen, since it's open source. Maybe you can get some ideas from there.
The GreenDroid library (a collection of useful Android components) recently added a PagedView class that does exactly what you're looking for. It also includes a PageIndicator that works like the iOS dots.
It uses a Adapter system, similar to ListView (with efficient View re-use, etc), which I haven't seen in any of the other implementations of this pattern.
Source code:
https://github.com/cyrilmottier/GreenDroid
Demo application:
https://market.android.com/details?id=com.cyrilmottier.android.gdcatalog
You could use a HorizontalScrollView to hold a LinearLayout containing all your image views.
I think you want like the paging control in iphone app development, in android this functionality is available by using ViewFlipper. It may help you that you want to implement.
Like dmon said you can try something with the android homescreen app OR try this http://code.google.com/p/deezapps-widgets/
This seems to me like a custom implementation of the homescreen from android.. and has the page control as well jus like the iphone.. I think this is what you are looking for...
I have hacked around with items posted here: Horizontal "tab"ish scroll between views
I had good luck replicating a horizontal scrolling type navigation that your describing.
Here is a solution to this problem with a lot of helpful code.
I posted code to do something like the Android homescreen before : Developing an Android Homescreen
Note that will make the whole screen flip between pages, it will not work if you want to flip only part of the screen like shown on the video you linked to.
First the source code
package com.matthieu.launcher;
import android.content.Context;
import android.util.Log;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewConfiguration;
import android.widget.Scroller;
public class DragableSpace extends ViewGroup {
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mScrollX = 0;
private int mCurrentScreen = 0;
private float mLastMotionX;
private static final String LOG_TAG = "DragableSpace";
private static final int SNAP_VELOCITY = 1000;
private final static int TOUCH_STATE_REST = 0;
private final static int TOUCH_STATE_SCROLLING = 1;
private int mTouchState = TOUCH_STATE_REST;
private int mTouchSlop = 0;
public DragableSpace(Context context) {
super(context);
mScroller = new Scroller(context);
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
this.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.FILL_PARENT));
}
public DragableSpace(Context context, AttributeSet attrs) {
super(context, attrs);
mScroller = new Scroller(context);
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
this.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT ,
ViewGroup.LayoutParams.FILL_PARENT));
TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.DragableSpace);
mCurrentScreen = a.getInteger(R.styleable.DragableSpace_default_screen, 0);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
/*
* This method JUST determines whether we want to intercept the motion.
* If we return true, onTouchEvent will be called and we do the actual
* scrolling there.
*/
/*
* Shortcut the most recurring case: the user is in the dragging state
* and he is moving his finger. We want to intercept this motion.
*/
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE)
&& (mTouchState != TOUCH_STATE_REST)) {
return true;
}
final float x = ev.getX();
switch (action) {
case MotionEvent.ACTION_MOVE:
/*
* mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
* whether the user has moved far enough from his original down touch.
*/
/*
* Locally do absolute value. mLastMotionX is set to the y value
* of the down event.
*/
final int xDiff = (int) Math.abs(x - mLastMotionX);
boolean xMoved = xDiff > mTouchSlop;
if (xMoved) {
// Scroll if the user moved far enough along the X axis
mTouchState = TOUCH_STATE_SCROLLING;
}
break;
case MotionEvent.ACTION_DOWN:
// Remember location of down touch
mLastMotionX = x;
/*
* If being flinged and user touches the screen, initiate drag;
* otherwise don't. mScroller.isFinished should be false when
* being flinged.
*/
mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
// Release the drag
mTouchState = TOUCH_STATE_REST;
break;
}
/*
* The only time we want to intercept motion events is if we are in the
* drag mode.
*/
return mTouchState != TOUCH_STATE_REST;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
final int action = event.getAction();
final float x = event.getX();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.i(LOG_TAG, "event : down");
/*
* If being flinged and user touches, stop the fling. isFinished
* will be false if being flinged.
*/
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
// Remember where the motion event started
mLastMotionX = x;
break;
case MotionEvent.ACTION_MOVE:
// Log.i(LOG_TAG,"event : move");
// if (mTouchState == TOUCH_STATE_SCROLLING) {
// Scroll to follow the motion event
final int deltaX = (int) (mLastMotionX - x);
mLastMotionX = x;
//Log.i(LOG_TAG, "event : move, deltaX " + deltaX + ", mScrollX " + mScrollX);
if (deltaX < 0) {
if (mScrollX > 0) {
scrollBy(Math.max(-mScrollX, deltaX), 0);
}
} else if (deltaX > 0) {
final int availableToScroll = getChildAt(getChildCount() - 1)
.getRight()
- mScrollX - getWidth();
if (availableToScroll > 0) {
scrollBy(Math.min(availableToScroll, deltaX), 0);
}
}
// }
break;
case MotionEvent.ACTION_UP:
Log.i(LOG_TAG, "event : up");
// if (mTouchState == TOUCH_STATE_SCROLLING) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
int velocityX = (int) velocityTracker.getXVelocity();
if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
// Fling hard enough to move left
snapToScreen(mCurrentScreen - 1);
} else if (velocityX < -SNAP_VELOCITY
&& mCurrentScreen < getChildCount() - 1) {
// Fling hard enough to move right
snapToScreen(mCurrentScreen + 1);
} else {
snapToDestination();
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
// }
mTouchState = TOUCH_STATE_REST;
break;
case MotionEvent.ACTION_CANCEL:
Log.i(LOG_TAG, "event : cancel");
mTouchState = TOUCH_STATE_REST;
}
mScrollX = this.getScrollX();
return true;
}
private void snapToDestination() {
final int screenWidth = getWidth();
final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth;
Log.i(LOG_TAG, "from des");
snapToScreen(whichScreen);
}
public void snapToScreen(int whichScreen) {
Log.i(LOG_TAG, "snap To Screen " + whichScreen);
mCurrentScreen = whichScreen;
final int newX = whichScreen * getWidth();
final int delta = newX - mScrollX;
mScroller.startScroll(mScrollX, 0, delta, 0, Math.abs(delta) * 2);
invalidate();
}
public void setToScreen(int whichScreen) {
Log.i(LOG_TAG, "set To Screen " + whichScreen);
mCurrentScreen = whichScreen;
final int newX = whichScreen * getWidth();
mScroller.startScroll(newX, 0, 0, 0, 10);
invalidate();
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childLeft = 0;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
final int childWidth = child.getMeasuredWidth();
child.layout(childLeft, 0, childLeft + childWidth, child
.getMeasuredHeight());
childLeft += childWidth;
}
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("error mode.");
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("error mode.");
}
// The children are given the same width and height as the workspace
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
Log.i(LOG_TAG, "moving to screen "+mCurrentScreen);
scrollTo(mCurrentScreen * width, 0);
}
#Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
mScrollX = mScroller.getCurrX();
scrollTo(mScrollX, 0);
postInvalidate();
}
}
}
And the layout file :
<?xml version="1.0" encoding="utf-8"?>
<com.matthieu.launcher.DragableSpace xmlns:app="http://schemas.android.com/apk/res/com.matthieu.launcher"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/space"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:default_screen="1"
>
<include android:id="#+id/left" layout="#layout/left_screen" />
<include android:id="#+id/center" layout="#layout/initial_screen" />
<include android:id="#+id/right" layout="#layout/right_screen" />
</com.matthieu.launcher.DragableSpace>
To be able to have the extra attribute in the xml file, you want to save this in res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DragableSpace">
<attr name="default_screen" format="integer"/>
</declare-styleable>
</resources>
The original SO post also has a few comments to make it work correctly...
If you're specifically interested in duplicating the home screen-style paging control, take a look at this, with source at Github. It even includes code to do the little page dots on the bottom.
I have a Listview with fullscreen pictures. (Like a vertical gallery). My problem is that I want to override the listviews onTouchEvent so that it only changes one picture at a time and so that after scrolling (When no fling occurs), the selected picure centers itself.
My problem is that when i call getChildAt(getFirstVisiblePosition()), it returns null. I allready checked and getFirstVisiblePosition() is returning a view that is vissible at the moment.
I'm using smoothScrollToPosition to center the pictures.
My code is verry long, but the important part is here: (when recieving a MotionEvent.ACTION_UP).
case MotionEvent.ACTION_UP:
{
try
{
//Calculetes the velocity if the movement
int initialVelocity = (int)ModifiedVelocityTracker.getYVelocity();*/
if (mVelocityTracker == null)
{
mVelocityTracker = VelocityTracker.obtain();
}
final VelocityTracker ModifiedVelocityTracker = mVelocityTracker;
ModifiedVelocityTracker.computeCurrentVelocity(1000);
int initialVelocity = (int)ModifiedVelocityTracker.getYVelocity();
//Detects if the motion is a fling
if ((Math.abs(initialVelocity) >
ViewConfiguration.get(mContext).getScaledMinimumFlingVelocity()) &&
(getChildCount() > 0)) // TODO FLing
{
super.onTouchEvent(getEventWithACTION_MOVE(ev));
//Send the events to cancel the superclass fling
//MotionEvent me[] = mVelocityTracker.getTrickEvents();
MotionEvent me[] = getTrickEvents(ev);
for(int i=0;i<me.length;i++)
super.onTouchEvent(me[i]);
int firstVisiblePosition = getFirstVisiblePosition();
// Pint y=0 is located on the bottom, so a negative speed means
//the finger moved up and the picture to come is the one under
if(initialVelocity < 0) //EventUp-GoDown
{
if(firstVisiblePosition != getCount() - 1) //Can Move
{
//smoothScrollToPosition(firstVisiblePosition + 1);
View currentTopView = getChildAt(firstVisiblePosition);
int botom = currentTopView.getBottom();
botom = botom - firstVisiblePosition * getHeight();
smoothScrollBy(-botom, 500);
} else
{
smoothScrollToPosition(firstVisiblePosition);
}
} else //EventDown-GoUp
{
smoothScrollToPosition(firstVisiblePosition);
}
}
else // TODO Stay in the picture that has a bigger area shown
{
onTouchEvent = super.onTouchEvent(ev);
//Center The View after the parent stops moving it
/*
* FIREST VIEW |****| / SECOND VIEW | |
*
* CASE 1 Bottom over center - Set second view the main one
* |****|
* |****| Bottom of Top View at First visible Position
* | |
* |----| center line
* | |
* | |
* | |
*
* CASE 2 Bottom under center - Set first view the main one
* |****|
* |****|
* |****|
* |----| center line
* |****|
* |****| Bottom of Top View at First visible Position
* | |
* */
int firstVisiblePosition = getFirstVisiblePosition();
View currentTopView = getChildAt(firstVisiblePosition);
int botom;
if(currentTopView != null)
{
botom = currentTopView.getBottom();
}else{
currentTopView = getSelectedView();
if(firstVisiblePosition == getPositionForView(currentTopView))
{
botom = currentTopView.getBottom();
}
else
{
botom = currentTopView.getTop();
}
}
int center = getHeight()/2;
botom = botom - firstVisiblePosition * getHeight();
if(botom < center) //Case 1 - Scroll Down
{
//Checks if the top view is the last one.
//Shouldn't happen, but just in case.
if(firstVisiblePosition != getCount() - 1) //Can Move
{
smoothScrollToPosition(firstVisiblePosition + 1);
} else
{
smoothScrollToPosition(firstVisiblePosition);
}
}
else //Case 2
{
smoothScrollToPosition(firstVisiblePosition);
}
}
onTouchEvent = true;
} catch(NullPointerException e)
{
e.printStackTrace();
}
}
Tank you in advance.
Because of the comment.
import android.content.Context;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ListView;
public class FixedListView extends ListView{
final static String LOGTAG = "TEST";
private Context mContext;
VelocityTracker mVelocityTracker;
public FixedListView(Context context) {
super(context);
mContext = context;
setDivider(null);
// TODO Auto-generated constructor stub
}
#Override
public boolean onTouchEvent(MotionEvent ev)
{
boolean onTouchEvent = true;
final int action = ev.getAction();
//Add the event to the ModifiedVelocityTracker used for detect if if the motion is a fling (also creates it)
Log.d(LOGTAG, "Before add to Traker: " + SystemClock.uptimeMillis());
/*if (mVelocityTracker == null)
{
mVelocityTracker = ModifiedVelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);*/
Log.d(LOGTAG, "After add to Traker: " + SystemClock.uptimeMillis());
//Detect the event action type
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
{
Log.d(LOGTAG, "After eval action Traker: " + SystemClock.uptimeMillis());
onTouchEvent = super.onTouchEvent(ev);
Log.d(LOGTAG, "After super Y = " + ev.getY() +": " + SystemClock.uptimeMillis());
break;
}
case MotionEvent.ACTION_UP:
{
try
{
//Calculetes the velocity if the movement
//NOTE: I'm using my own VelocityTraker because the Android.Utils
//one can only be used once at a time and the superclass uses it.
/*final ModifiedVelocityTracker ModifiedVelocityTracker = mVelocityTracker;
ModifiedVelocityTracker.computeCurrentVelocity(1000);
int initialVelocity = (int)ModifiedVelocityTracker.getYVelocity();*/
if (mVelocityTracker == null)
{
mVelocityTracker = VelocityTracker.obtain();
}
final VelocityTracker ModifiedVelocityTracker = mVelocityTracker;
ModifiedVelocityTracker.computeCurrentVelocity(1000);
int initialVelocity = (int)ModifiedVelocityTracker.getYVelocity();
//Detects if the motion is a fling
if ((Math.abs(initialVelocity) >
ViewConfiguration.get(mContext).getScaledMinimumFlingVelocity()) &&
(getChildCount() > 0)) // TODO FLing
{
super.onTouchEvent(getEventWithACTION_MOVE(ev));
//Send the events to cancel the superclass fling
//MotionEvent me[] = mVelocityTracker.getTrickEvents();
MotionEvent me[] = getTrickEvents(ev);
for(int i=0;i<me.length;i++)
super.onTouchEvent(me[i]);
int firstVisiblePosition = getFirstVisiblePosition();
// Pint y=0 is located on the bottom, so a negative speed means
//the finger moved up and the picture to come is the one under
if(initialVelocity < 0) //EventUp-GoDown
{
if(firstVisiblePosition != getCount() - 1) //Can Move
{
//smoothScrollToPosition(firstVisiblePosition + 1);
View currentTopView = getChildAt(firstVisiblePosition);
int botom = currentTopView.getBottom();
botom = botom - firstVisiblePosition * getHeight();
smoothScrollBy(-botom, 500);
} else
{
smoothScrollToPosition(firstVisiblePosition);
}
} else //EventDown-GoUp
{
smoothScrollToPosition(firstVisiblePosition);
}
}
else // TODO Stay in the picture that has a bigger area shown
{
onTouchEvent = super.onTouchEvent(ev);
//Center The View after the parent stops moving it
/*
* FIREST VIEW |****| / SECOND VIEW | |
*
* CASE 1 Bottom over center - Set second view the main one
* |****|
* |****| Bottom of Top View at First visible Position
* | |
* |----| center line
* | |
* | |
* | |
*
* CASE 2 Bottom under center - Set first view the main one
* |****|
* |****|
* |****|
* |----| center line
* |****|
* |****| Bottom of Top View at First visible Position
* | |
* */
int firstVisiblePosition = getFirstVisiblePosition();
View currentTopView = getChildAt(firstVisiblePosition);
int botom;
if(currentTopView != null)
{
botom = currentTopView.getBottom();
}else{
currentTopView = getSelectedView();
if(firstVisiblePosition == getPositionForView(currentTopView))
{
botom = currentTopView.getBottom();
}
else
{
botom = currentTopView.getTop();
}
}
int center = getHeight()/2;
botom = botom - firstVisiblePosition * getHeight();
if(botom < center) //Case 1 - Scroll Down
{
//Checks if the top view is the last one.
//Shouldn't happen, but just in case.
if(firstVisiblePosition != getCount() - 1) //Can Move
{
smoothScrollToPosition(firstVisiblePosition + 1);
} else
{
smoothScrollToPosition(firstVisiblePosition);
}
}
else //Case 2
{
smoothScrollToPosition(firstVisiblePosition);
}
}
onTouchEvent = true;
} catch(NullPointerException e)
{
e.printStackTrace();
}
if(mVelocityTracker != null)
mVelocityTracker.recycle();
break;
}
case MotionEvent.ACTION_CANCEL:
{
try
{
onTouchEvent = super.onTouchEvent(ev);
//Center The View after the parent stops moving it
/*
* FIREST VIEW |****| / SECOND VIEW | |
*
* CASE 1 Bottom over center - Set second view the main one
* |****|
* |****| Bottom of Top View at First visible Position
* | |
* |----| center line
* | |
* | |
* | |
*
* CASE 2 Bottom under center - Set first view the main one
* |****|
* |****|
* |****|
* |----| center line
* |****|
* |****| Bottom of Top View at First visible Position
* | |
* */
int firstVisiblePosition = getFirstVisiblePosition();
View currentTopView = getChildAt(firstVisiblePosition);
int center = getHeight()/2;
if(currentTopView.getBottom() < center) //Case 1 - Scroll Down
{
//Checks if the top view is the last one.
//Shouldn't happen, but just in case.
if(firstVisiblePosition != getCount() - 1) //Can Move
{
smoothScrollToPosition(firstVisiblePosition + 1);
} else
{
smoothScrollToPosition(firstVisiblePosition);
}
}
else //Case 2
{
if(firstVisiblePosition != 0) //Can Move
{
smoothScrollToPosition(firstVisiblePosition - 1);
} else
{
smoothScrollToPosition(firstVisiblePosition);
}
}
onTouchEvent = true;
} catch(NullPointerException e)
{
e.printStackTrace();
}
if(mVelocityTracker != null)
{
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
}
// TODO Auto-generated method stub
return onTouchEvent;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
return super.onInterceptTouchEvent(ev);
}
public MotionEvent[] getTrickEvents(MotionEvent ev)
{
//Detect the last touched position
final float requiredX = ev.getX();
final float requiredY = ev.getY();
//Get a time value that is longer than the last added event value by a bigger
//number than the LONGEST_PAST_TIME accepted by the VelocityTraker
//NOTE: If GOOGLE changes the LONGEST_PAST_TIME, we will have to change it too,
//I wasn't able to retrieve the original VelocityTracker LONGEST_PAST_TIME directly from it.
final long requiredPastTime = ev.getEventTime() + 201;
//Create the MotionEvents (Simulating no movement in y).
MotionEvent m1 = null;
if(requiredX == 0) //If at the left border, move one pixel to the right.
{
m1 = MotionEvent.obtain(requiredPastTime, requiredPastTime,
MotionEvent.ACTION_MOVE,
requiredX + 1, requiredY, 0);
}
else //If not at the left border, move one pixel to the left
{
m1 = MotionEvent.obtain(requiredPastTime, requiredPastTime,
MotionEvent.ACTION_MOVE,
requiredX - 1, requiredY, 0);
}
//Return to the original position after 100 time units
MotionEvent m2 = MotionEvent.obtain(requiredPastTime + 100, requiredPastTime + 100,
MotionEvent.ACTION_UP,
requiredX, requiredY, 0);
MotionEvent motEvents[] = {m1,m2};
return motEvents;
}
public MotionEvent getEventWithACTION_MOVE(MotionEvent ev)
{
//Detect the last touched position
final float requiredX = ev.getX();
final float requiredY = ev.getY();
//Get a time value that is longer than the last added event value by a bigger
//number than the LONGEST_PAST_TIME accepted by the VelocityTraker
//NOTE: If GOOGLE changes the LONGEST_PAST_TIME, we will have to change it too,
//I wasn't able to retrieve the original VelocityTracker LONGEST_PAST_TIME directly from it.
final long Time = ev.getEventTime();
//Create the MotionEvent
MotionEvent m1 = MotionEvent.obtain(Time, Time,
MotionEvent.ACTION_MOVE,
requiredX , requiredY, 0);
return m1;
}
}
And the adapter
public class FixedListAdapter extends BaseAdapter
{
int pictures[];
public FixedListAdapter(int pictures[])
{
this.pictures = pictures;
}
public int getCount() {
return pictures.length;
}
public Object getItem(int position) {
return pictures[position];
}
public long getItemId(int position) {
return pictures[position];
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout l = new LinearLayout(GalleryTest.this);
l.setLayoutParams(new ListView.LayoutParams(
ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.MATCH_PARENT));
l.setGravity(Gravity.CENTER);
l.setPadding(0,0,0,0);
l.setBackgroundColor(Color.BLACK);
ImageView i = new ImageView(GalleryTest.this);
i.setLayoutParams(new LinearLayout.LayoutParams(width, height));
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setImageResource(pictures[position]);
l.addView(i);
return l;
}
}
NOTE: The picture changes on the device and the getFirstVisiblePosition() gives me the index of the first picture being shown. The problem is that getChildAt returns null with a view that is actually on screen.
Solved my problem.
I didn't find the reason for the problem, but changing the code a little bit I got to trick the problem.
int firstVisiblePosition = getFirstVisiblePosition();
View currentTopView = getChildAt(firstVisiblePosition);
int transpose = 0;
while(currentTopView == null)
{
transpose++;
currentTopView = getChildAt(firstVisiblePosition - transpose);
}
int botom = currentTopView.getBottom();
int top = currentTopView.getTop();
int height = getHeight();
botom = botom + height * (transpose - firstVisiblePosition);
top = top + height * (transpose - firstVisiblePosition);
What I did was to check if the view was null (even if it was being shown). If it was null I checked if the view over it was null, an so on until i go one that wasnt null on getChildAt(position).
When i found it, I used its position to calculate the position of the other view.
I also changed the code a little bit to use SmoothScrollBy insted os SmoothScrollToPosition because it made the view got perfectly centerded this way.
Now, If someone ever finds the reason for the problem, please tell me why its happening.