Search code examples
androidandroid-fragmentsandroid-viewpagerswipegesture

Vertical Swipe Issue in View Pager


I am using view pager and the vertical swipe is not working, I searched a lot but to no avail I find this solution but this isn't working properly too, the solution is to block the click listener on the text view link this

details.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                details.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

but then i can't swipe the view pages on the text view, i then tried to detect the gesture using gesture detector on my fragment and on left/right swipe i change the fragment with the animation, this trick works fine but the problem is the look of animation isn't as the view pager gives. I did find the solution and i am sharing it to help other better answers is appreciated too.


Solution

  • Here is what i did and hope it help you guys, thanks if it doesn't let me know and if you have any better idea please share so that we can improve.

    // write this code in activity or fragmentActivity oncreate note that i am passing the data in internt for my use please skip that if u dont want

    Intent newsFrag = new Intent(mContext, PageViewActivity.class);
    newsFrag.putExtra("xyz", getArguments().getString("xyz"));
    newsFrag.putExtra("abc", abc);
    startActivity(newsFrag); 
    

    in view pager activity i did this

    import java.util.ArrayList;
    import java.util.List;
    
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.ViewPager;
    
    public class PageViewActivity extends FragmentActivity {
    
    MyPageAdapter pageAdapter;
    List<Fragment> fragments;
    RSSFeed feed;
    String xyz;
    int pos;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_page_view);
        xyz= getIntent().getExtras().getString("xyx");
        abc = getIntent().getExtras().getInt("abc");
    
        feed = LocalDatabaseManager.getInstance(this).fetchOnCategory(xyz);
    
        fragments = getFragments();
    
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
    
        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
    
        pager.setAdapter(pageAdapter);
        pager.setCurrentItem(pos - 1);
    }
    
    private List<Fragment> getFragments() {
    
        List<Fragment> fList = new ArrayList<Fragment>();
        for (int i = 0; i < feed.getItemCount(); i++) {
            MyFragmentNews myfrag = new MyFragmentNews();
            Bundle bundle = new Bundle();
            bundle.putInt("abc", abc- 1);
            bundle.putString("xyz", xyz);
            myfrag.setArguments(bundle);
    
            myfrag.newInstance(feed.getItem(i));
            fList.add(myfrag);
        }
        return fList;
      }
    }
    

    this is how i make fragments

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTransaction;
    import android.text.Html;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.FrameLayout;
    import android.widget.ImageButton;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    
    public class MyFragmentNews extends Fragment {
    RSSItem rssItem;
    ImageButton bookmark;
    private RelativeLayout progressLoader;
    private FrameLayout theRoot;
    
    public MyFragmentNews newInstance(RSSItem newsItem) {
        MyFragmentNews f = new MyFragmentNews();
        rssItem = newsItem;
        return f;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState) {
    
        View view = inflater.inflate(R.layout.news_details, container, false);
    
        TextView heading = (TextView) view.findViewById(R.id.newHeading);
        heading.setText(AppUtils.cleanSymbols(Html.fromHtml(rssItem.getTitle()).toString()));
    
    
        return view;
    }
    

    }

    and this is the link that helped me http://www.javacodegeeks.com/2013/04/android-tutorial-using-the-viewpager.html

    if someone what to do it with gesture dector i can post that code too, hope it helps thanks