【问题标题】:Android Listview inside Viewpager Fragement- No scrollViewpager Fragment中的Android Listview-不滚动
【发布时间】:2015-08-05 16:53:52
【问题描述】:

我正在尝试在 Viewpager 中创建一个 ListView。 ViewPager 正在使用片段。 所以 ListView 在 Fragment 里面。 我的 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);
    }
}
}

【问题讨论】:

  • 如果需要,我也可以发布更多代码。

标签: android listview android-fragments android-listview android-viewpager


【解决方案1】:

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

【讨论】:

  • 我应该在哪个布局中更改这个?
  • 我改了还是不滚动
  • 它对我有用,但什么是 lvalltourney?如果它是视图寻呼机或列表视图,您可能不想更改触摸事件。
  • lvalltourney 是一个 ListView。删除 ontouch 事件也不会改变任何事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多