2

我有一个布局,其中包括ViewPager最初正确布局的布局,但是如果我在设备重建ViewPager折叠时旋转设备。我已经尝试过使用 RelativeLayout 和 LinearLayout 两种情况下的结果相同。这是我的线性布局版本:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fullPlayer"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/AppTheme.Player"
    android:clickable="true"
    android:animateLayoutChanges="true"
    tools:context="com.xxx.music.fragment.PlayerFragment">

    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/playerQueueList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize"
            style="@style/AppTheme.Queue"
            />

        <RelativeLayout
            style="@style/AppTheme.ActionBar.Transparent"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">

            <ImageButton android:id="@+id/playerToggle"
                style="@style/Widget.AppCompat.Button.Borderless"
                app:srcCompat="@drawable/ic_arrow_back_white_24dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />

            <ImageButton
                android:id="@+id/playerMenu"
                style="@style/Widget.AppCompat.Button.Borderless"
                app:srcCompat="@drawable/ic_more_vert_white_24dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"/>

            <TextView
                android:id="@+id/playerTitle"
                style="@style/TextAppearance.AppCompat.Title.Inverse"
                android:visibility="invisible"
                android:layout_toRightOf="@id/playerToggle"
                android:layout_toLeftOf="@id/playerMenu"
                android:layout_centerVertical="true"
                android:text="@string/playerQueue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </RelativeLayout>
    </RelativeLayout>

    <LinearLayout android:id="@+id/songInfoHeader"
        android:orientation="vertical"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="@dimen/default_gap"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView android:id="@+id/songTitleView"
            style="@style/AppTheme.SongTitle"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <TextView android:id="@+id/songArtistView"
            style="@style/AppTheme.SongArtist"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView android:id="@+id/songAlbumView"
            style="@style/AppTheme.SongAlbum"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/default_gap"
        android:paddingBottom="@dimen/default_gap"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/songPositionView"
            style="@style/AppTheme.SongAlbum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Space
            android:layout_width="@dimen/default_gap"
            android:layout_height="wrap_content" />

        <SeekBar android:id="@+id/songProgressView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:indeterminate="false"
            android:indeterminateBehavior="repeat"
            android:layout_gravity="fill_horizontal"/>

        <Space
            android:layout_width="@dimen/default_gap"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/songDurationView"
            style="@style/AppTheme.SongAlbum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout android:id="@+id/playerFooter"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_marginTop="@dimen/default_gap"
        android:layout_marginBottom="@dimen/default_gap"
        android:gravity="center_horizontal">
        <ImageButton android:id="@+id/playQueueButton"
            android:scaleType="fitXY"
            android:layout_height="24dp"
            android:layout_width="24dp"
            android:layout_gravity="center_vertical"
            style="@style/AppTheme.Button.Flat.Inverse"
            app:srcCompat="@drawable/ic_queue_music_white_24dp"/>

        <Space
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

        <ImageButton android:id="@+id/prevTrackButton"
            android:scaleType="fitXY"
            android:layout_height="48dp"
            android:layout_width="48dp"
            android:layout_gravity="center_vertical"
            style="@style/AppTheme.Button.Flat.Inverse"
            app:srcCompat="@drawable/ic_skip_previous_white_24dp"/>

        <Space
            android:layout_width="20dp"
            android:layout_height="match_parent" />

        <ImageButton android:id="@+id/playButton"
            android:scaleType="fitXY"
            android:layout_height="72dp"
            android:layout_width="72dp"
            android:layout_gravity="center_vertical"
            style="@style/AppTheme.Button.Flat.Inverse"
            app:srcCompat="@drawable/ic_play_circle_outline_white_24px"/>

        <Space
            android:layout_width="20dp"
            android:layout_height="match_parent" />

        <ImageButton android:id="@+id/nextTrackButton"
            android:scaleType="fitXY"
            android:layout_height="48dp"
            android:layout_width="48dp"
            android:layout_gravity="center_vertical"
            style="@style/AppTheme.Button.Flat.Inverse"
            app:srcCompat="@drawable/ic_skip_next_white_24dp"/>

        <Space
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

        <ImageButton android:id="@+id/shuffleTrackButton"
            android:scaleType="fitXY"
            android:layout_height="24dp"
            android:layout_width="24dp"
            android:layout_gravity="center_vertical"
            style="@style/AppTheme.Button.Flat.Inverse"
            app:srcCompat="@drawable/ic_shuffle_white_24dp"/>
    </LinearLayout>


