2016-03-30 17 views
0

ViewPagerの問題があります。ScrollViewです。 LinearLayoutがルートレイアウトである場合、正しく動作するアダプタを作成しました。しかし、ScrollViewに切り替えると、画像は表示されません。 ScrollViewが必要です。表示する要素がいくつかあり、スクロール可能でなければなりません。私のレイアウトはactivity_exercises_preview.xmlです。ScrollView内のViewPager画像スライダー

レイアウト、それは動作しません:

<ScrollView 
    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="fill_parent" 
    android:padding="5dp" 
    android:background="@color/colorBackground"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:cardCornerRadius="0dp" 
      android:id="@+id/cv" 
      > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:minHeight="200dp"> 

       <android.support.v4.view.ViewPager 
        android:id="@+id/viewPager" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

       </android.support.v4.view.ViewPager> 

      </LinearLayout> 

     <android.support.v7.widget.CardView...> 

     <android.support.v7.widget.CardView...> 

     <android.support.v7.widget.CardView...> 

    </LinearLayout> 

</ScrollView> 

enter image description here

作品のレイアウト、しかし、ScrollViewなし:

<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="fill_parent" 
    android:padding="5dp" 
    android:background="@color/colorBackground"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:cardCornerRadius="0dp" 
      android:id="@+id/cv" 
      > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:minHeight="200dp"> 

       <android.support.v4.view.ViewPager 
        android:id="@+id/viewPager" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

       </android.support.v4.view.ViewPager> 

      </LinearLayout> 

     <android.support.v7.widget.CardView...> 

     <android.support.v7.widget.CardView...> 

     <android.support.v7.widget.CardView...> 

    </LinearLayout> 

</LinearLayout> 

enter image description here

マイアダプタ:

public class CustomAdapter extends PagerAdapter{ 

    Context context; 
    int[] imageId = {R.drawable.icon, R.drawable.gwiazdki, R.drawable.arnoldki_3}; 

    public CustomAdapter(Context context){ 
     this.context = context; 
    } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     // TODO Auto-generated method stub 

     LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 

     View viewItem = inflater.inflate(R.layout.image_item, container, false); 
     ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView); 
     imageView.setImageResource(imageId[position]); 
     ((ViewPager)container).addView(viewItem); 

     return viewItem; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return imageId.length; 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     // TODO Auto-generated method stub 

     return view == ((View)object); 
    } 


    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     // TODO Auto-generated method stub 
     ((ViewPager) container).removeView((View) object); 
    } 
} 

    Activity: 



public class ExercisesPreview extends AppCompatActivity { 
     List<Exercise> exercises; 
     Exercise exercise; 
     ViewPager viewPager; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_exercises_preview); 

      viewPager = (ViewPager) findViewById(R.id.viewPager); 
      PagerAdapter adapter = new CustomAdapter(ExercisesPreview.this); 
      viewPager.setAdapter(adapter); 
      viewPager.setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View v, MotionEvent event) { 
        viewPager.getParent().requestDisallowInterceptTouchEvent(true); 
        return false; 
       } 
      }); 

     } 
    (...) 
    } 

image_item.xml:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 

任意のアイデア?

+0

私はあなたのような問題があったが、代を処理し、あなたがしたいscrollview内部メモviewPagerとしてpropperly –

+0

を動作しません、私のカスタムnestedScrollViewによって解決しますレイアウトファイルを表示しますか?私は 'NestedScrollView'でも試しましたが、残念なことに運がありません。 – user3448282

+1

ありがとうございました。私のレイアウトがあなたに投稿する他の方法があれば、それを削除するよう教えてもらえませんでした。 –

答えて

1

使用あなたのscrollView内でこのカスタムViewPager:

public class WrapContentHeightViewPager extends ViewPager { 
public WrapContentHeightViewPager(Context context) { 
    super(context); 
} 

public WrapContentHeightViewPager(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int height = 0; 
    for (int i = 0; i < getChildCount(); i++) { 
     View child = getChildAt(i); 
     child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
     int h = child.getMeasuredHeight(); 
     if (h > height) height = h; 
    } 
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
}} 
+0

うわー!それは完全に動作します!ありがとう、ブライアン! – user3448282