Reputation: 1152
i have been searching solution for this problem since two days but not succeeded yet.I have taken View pager to show details of a List view in my application, When swiped shows the details of next list item. I have the Requirement of Horizontal List view inside view pager single item.Now My problem here is When i want to swipe this Horizontal List view entire View pager item is getting Swiped, i also handled On touch event for my Horizontal List view to disallow the swipe action of View Pager but still View pager is getting swiped.Please help me, below is my code
CustomViewPager.Java
import com.devpoint.adapter.HorizontalListView;
import android.content.Context;
import android.graphics.Rect;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class CustomViewPager extends ViewPager {
private boolean scrollDisable = false;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(scrollDisable) {
return false;
} else {
return super.onInterceptTouchEvent(ev);
}
}
public void disableScroll() {
scrollDisable = true;
}
public void enableScroll() {
scrollDisable = false;
}
}
And it is my single item of a View Pager Item
details_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<ScrollView
android:id="@+id/scrollviewid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp" >
------------------------
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/padding_medium"
android:background="#FFFFFF"
android:clickable="true"
android:orientation="vertical"
android:padding="35dp"
card_view:cardCornerRadius="@dimen/padding_radius" >
<TableRow
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="#FFFFFF"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<com.devpoint.adapter.HorizontalListView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:columnWidth="60dp"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</TableRow>
</android.support.v7.widget.CardView>
</ScrollView>
</RelativeLayout>
and i handled touch item for Horizontal Listview Inside Adapter as
cardview.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0,
MotionEvent ev) {
switch(ev.getAction()) {
case MotionEvent.ACTION_DOWN:
DetailsViewpagerFragment.viewPager.disableScroll();
//enable swiping
scrollview.setEnabled(false);
break;
case MotionEvent.ACTION_MOVE:
DetailsViewpagerFragment.viewPager.disableScroll();
scrollview.setEnabled(false);
break;
case MotionEvent.ACTION_UP:
DetailsViewpagerFragment.viewPager.enableScroll();
scrollview.setEnabled(true);
break;
}
return false;
}
});
Note: I am Using Fragments in My Application.Thank You
Any help would be very very very thankfull....
Upvotes: 1
Views: 1946
Reputation: 460
I face the same problems as yours. I hope is not too late to answer your question now. The way I solve this:
// Declare Variables
HorizontalListView listView;
ViewPager viewPager;
ScrollView scrollListView;
We need below variables to fix this issues. We can call viewPager in Fragment.
listView = (HorizontalListView) findViewById(R.id.your_list_view);
viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
scrollListView = (ScrollView) swipeView.findViewById(R.id.your_scrollview_layout_id);
Now implement setOnTouchListener:
listView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
// disable swiping of ViewPager
// disable scrolling of ScrollView
viewPager.requestDisallowInterceptTouchEvent(true);
scrollListView.requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
Note**:
(1) This method doesn't need to customise ViewPager class.
(2) Disable ScrollView give me a better scrolling performance in HorizontalListView.
(3) Initially I Implements setOnTouchListener to parent layout of HorizontalListView (like what you did), but I doesn't get the desired result. After HorizontalListView implements setOnTouchListener, it works well.
Hopefully it's helps others too. ^^v
Upvotes: 2
Reputation: 427
This is the code for nonSwipeable viewpager... hopefully it will work for you
public class NonSwipeableViewPager extends ViewPager {
public NonSwipeableViewPager(Context context) {
super(context);
}
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
}
Upvotes: 0