Android Listview inside Viewpager Fragement- No scroll
Asked Answered
C

1

3

I am trying to create a ListView inside a Viewpager. ViewPager is using Fragements. So ListView is inside the Fragement. My Listview is not scrolling, although in Listview onitmclicked and ontouch gets called.

My Code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app1="http://schemas.android.com/apk/res/com.bookthefield.user"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/tabHost"
    android:focusableInTouchMode="false" />

<utilities.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="@color/white"
    app1:pstsIndicatorColor="@color/available"
    app1:pstsShouldExpand="true" >
</utilities.PagerSlidingTabStrip>

</RelativeLayout>




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d8d7dd" >

<ListView
    android:id="@+id/lvalltourney"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:divider="#d8d7dd"
    android:dividerHeight="10dp" >
</ListView>

</RelativeLayout>



  lvalltourney.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            lvalltourney.getParent().requestDisallowInterceptTouchEvent(
                    true);

            int action = event.getActionMasked();

            switch (action) {
            case MotionEvent.ACTION_UP:
                lvalltourney.getParent()
                        .requestDisallowInterceptTouchEvent(false);
                break;
            }

            return false;
        }
    });

Adapter Code

class tourneyadapter extends BaseAdapter {
    private Context mContext;
    private final ArrayList<tourney> tourneylist;

    public tourneyadapter(Context c, ArrayList<tourney> tourneylist) {
        mContext = c;

        this.tourneylist = tourneylist;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return tourneylist.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub

        return 0;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid; //
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.tourneylistingitem, null);

        } else {
            grid = (View) convertView;
        }
        final tourney temp = tourneylist.get(position);
        TextView tvtourneyname = (TextView) grid
                .findViewById(R.id.tvtourneyname);

        tvtourneyname.setText(temp.getName());
        TextView tvlocationname = (TextView) grid
                .findViewById(R.id.tvlocationtourney);
        tvlocationname.setText(temp.getAddress());
        TextView tvdatetourney = (TextView) grid
                .findViewById(R.id.tvdatetourney);
        tvdatetourney.setText(temp.getDate());
        try {
            TextView tvcost = (TextView) grid
                    .findViewById(R.id.tventeryfee);
            tvcost.setText(temp.getEntryfee() + " ");
            ImageView imagesport = (ImageView) grid
                    .findViewById(R.id.imagesport);
            imagesport.setImageResource(temp.getDrawable());
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        return grid;
    }
}

UPDATE:1

I extended my Viewpager and used it in xml and javacode

public class MyViewPager extends ViewPager {
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;

public MyViewPager(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 SimpleOnGestureListener {
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {
        return Math.abs(distanceY) > Math.abs(distanceX);
    }
}
}
Conduction answered 23/5, 2015 at 20:54 Comment(1)
I can also post more code if requested.Conduction
M
3

Set the height of your ListView to wrap_content

android:layout_height = "wrap_content"

When you fix the height, it won't scroll.

EDIT

Also remove this section:

lvalltourney.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        lvalltourney.getParent().requestDisallowInterceptTouchEvent(
                true);

        int action = event.getActionMasked();

        switch (action) {
        case MotionEvent.ACTION_UP:
            lvalltourney.getParent()
                    .requestDisallowInterceptTouchEvent(false);
            break;
        }

        return false;
    }
});
Micrography answered 23/5, 2015 at 20:59 Comment(7)
In which layout should I change this?Conduction
I changed it still does not scrollConduction
It works for me, but still what is lvalltourney? If it is either view pager or the list view you might not want to change the on touch events.Micrography
lvalltourney is a ListView.Also removing the ontouch events does not change anythingConduction
Take a look at this: #2646528Micrography
I used that still no scrollConduction
@Conduction Have you got the solution.? I have the same issue. Please help me if you have solution . ThanksHaslet

© 2022 - 2024 — McMap. All rights reserved.