没有滚动的Andr​​oid里面的列表视图

没有滚动的Andr​​oid里面的列表视图

本文介绍了Viewpager Fragement-没有滚动的Andr​​oid里面的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Viewpager内的ListView。
ViewPager使用Fragements。
所以ListView控件是Fragement内。
我的列表视图不滚动,虽然在列表视图onitmclicked和ontouch被调用。

我的code

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:APP1 =htt​​p://schemas.android.com/apk/res/com.bookthefield.user
机器人:layout_width =match_parent
机器人:layout_height =match_parent>< android.support.v4.view.ViewPager
    机器人:ID =@ + ID /寻呼机
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_below =@ + ID / tabHost
    机器人:focusableInTouchMode =FALSE/>< utilities.PagerSlidingTabStrip
    机器人:ID =@ + ID /标签
    机器人:layout_width =match_parent
    机器人:layout_height =48dip
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentLeft =真
    机器人:背景=@色/白
    APP1:pstsIndicatorColor =@色/可用
    APP1:pstsShouldExpand =真正的>
< /utilities.PagerSlidingTabStrip>< / RelativeLayout的>
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#d8d7dd>< ListView控件
    机器人:ID =@ + ID / lvalltourney
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真
    机器人:layout_marginBottom =10dp
    机器人:layout_marginLeft =10dp
    机器人:layout_marginRight =10dp
    机器人:layout_marginTop =10dp
    机器人:分=#d8d7dd
    机器人:dividerHeight =10dp>
< /&的ListView GT;< / RelativeLayout的>  lvalltourney.setOnTouchListener(新View.OnTouchListener(){
        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            lvalltourney.getParent()。requestDisallowInterceptTouchEvent(
                    真正);            INT行动= event.getActionMasked();            开关(动作){
            案例MotionEvent.ACTION_UP:
                lvalltourney.getParent()
                        .requestDisallowInterceptTouchEvent(假);
                打破;
            }            返回false;
        }
    });

适配器code

 类tourneyadapter延伸BaseAdapter {
    私人语境mContext;
    私人最终的ArrayList<&锦标赛GT; tourneylist;    公共tourneyadapter(上下文C,ArrayList的<&锦标赛GT; tourneylist){
        mContext = C;        this.tourneylist = tourneylist;    }    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回tourneylist.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回null;
    }    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根        返回0;
    }    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,
            父母的ViewGroup){
        // TODO自动生成方法存根
        查看网格; //
        LayoutInflater吹气=(LayoutInflater)mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        如果(convertView == NULL){            格=新景(mContext);
            格= inflater.inflate(R.layout.tourneylistingitem,NULL);        }其他{
            格=(查看)convertView;
        }
        最终锦标赛TEMP = tourneylist.get(位置);
        TextView的tvtourneyname =(TextView的)电网
                .findViewById(R.id.tvtourneyname);        tvtourneyname.setText(temp.getName());
        TextView的tvlocationname =(TextView的)电网
                .findViewById(R.id.tvlocationtourney);
        tvlocationname.setText(temp.getAddress());
        TextView的tvdatetourney =(TextView的)电网
                .findViewById(R.id.tvdatetourney);
        tvdatetourney.setText(temp.getDate());
        尝试{
            TextView的tvcost =(TextView的)电网
                    .findViewById(R.id.tventeryfee);
            tvcost.setText(temp.getEntryfee()+);
            ImageView的imagesport =(ImageView的)电网
                    .findViewById(R.id.imagesport);
            imagesport.setImageResource(temp.getDrawable());
        }赶上(例外E1){
            // TODO自动生成catch块
            e1.printStackTrace();
        }        返回网格;
    }
}

更新:1

我伸出我的Viewpager和XML和Java code

用它

 公共类MyViewPager扩展ViewPager {
私人GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;公共MyViewPager(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    mGestureDetector =新GestureDetector(上下文,新YScrollDetector());
    setFadingEdgeLength(0);
}@覆盖
公共布尔onInterceptTouchEvent(MotionEvent EV){
    返回super.onInterceptTouchEvent(EV)
            &功放;&安培; mGestureDetector.onTouchEvent(EV);
}如果我们在X方向是滚动//返回FALSE
类YScrollDetector扩展SimpleOnGestureListener {
    @覆盖
    公共布尔onScroll(MotionEvent E1,E2 MotionEvent,
            浮distanceX,浮distanceY){
        返回Math.abs(distanceY)GT; Math.abs(distanceX);
    }
}
}


解决方案

设置你的ListView的高度WRAP_CONTENT

 的android:layout_height =WRAP_CONTENT

在你修复的高度,它不会滚动。

修改

同时删除此部分:

  lvalltourney.setOnTouchListener(新View.OnTouchListener(){
    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        lvalltourney.getParent()。requestDisallowInterceptTouchEvent(
                真正);        INT行动= event.getActionMasked();        开关(动作){
        案例MotionEvent.ACTION_UP:
            lvalltourney.getParent()
                    .requestDisallowInterceptTouchEvent(假);
            打破;
        }        返回false;
    }
});

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);
    }
}
}
解决方案

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

这篇关于Viewpager Fragement-没有滚动的Andr​​oid里面的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:46