gpt4 book ai didi

java - ViewPager ScrollView 不起作用

转载 作者:行者123 更新时间:2023-12-02 13:01:38 25 4
gpt4 key购买 nike

我在 ScrollView 中实现了一个viewpager,但是每当它到达屏幕的底部边缘时,viewpager就会变得畸形,从而导致 ScrollView 无法工作并且viewpager底部的元素不会显示。这是我的代码:

1. Main Acitivity

public class MainActivity extends AppCompatActivity {

ViewPager viewPager;
ViewPager viewPager2;
PagerAdapter adapter2;
private static int currentPage = 0;
private int[] IMAGES = {R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four};
private ArrayList<Integer> IMAGESArray = new ArrayList<Integer>();
private ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.landing_page);
scrollView = (ScrollView) findViewById(R.id.scroll);
viewPager2 = (ViewPager) findViewById(R.id.pager3);
// Pass results to ViewPagerAdapter Class
adapter2 = new FirstViewPagerAdapter(TestActivity.this);
// Binds the Adapter to the ViewPager
viewPager2.setAdapter(adapter2);
init();
}

private void init() {
for(int i=0;i<IMAGES.length;i++)
IMAGESArray.add(IMAGES[i]);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new SecondViewPagerAdapter(TestActivity.this, IMAGESArray));
CircleIndicator indicator = (CircleIndicator) findViewById(R.id.indicator);
indicator.setViewPager(viewPager);
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == IMAGES.length) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 2500, 2500);
}
}


2. The first view pager adapter

public class FirstViewPagerAdapter extends PagerAdapter {

private Context mContext;
private ViewPager pager=null;
private int images [] = {R.drawable.whiskey, R.drawable.vodka, R.drawable.whiskey, R.drawable.vodka, R.drawable.whiskey};
public FirstViewPagerAdapter(Context context) {
mContext = context;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

LayoutInflater inflater = LayoutInflater.from(mContext);
View itemView = inflater.inflate(R.layout.page, container, false);
ImageView image1 = (ImageView)itemView.findViewById(R.id.image);
image1.setImageResource(images[position]);

((ViewPager) container).addView(itemView);
return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView((View)object);
}

@Override
public int getCount() {
return(5);
}


@Override
public boolean isViewFromObject(View view, Object object) {
return(view == object);
}
}


3. The second view pager adapter

public class SecondViewPagerAdapter extends PagerAdapter {

private ArrayList<Integer> images;
private LayoutInflater inflater;
private Context context;

public SecondViewPagerAdapter(Context context, ArrayList<Integer> images) {
this.context = context;
this.images=images;
inflater = LayoutInflater.from(context);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}

@Override
public int getCount() {
return images.size();
}

@Override
public Object instantiateItem(ViewGroup view, int position) {
View myImageLayout = inflater.inflate(R.layout.view_pager_item, view, false);
ImageView myImage = (ImageView) myImageLayout.findViewById(R.id.flag);
myImage.setImageResource(images.get(position));
view.addView(myImageLayout, 0);
return myImageLayout;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}}


4. landing_page xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="match_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
android:background="#cccccc"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:vpi="http://schemas.android.com/tools"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:background="#cccccc"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true" />
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="200dp"
android:layout_gravity="center"
android:gravity="center"
app:ci_drawable="@drawable/myindicator"
vpi:snap="true"/>
</RelativeLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:src="@drawable/hdspiritsz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="7dp"/>
<ImageView android:layout_width="match_parent"
android:src="@drawable/hdextrasz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="7dp"
android:layout_marginLeft="4dp"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:layout_width="match_parent"
android:src="@drawable/hdspiritsz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="7dp"/>
<ImageView android:layout_width="match_parent"
android:src="@drawable/hdextrasz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="7dp"
android:layout_marginLeft="4dp"/>
</LinearLayout>
<RelativeLayout android:layout_width="match_parent"
android:background="#cccccc"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/pager3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:layout_width="match_parent"
android:src="@drawable/hdspiritsz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="7dp"/>
<ImageView android:layout_width="match_parent"
android:src="@drawable/hdextrasz"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginRight="7dp"
android:layout_marginLeft="4dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>

最佳答案

将 android:fillViewport="true"添加到 ScrollView 元素。

关于java - ViewPager ScrollView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44279482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com