使用片段动态设置ViewPager的高度

时间:2016-11-05 11:55:51

标签: android android-fragments android-viewpager

我试图动态设置ViewPager的高度,以便Fragment的每个ViewPager都有自己的高度。我的代码基于czaku在Dynamically set ViewPager height

找到的解决方案

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

1 个答案:

答案 0 :(得分:-1)

我从vabhishek找到了一个有效的解决方案:

https://github.com/vabhishek/WrapContentViewPagerDemo

即使没有vabhishek定义的CustomScrollView,这对我也适用。