Adding Viewpager as title to listView

I am trying to program an action that shows the viewer with some images and a list below with some data .. but my problem is that I can only scroll the list on the last part of the screen, which I wanted to make the viewpager scroll through the list, so I thought about to put it in the header. here is my xml file

<TableLayout 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=".MainActivity" > <com.devsmart.android.ui.HorizontalListView android:id="@+id/sectionsList" android:layout_width="wrap_content" android:layout_height="25dp" android:background="@drawable/menu_bg" /> <include layout="@layout/main_screen_components" /> <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 

and I write it in my class

 @Override protected void onPostExecute(List<News> result) { Utils.pagerNews(result); Utils.listNews(result); ImagePagerAdapter pAdapter = new ImagePagerAdapter( appManager.getPagerNews()); pager.setAdapter(pAdapter); View headerView = getLayoutInflater().inflate(R.layout.main_screen_components , null , false); NewsListAdapter adapter = new NewsListAdapter(getBaseContext(), appManager.getListNews()); listView.addHeaderView(headerView); listView.setAdapter(adapter); progress.dismiss(); } 

when I run this code, it gives me a duplicated pager. fixed with data and empty as the title of my list. When I delete, including my pager, it crashes when I try to install the adapter for the pager .. any idea?

+6
source share
2 answers

just set viewPager onTouchListener, for example:

 viewPager.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { PointF downP = new PointF(); PointF curP = new PointF(); int act = event.getAction(); if(act == MotionEvent.ACTION_DOWN || act == MotionEvent.ACTION_MOVE || act == MotionEvent.ACTION_UP){ ((ViewGroup) v).requestDisallowInterceptTouchEvent(true); if (downP.x == curP.x && downP.y == curP.y) { return false; } } return false; } 
+20
source

Use the following custom viewpager class:

 public class CustomViewPager extends ViewPager { boolean xScored = false, yScored = false; public CustomViewPager(Context context) { super(context); } public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTouchEvent(MotionEvent ev) { mGestureDetector.onTouchEvent(ev); if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) { xScored = false; yScored = false; requestDisallowInterceptTouchEvent(false); } return super.onTouchEvent(ev); } GestureDetector.SimpleOnGestureListener mOnGestureListener = new GestureDetector.SimpleOnGestureListener() { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if (!yScored && Math.abs(distanceX) > Math.abs(distanceY)) { xScored = true; yScored = false; } if (xScored) { requestDisallowInterceptTouchEvent(true); } else if (!xScored && Math.abs(distanceY) > Math.abs(distanceX)) { xScored = false; yScored = true; requestDisallowInterceptTouchEvent(false); } return true; } }; GestureDetector mGestureDetector = new GestureDetector(getContext(), mOnGestureListener); } 
0
source

Source: https://habr.com/ru/post/947211/


All Articles