我正在尝试在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);
}
}
}
答案 0 :(得分:3)
将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;
}
});