其他分享
首页 > 其他分享> > Viewpager Fragement内部的Android Listview-无滚动

Viewpager Fragement内部的Android Listview-无滚动

作者:互联网

我试图在Viewpager中创建一个ListView.
ViewPager正在使用Fragements.
所以ListView在Fragement内部.
我的Listview没有滚动,尽管在Listview中onitmclicked和ontouch被调用.

我的密码

<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;
        }
    });

适配器代码

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;
    }
}

更新:1

我扩展了Viewpager并在xml和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);
    }
}
}

解决方法:

将您的ListView的高度设置为wrap_content

android:layout_height = "wrap_content"

固定高度时,它不会滚动.

编辑

同时删除此部分:

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;
    }
});

标签:android-fragments,android-viewpager,listview,android-listview,android
来源: https://codeday.me/bug/20191120/2042692.html