I'm trying to do a gallery of ImageViews but with HortizontalScrollView, because gallery is deprecated and it does not work well. But I don't find the form for slide the images like Gallery. I have done this code: - xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColor"
android:id="@+id/general"
tools:context="com.mappleapps.tm2ibz.Activity.PoblacionsActivity">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="198dp"
android:id="@+id/layGalMap">
<LinearLayout
android:id="@+id/mygallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
/>
</HorizontalScrollView>
Activity file:
hsv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP)
{
int i1 = 0;
int i2 = imageView.getWidth();
int i3 = i2*2;
int i4 = i2*3;
int i5 = i2*4;
float currentPosition = hsv.getScrollX();
float pagesCount = hsv.getChildCount();
float pageLengthInPx = imageView.getMeasuredWidth()/pagesCount;
float currentPage = currentPosition/pageLengthInPx;
Boolean isBehindHalfScreen = currentPage-(int)currentPage > 0.5;
if(isBehindHalfScreen){
if(contD == 1){
hsv.scrollTo(i2, 0);
contD = 2;
contE = 1;
}else if(contD == 2){
hsv.scrollTo(i3, 0);
contD = 3;
contE = 2;
}else if(contD == 3){
hsv.scrollTo(i4, 0);
contD = 4;
contE = 3;
}else if(contD == 4){
hsv.scrollTo(i5, 0);
contD = 4;
contE = 4;
}
}else{
if(contE == 4){
hsv.scrollTo(i4, 0);
contD = 4;
contE = 3;
}else if(contE == 3){
hsv.scrollTo(i3, 0);
contD = 3;
contE = 2;
}else if(contE == 2){
hsv.scrollTo(i2, 0);
contD = 2;
contE = 1;
}else if(contE == 1){
hsv.scrollTo(i1, 0);
contD = 1;
contE = 1;
}
}
}
return false;
}
});
From ANDROID HORIZONTALSCROLLVIEW WITH CENTER LOCK,
You shout try using custom HorizontalListView
HorizontalListView in XML
HorizontalListView.java
Activity Class
In Adapter
Hope this will help you.
Thanks, Girish