</LinearLayout>

我有一个使用 RelativeLayout 作为 top 的版本ViewGroup,但它的行为完全相同。

我已经尝试了几种解决方法。一种是在创建视图后使用此方法:

protected void requestLayoutBugFix() {
    // this is here because bottom sheet library has a bug which will
    // cause layouts to not update when they are complicated
    final View v = getView();
    if( v != null ) {
        Ln.i("Request Layout to fix bugs");
        v.requestLayout();
    }
}

但这似乎并不影响它。如果我折叠 BottomSheet 并展开它。它修复它。我在页面上还有另一个视图(播放队列),如果我在播放队列和专辑图像之间切换,它会正确绘制。所以我认为这与动画和BottomSheet有关。

这是我用来折叠/展开播放器的代码:

public void expandPlayer() {
    Ln.i("Expand Player");
    albumPager.setVisibility(View.VISIBLE);
    playerQueueList.setVisibility(View.GONE);
    playerUI.setHideable(false);
    playerUI.setState(BottomSheetBehavior.STATE_EXPANDED);
    fullPlayer.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in));
    miniPlayer.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out));
    playerQueueVisible = false;
}

public void collapsePlayer() {
    Ln.i("Collapse Player");
    playerUI.setHideable(false);
    playerUI.setState(BottomSheetBehavior.STATE_COLLAPSED);
    fullPlayer.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out));
    miniPlayer.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in));
}

用于 PageAdapter 的代码:

private class AlbumPageAdapter extends FragmentStatePagerAdapter {

    List<Track> tracks;

    public AlbumPageAdapter(FragmentManager fm) {
        super(fm);
        tracks = new ArrayList<Track>();
        refresh();
    }

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

    @Override
    public Fragment getItem(int position) {
        Track track = tracks.get(position);
        return ImageSetFragment.newInstance(track);
    }

    public void refresh() {
        tracks.clear();
        tracks.addAll( player.getQueue() );
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        // refresh all fragments when data set changed
        return PagerAdapter.POSITION_NONE;
    }
}

图像集片段:

public class ImageSetFragment extends EnhancedRoboFragment {

private static final String IMAGE_SET = "IMAGE_SET";

private ImageSet imageSet;
private ImageView mImageView;

public ImageSetFragment() {}

public static ImageSetFragment newInstance(ImageSet imageSet) {
    final ImageSetFragment f = new ImageSetFragment();
    final Bundle args = new Bundle();
    args.putStringArray(IMAGE_SET, new String[] { imageSet.getImageLarge().toString(), imageSet.getImageNormal().toString(), imageSet.getImageMedium().toString() } );
    f.setArguments(args);
    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    imageSet = getArguments() != null ? new SimpleImageSet( getArguments().getStringArray(IMAGE_SET) ) : null;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.player_image, container, false);
    mImageView = (ImageView) v.findViewById(R.id.imageView);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Rect insets = new Rect( 25, 25, 25, 25);
    LayoutUtil.loadImage( getActivity(), mImageView, new BlurredBgHDTransformation(mImageView.getContext(), insets), imageSet);

    mImageView.setPadding(3, 3, 3, 3);
}

}

更新: 我还尝试用 ImageView 替换 ViewPager。未绘制 ImageView 的行为相同。它实际上有点糟糕,因为 ViewPager 渲染,但只渲染到标题的高度,所以你看到 ViewPager 的顶部,但在那之后什么也没有。当我用 ImageView 替换它时,当我旋转手机时,它没有呈现 ImageView 的任何部分。

4

0 回答 0