我有一个viewpager和viewpager下面的一些按钮。我在scrollview中添加了整个布局,但由于viewpager,scrollview无法正常工作。我尝试了很多自定义scrollview和viewpager类,但没有一个工作。有没有办法做到这一点,或者我必须使用其他东西而不是viewpager。我正在使用viewpager来显示图像。
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
MyPagerAdapter adapter;
private ArrayList<String> alName=new ArrayList<String>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
alName.add("http://i.imgur.com/wre2PiR.jpg");
alName.add("http://i.imgur.com/BGGOhh1.jpg");
alName.add("http://i.imgur.com/XGCLH1M.jpg");
alName.add("http://i.imgur.com/kiPwp9I.jpg");
adapter = new MyPagerAdapter(MainActivity.this, alName);
viewPager.setAdapter(adapter);
} // onCreate ends
public class MyPagerAdapter extends PagerAdapter {
private Context mContext;
private ArrayList<String> arrayList=new ArrayList<String>();
MyPagerAdapter(Context context, ArrayList<String> jarr) {
this.mContext = context;
this.arrayList=jarr;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView view = new ImageView(container.getContext());
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(MainActivity.this).load(arrayList.get(position)).into(view);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 4"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 6"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 7"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 8"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
答案 0 :(得分:0)
您可以自定义这两个组件来解决此问题。
自定义ScrollView:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
xLast = ev.getX();
yLast = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - xLast);
yDistance += Math.abs(curY - yLast);
xLast = curX;
yLast = curY;
if(xDistance > yDistance){
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
自定义Viewpager:
public boolean dispatchTouchEvent(MotionEvent ev)
{
boolean ret = super.dispatchTouchEvent(ev);
if(ret)
{
requestDisallowInterceptTouchEvent(true);
}
return ret;
}
答案 1 :(得分:0)
在这种情况下,您已将滚动视图设置为父布局,但如果您希望寻呼机内的内容滚动创建片段并将滚动布局设置为片段并将片段设置为寻呼机适配器,则谷歌提供一个很好的例子:
希望这会有所帮助