I have a (Sherlock)FragmentActivity with 2 tabbed fragments. The left fragment is a GridView that displays pictures of an album and the right fragment consists of a ViewPager that is used to view individual pictures. From the left fragment you can scroll through the pictures and select one. Tabbing (or swiping) over to the right fragment will show the picture and because it is a ViewPager you can swipe to the preceding or the next picture.
This works great except that the FragmentActivity wants to intercept the right swipe and move back to the left tab. I would like to prevent the FragmentActivity from intercepting the swipes when I am on the right tab. If I had to disable swiping between tabs altogether it would be satisfactory. I just want the swiping to be dedicated to the current tab and not be used to move between tabs.
The following images indicate the current behavior. The right image shows what happens when I do a swipe to the right. As you can see the left tab starts to appear. I want the swipe to instead apply to the right tab only so that I can swipe between the images without the left tab appearing.
I see solutions to control swiping within a ViewPager but have yet to find a solution to control swiping between tabbed fragments.
Here is the xml for the GridView fragment and the ViewPager fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dip"
android:gravity="center"
android:horizontalSpacing="4dip"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="4dip" />
</FrameLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"/>
</LinearLayout>
Here a code summary of the ViewPager fragment:
public class FragmentFlash extends SherlockFragment {
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private ViewPager pager = null;
private int pagerPosition;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pagerPosition = 0;
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.flash, container, false);
pager = (ViewPager) v.findViewById(R.id.pager);
pager.setOnTouchListener(gestureListener);
return v;
}
class MyGestureDetector extends SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 10;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 50;
#Override
public boolean onDown(MotionEvent e) {
return true;//false; make onFling work with fragments
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
else
// right to left swipe
if(distanceX > SWIPE_MIN_DISTANCE) {
if (pagerPosition < imageUrls.length-1)
pager.setCurrentItem(++pagerPosition);
// left to right swipe
} else if (distanceX < -SWIPE_MIN_DISTANCE) {
if (pagerPosition > 0)
pager.setCurrentItem(--pagerPosition);
}
return true;
} catch (Exception e) {
// nothing
}
return false;
}
}
private class ImagePagerAdapter extends PagerAdapter {
private String[] images;
private LayoutInflater inflater;
ImagePagerAdapter(String[] images) {
this.images = images;
inflater = mContext.getLayoutInflater();
}
#Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public void finishUpdate(View container) {
}
#Override
public int getCount() {
return images.length;
}
#Override
public Object instantiateItem(View view, int position) {
final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
byte[] image = ;//get byte array from file at images[position];
if (null != image) {
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
imageView.setImageBitmap(bitmap);
}
((ViewPager) view).addView(imageLayout, 0);
return imageLayout;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
#Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View container) {
}
}
public void pagerPositionSet(int pagerPosition, String[] imageUrls) {
Log.i(Flashum.LOG_TAG, "FragmentFlash pagerPositionSet: " + pagerPosition);
if (pagerPosition >= 0)
this.pagerPosition = pagerPosition;
if (pager != null) {
pager.setAdapter(new ImagePagerAdapter(imageUrls));
pager.setCurrentItem(this.pagerPosition);
}
}
}
This is the item_pager_image.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1dip" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image" />
<ProgressBar
android:id="#+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
Ok, I finally figured this out. Laurence Dawson was on the right track but instead of applying the CustomViewPager to the fragment it needs to be applied to the FragmentActivity. You see, switching between tabs, which is managed by the activity, is also a ViewPager adaptor. Thus, xml for the activity is
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<com.Flashum.CustomViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
And the custom ViewPager is as suggested except that that the constructor enables it to cause onInterceptTouchEvent to return false. This prevents the FragmentActivity from acting on the swipe so that it can be dedicated to the fragment!
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = **false**;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
You need to override the onInterceptTouchEvent method for your ViewPager, you can do this by extending ViewPager or visit this link for a great tutorial on adding this:
http://blog.svpino.com/2011/08/disabling-pagingswiping-on-android.html
Related
I have a tablayout with viewpager and a recyclerview inside... I've override the onMeasure method... The first time I open the fragment, the fragment inside the viewpager not show, but if i change tab it appear and if i go back to the tab that doesn't show, it begin to show... Can some one help me please!!!
I Think i saw is the viewpager get visible on screen if it fit on the screen, if i need to scroll the screen to see the viewpager it begin hided until i go to another tab and back again to see the content.
I got diferent sizes of contents inside the viewpager, that's why i need to use a custom view pager and override the onMeasure...
The source code below...
THE VIEW PAGER
public class CustomPager extends ViewPager {
private int mCurrentPagePosition = 0;
public CustomPager(Context context) {
super(context);
}
public CustomPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
View child = getChildAt(mCurrentPagePosition);
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
} catch (Exception e) {
e.printStackTrace();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void reMeasureCurrentPage(int position) {
mCurrentPagePosition = position;
requestLayout();
}
}
THE SCROLLVIEW
public class CustomScrollView extends ScrollView {
private GestureDetector mGestureDetector;
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, new YScrollDetector());
setFadingEdgeLength(0);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev)
&& mGestureDetector.onTouchEvent(ev);
}
// Return false if we're scrolling in the x direction
class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
return (Math.abs(distanceY) > Math.abs(distanceX));
}
}
}
THE LAYOUT
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="br.com.ole.oleconsignado.ui.fragment.main.LastEntriesFragment">
<View
android:id="#+id/vw_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="#dimen/dimen_mdpi"
android:layout_marginLeft="#dimen/dimen_mdpi"
android:layout_marginRight="#dimen/dimen_mdpi"
android:layout_marginEnd="#dimen/dimen_mdpi"
android:layout_marginStart="#dimen/dimen_mdpi"
android:background="#color/colorGrey"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/colorLightBlue"
app:tabSelectedTextColor="#color/colorLightBlue"
android:layout_marginLeft="#dimen/dimen_mdpi"
android:layout_marginRight="#dimen/dimen_mdpi">
<android.support.design.widget.TabItem
android:id="#+id/tab_national"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_national" />
<android.support.design.widget.TabItem
android:id="#+id/tab_international"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_international" />
</android.support.design.widget.TabLayout>
<br.com.ole.oleconsignado.util.CustomScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<br.com.ole.oleconsignado.util.CustomPager
android:id="#+id/vp_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimen_mdpi" />
</br.com.ole.oleconsignado.util.CustomScrollView>
</LinearLayout>
I realy don't know why it happen... Please, help me!
Here some examples similar...
Set Viewpager height inside Scrollview in android
getchildat() method returns null for last position (3rd Fragment in my case) Fragment for WarpContent ViewPager Android
ViewPager wrap_content not working
Android - ViewPager tab not showing first time
Sorry I couldn't understan your use-case completely but I think I can help.
Are You Trying to get any sort Dimensional-Measurements from the activity?
If Yes Then unfortunately the activities and fragments are not completely generated until onPause()
but you can try to do this-
Create a variable of your parent layout in your activity ;
RelativeLayout canvas;
canvas = (RelativeLayout) findViewById(R.id.canvas);
canvas.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
canvas.getViewTreeObserver().removeOnGlobalLayoutListener(this);
/* Code to get dimensional measurements */
}
});
Hope It Helps :)
I have a viewpager and some buttons below viewpager. I added whole layout inside scrollview but due to viewpager the scrollview is not working. I tried many custom scrollview and viewpager classes but none worked. Is there any way to do it or do I have to use something else instead of viewpager. I'm using viewpager to display images.
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
MyPagerAdapter adapter;
private ArrayList<String> alName=new ArrayList<String>();
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
alName.add("http://i.imgur.com/wre2PiR.jpg");
alName.add("http://i.imgur.com/BGGOhh1.jpg");
alName.add("http://i.imgur.com/XGCLH1M.jpg");
alName.add("http://i.imgur.com/kiPwp9I.jpg");
adapter = new MyPagerAdapter(MainActivity.this, alName);
viewPager.setAdapter(adapter);
} // onCreate ends
public class MyPagerAdapter extends PagerAdapter {
private Context mContext;
private ArrayList<String> arrayList=new ArrayList<String>();
MyPagerAdapter(Context context, ArrayList<String> jarr) {
this.mContext = context;
this.arrayList=jarr;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView view = new ImageView(container.getContext());
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(MainActivity.this).load(arrayList.get(position)).into(view);
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
Layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 4"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 6"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 7"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 8"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
You can custom the two components to solve this issue.
Custom ScrollView:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
xLast = ev.getX();
yLast = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - xLast);
yDistance += Math.abs(curY - yLast);
xLast = curX;
yLast = curY;
if(xDistance > yDistance){
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
Custom Viewpager:
public boolean dispatchTouchEvent(MotionEvent ev)
{
boolean ret = super.dispatchTouchEvent(ev);
if(ret)
{
requestDisallowInterceptTouchEvent(true);
}
return ret;
}
In this case you have set the scroll view as the parent layout , but if you want the content inside the pager to scroll create a fragment and set a scrolling layout to the fragment and set the fragment to the pager adapter , google provides a good example:
Android Developers' Reference
hope this helps
I have a GridView in which I show the images. On the click of the Image the Image expands to full screen. Everything is fine up to this. Now I want to swipe these Image. When I swipe the Image, nothing happens. It remains as it is. Can anyone help me, whats wrong in the code. Below is my code and layout files.
public class MainActivity extends Activity {
private GridView gridview;
private int j = 0;
private final GestureDetector detector = new GestureDetector(new SwipeGestureDetector());
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
// Create Array thumbs resource id's:
private int thumb[] = { R.drawable.logowhite,
R.drawable.logo_bg,
R.drawable.logowhite,
R.drawable.logo_bg,
R.drawable.logowhite,
R.drawable.logo_bg, };
private ImageView expandedImageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the variables:
gridview = (GridView) findViewById(R.id.gridView);
// Set an Adapter to the ListView
gridview.setAdapter(new ImageAdapter(this));
// Set on item click listener to the ListView
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
j = pos;
setContentView(R.layout.slide_image);
expandedImageView = (ImageView) findViewById(R.id.imageViewslides);
expandedImageView.setBackgroundResource(thumb[pos]);
}
});
}
// Create an Adapter Class extending the BaseAdapter
class ImageAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageAdapter(MainActivity activity) {
// TODO Auto-generated constructor stub
layoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// Set the count value to the total number of items in the Array
return thumb.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.grid_item, null);
}
// Initialize the views in the layout
ImageView imgView = (ImageView) listItem.findViewById(R.id.thumb);
// Set the views in the layout
imgView.setBackgroundResource(thumb[pos]);
return listItem;
}
}
private class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if(thumb.length>j) {
j++;
if(j < thumb.length){
expandedImageView.setImageResource(thumb[j]);
return true;
}
else {
j = 0;
expandedImageView.setImageResource(thumb[j]);
return true;
}
}
}
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if(j>0){
j--;
expandedImageView.setImageResource(thumb[j]);
return true;
}
else {
j = thumb.length-1;
expandedImageView.setImageResource(thumb[j]);
return true;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
}
Layout File are: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.mukesh.gridviewimageswipe.MainActivity">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="80dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
<ImageView
android:id="#+id/expanded_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:visibility="invisible" />
</FrameLayout>
</RelativeLayout>
grid_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<ImageView
android:id="#+id/thumb"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_centerHorizontal="true"
android:contentDescription="#string/app_name" />
</RelativeLayout>
slide_image.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/imageViewslide"
android:layout_width="fill_parent"
android:layout_height="420dp"
android:orientation="horizontal"
android:layout_below="#+id/textq">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageViewslides">
</ImageView>
</LinearLayout>
</LinearLayout>
I am working on images, I am going to display image in full screen, as user touches on screen then some controls will be shown. So I want to disable zooming when controls are on the screen.
Or somebody tell me how to detect the image is zoom or not?
activity_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="#+id/imgSelected"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="centerInside"
android:src="#drawable/placeholder"
android:contentDescription="#string/image" />
<LinearLayout
android:id="#+id/imageControls"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:weightSum="9"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="horizontal"
android:visibility="gone"
android:background="#drawable/image_controls">
<Button
android:id="#+id/btnSetAsBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="left|center_vertical"
android:background="#00000000"
style="#style/ImageControls"
android:text="Set"/>
<Button
android:id="#+id/btnFavorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="#00000000"
style="#style/ImageControls"
android:text="Favourite"/>
<Button
android:id="#+id/btnShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="right|center_vertical"
android:background="#00000000"
style="#style/ImageControls"
android:text="Share"/>
</LinearLayout>
</RelativeLayout>
activity_zoom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
</LinearLayout>
ImageActivity.java
public class ImageActivity extends Activity {
private int imageId;
private int categoryId;
private String imageUrl;
private ZoomView zoomView;
private ImageView imageView;
private LinearLayout main_container;
private boolean controlFlag;
private LinearLayout imageControls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zoom);
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
final View convertView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_image, null, false);
convertView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageControls = (LinearLayout) convertView.findViewById(R.id.imageControls);
controlFlag = false;
zoomView = new ZoomView(this);
imageId = getIntent().getExtras().getInt("image");
categoryId = getIntent().getExtras().getInt("category");
imageView = (ImageView) convertView.findViewById(R.id.imgSelected);
imageUrl = SplashActivity.urlList.get(categoryId).get(imageId);
Log.e(String.valueOf(categoryId),imageUrl);
Picasso
.with(this)
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.into(imageView, new Callback() {
#Override
public void onError() {
}
#Override
public void onSuccess() {
imageView.setScaleType(ScaleType.FIT_CENTER);
}
});
zoomView.addView(convertView);
main_container = (LinearLayout) findViewById(R.id.main_container);
main_container.addView(zoomView);
convertView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(controlFlag) {
imageControls.setVisibility(View.GONE);
controlFlag = false;
} else {
imageControls.setVisibility(View.VISIBLE);
controlFlag = true;
}
return false;
}
});
}
}
I am using android-zoom-view.jar library for zooming. I want to display LinearLayout (imageControls) only if image is not zoom Or disable zooming if LinearLayout (imageControls) is visible. Because ZoomView instance perform zooming on all over activity so the imageControls will also be zoom. This is not a good user friendly design.
So please help me to make this layout better.
Thanks...
In zoomview class thinking you using this https://github.com/Polidea/android-zoom-view/blob/master/src/pl/polidea/view/ZoomView.java
just add this
boolean isZoomEnable= true;
public void setIsZoomEnable(boolean value){
isZoomEnable = value;
}
#Override
public boolean dispatchTouchEvent(final MotionEvent ev) {
if(!isZoomEnable) return false;
// single touch
if (ev.getPointerCount() == 1) {
processSingleTouchEvent(ev);
}
// // double touch
if (ev.getPointerCount() == 2) {
processDoubleTouchEvent(ev);
}
// redraw
getRootView().invalidate();
invalidate();
return true;
}
Then in your code
convertView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(controlFlag) {
imageControls.setVisibility(View.GONE);
controlFlag = false;
} else {
imageControls.setVisibility(View.VISIBLE);
controlFlag = true;
}
zoomView.setIsZoomEnable(!controlFlag);
return false;
}
});
I need to add a swipe view in tab activity.In these below xml coding
I just created a six tabs namely home,blog,audio,video,more and
gallery.
The Tabs are created perfectly.But I cannot able to know how to add a swipe for that six tabs.I need
to add a right to left swipe and left to right swipe.
layout_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/bottom_bar"
android:layout_weight="0" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="#+id/tab_home"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+id/tab_video"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+id/tab_audio"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:id="#+id/tab_blog"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:id="#+id/tab_gal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:id="#+id/tab_more"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</FrameLayout>
<com.sit.gems.util.AppPromoPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
</LinearLayout>
AppPromoPager.java:
These is the AppPromopager.Here I am using the ViewPager.
package com.sit.gems.util;
import java.net.URLDecoder;
import java.util.List;
import android.content.Context;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import com.gems.android.R;
import com.sit.gems.app.AppData;
import com.sit.gems.util.ImageCache.ImageCacheParams;
public class AppPromoPager extends RelativeLayout implements
ViewPager.OnPageChangeListener {
//private PromoPageIndicator indicator;
private PromoPagerListener listener;
private Handler trackerHandler = new Handler();
private ImageFetcher mImageFetcher;
public AppPromoPager(Context context) {
super(context);
}
public AppPromoPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AppPromoPager(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void init(FragmentActivity context, List<String> promotions) {
ImageCacheParams cacheParams = new ImageCacheParams(context, AppConstants.IMAGE_CACHE_DIR);
mImageFetcher = new ImageFetcher(context, AppData.getScreenWidth(),(AppData.getScreenWidth()/2));
mImageFetcher.setLoadingImage(R.drawable.ic_launcher);
mImageFetcher.addImageCache(context.getSupportFragmentManager(), cacheParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
ViewPager pager = new ViewPager(context);
ViewPagerAdapter adapter = new ViewPagerAdapter(context, promotions);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
pager.setOnPageChangeListener(this);
addView(pager, params);
//indicator = new PromoPageIndicator(context);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.setMargins(0, 0, 0, 3);
//addView(indicator, params1);
//if (promotions.size() > 1)
// indicator.addCircleImage(promotions.size());
/*pager.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE
&& scrollView != null) {
scrollView.requestDisallowInterceptTouchEvent(true);
}
return false;
}
});*/
}
class ViewPagerAdapter extends PagerAdapter {
Context activity;
List<String> promotions;
public ViewPagerAdapter(Context context, List<String> promotions) {
this.promotions = promotions;
activity = context;
}
public int getCount() {
return promotions.size();
}
public Object instantiateItem(View collection, final int position) {
final ImageView view = new ImageView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
view.setScaleType(ScaleType.CENTER);
String imageUrl = promotions.get(position);
if (imageUrl != null && imageUrl.length() > 0) {
setImage(view, imageUrl);
}
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.selectedPromo(promotions.get(position),position);
}
});
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
//listener.selectedPosition(arg0);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageSelected(int position) {
listener.selectedPosition(position);
}
public interface PromoPagerListener {
public void selectedPromo(String offer,int postion);
public void selectedPosition(int postion);
}
public void setPromoPagerListener(PromoPagerListener listener) {
this.listener = listener;
}
private void setImage(final ImageView view, final String imageUrl) {
trackerHandler.post(new Runnable() {
#Override
public void run() {
mImageFetcher.loadImage(URLDecoder.decode(imageUrl),view);
}
});
}
}
I thought with the help of viewpager I can able to get a Swipe view.
ViewPager was added in AppPromoPager.
I added an AppPromoPager to that
layout_home.xml file.But till now I didn't get a swipe view.Anybody can help me with these.Thank you.
Better to use the Scrollable Tabs which uses a combination of Swipe view pager with the action bar tabs.
What I did is in each tabcontent, implement the swipe listener. In the tabhost, implement a swipe method.
tabcontent
layout.setOnTouchListener(new OnTouchListener() {
private PointerCoords mDownPos = new PointerCoords();
private PointerCoords mUpPos = new PointerCoords();
#Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
event.getPointerCoords(0, mDownPos);
return true;
}
case MotionEvent.ACTION_UP: {
event.getPointerCoords(0, mUpPos);
float dx = mDownPos.x - mUpPos.x;
// Check for horizontal wipe
if (Math.abs(dx) > SWIPE_MIN_DISTANCE) {
if (dx > 0) {
mParent.swipe(DIRECTION_RIGHT);
} else {
mParent.swipe(DIRECTION_LEFT);
}
return true;
}
}
}
return false;
}
});
protected void swipe(int direction) {
int current = mTabHost.getCurrentTab();
if (direction == DIRECTION_LEFT) {
int newTab = --current % mTabHost.getTabWidget().getTabCount();
mTabHost.setCurrentTab( (newTab < 0) ? mTabHost.getTabWidget().getTabCount()-1 : newTab);
}
else {
mTabHost.setCurrentTab(++current % mTabHost.getTabWidget().getTabCount());
}
}
hey you should do this by actionBar tabs + viewpager here is a tutorial which let you make what you want.
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/