2016-11-05 91 views
1

我試圖動態設置ViewPager的高度,以使ViewPager的每個Fragment都有它自己的高度。我根據我對解決方案代碼由czaku這裏找到:Dynamically set ViewPager height動態設置ViewPager的高度與片段

activity_artwork_page.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" > 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_artwork_page" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     tools:context="com.example.andrea.chatbeacon.ArtworkPageActivity"> 

     <android.support.v4.view.ViewPager 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="4000dp" /> 

     <fragment 
      class="com.example.andrea.chatbeacon.ChatFragment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/chat_fragment" 
      android:layout_marginTop="40dp" 
      tools:layout="@layout/fragment_chat" /> 
    </LinearLayout> 

</ScrollView> 

ArtworkPageActivity.java

public class ArtworkPageActivity extends AppCompatActivity { 

    private ViewPager mPager; 

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

     mPager = (ViewPager) findViewById(R.id.pager); 
     PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); 
     mPager.setAdapter(mPagerAdapter); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     ViewTreeObserver viewTreeObserver = mPager.getViewTreeObserver(); 
     viewTreeObserver 
       .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 

        @Override 
        public void onGlobalLayout() { 

         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
           LinearLayout.LayoutParams.WRAP_CONTENT, 
           LinearLayout.LayoutParams.WRAP_CONTENT); 

         LinearLayout artwork_slide_layout = (LinearLayout) findViewById(R.id.artwork_slide_layout); 
         int viewPagerWidth = mPager.getWidth(); 
         int viewPagerHeight = artwork_slide_layout.getHeight(); 

         layoutParams.width = viewPagerWidth; 
         layoutParams.height = viewPagerHeight; 

         mPager.setLayoutParams(layoutParams); 
         mPager.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
        } 
       }); 
    } 

    @Override 
    public void onBackPressed() { 
     if (mPager.getCurrentItem() == 0) { 
      // If the user is currently looking at the first step, allow the system to handle the 
      // Back button. This calls finish() on this activity and pops the back stack 
      super.onBackPressed(); 
     } else { 
      // Otherwise, select the previous step 
      mPager.setCurrentItem(mPager.getCurrentItem() - 1); 
     } 
    } 

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 

     public ScreenSlidePagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      Fragment fragment = new ArtworkSlideFragment(); 
      Bundle args = new Bundle(); 
      args.putInt("position", position); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public int getCount() { 
      int id = getIntent().getIntExtra("id", 0); 
      return Variables.artworks[id].getTitles().length; 
     } 
    } 
} 

fragment_artwork_slide.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/artwork_slide_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".ArtworkSlideFragment"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/artwork_image" 
     android:contentDescription="@string/artwork_image_description" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/artwork_title" 
     android:textColor="@android:color/black" 
     android:textSize="18sp" 
     android:layout_marginTop="@dimen/activity_vertical_margin" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/artwork_text" 
     android:textColor="@android:color/black" 
     android:textSize="15sp" 
     android:layout_marginTop="10dp" /> 

</LinearLayout> 

ArtworkSlideFragment.java

public class ArtworkSlideFragment extends Fragment { 

    public ArtworkSlideFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     Bundle args = getArguments(); 
     int id = getActivity().getIntent().getIntExtra("id", 0); 
     int position = args.getInt("position", 0); 
     View view = inflater.inflate(R.layout.fragment_artwork_slide, container, false); 
     ImageView image = (ImageView) view.findViewById(R.id.artwork_image); 
     TextView title = (TextView) view.findViewById(R.id.artwork_title); 
     TextView text = (TextView) view.findViewById(R.id.artwork_text); 

     Glide.with(getActivity().getApplicationContext()).load(Variables.artworks[id].getImages()[position]).into(image); 
     title.setText(Variables.artworks[id].getTitles()[position]); 
     text.setText(Variables.artworks[id].getTexts()[position]); 

     return view; 
    } 
} 

我試圖設置

viewPagerHeight = artwork_slide_layout.getHeight(); 

,但它似乎沒有工作。我如何設置viewPagerHeight

+0

height returns by artwork_slide_layout.getHeight(); 0或某些值 –

+0

這是一些值,在第一個片段中打開它是12000. – Foursnakes

+0

您正在使用哪種設備和操作系統? –

回答