Yar*_*arh 6 android android-view
我想手动处理复杂视图的旋转 ,包括恢复支撑位置和大小.在这一刻,我正在尝试这样做onLayout(更好的想法是velcome).有时它运作良好,但通常第一次轮换是错位或没有孩子的视图.
private int oldOrientation = -1;
/**
* Override method to configure the dragged view and secondView layout properly.
*/
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Log.e("mylayout", "onLayout " + df.format(new Date(Calendar.getInstance().getTimeInMillis())));
if (isInEditMode()) {
super.onLayout(changed, left, top, right, bottom);
} else {
dragView.setVisibility(INVISIBLE);
if (isDragViewAtTop() && (oldOrientation != getResources().getConfiguration().orientation || oldOrientation == -1)) {
dragView.layout(left, top, right, transformer.getOriginalHeight());
secondView.layout(left, transformer.getOriginalHeight(), right, bottom);
ViewHelper.setY(dragView, top);
ViewHelper.setY(secondView, transformer.getOriginalHeight());
ViewHelper.setX(dragView, left);
ViewHelper.setX(secondView, left);
oldOrientation = getResources().getConfiguration().orientation;
} else if (isClosedAtLeft() && (
oldOrientation != getResources().getConfiguration().orientation
|| oldOrientation == -1)) {
dragView.layout(left, top, right, transformer.getOriginalHeight());
secondView.layout(left, transformer.getOriginalHeight(), right, bottom);
ViewHelper.setY(dragView, top);
ViewHelper.setY(secondView, transformer.getOriginalHeight());
ViewHelper.setX(dragView, left);
ViewHelper.setX(secondView, left);
closeToLeft();
oldOrientation = getResources().getConfiguration().orientation;
} else if (isClosedAtRight() && (
oldOrientation != getResources().getConfiguration().orientation
|| oldOrientation == -1)) {
dragView.layout(left, top, right, transformer.getOriginalHeight());
secondView.layout(left, transformer.getOriginalHeight(), right, bottom);
ViewHelper.setY(dragView, top);
ViewHelper.setY(secondView, transformer.getOriginalHeight());
ViewHelper.setX(dragView, left);
ViewHelper.setX(secondView, left);
closeToRight();
oldOrientation = getResources().getConfiguration().orientation;
} else if ((oldOrientation != getResources().getConfiguration().orientation
|| oldOrientation == -1)) {
dragView.layout(left, top, right, transformer.getOriginalHeight());
secondView.layout(left, transformer.getOriginalHeight(), right, bottom);
ViewHelper.setY(dragView, top);
ViewHelper.setY(secondView, transformer.getOriginalHeight());
ViewHelper.setX(dragView, left);
ViewHelper.setX(secondView, left);
smoothSlideTo(SLIDE_BOTTOM);
oldOrientation = getResources().getConfiguration().orientation;
}
dragView.setVisibility(VISIBLE);
}
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我尝试在旋转后恢复初始stte,当调用onLayout然后将其移动到位时,视图在旋转之前(有4个状态,从creen到左边,从屏幕到右边,scren顶部)或右下角).
编辑:
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.github.pedrovgs.sample"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="8" />
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- Application configuration -->
<application
android:name=".DraggablePanelApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<!-- Maps API KEY -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC1rMU-mkhoyTvBIdTnYU0dss0tU9vtK48" />
<!-- Main Activity -->
<activity
android:name=".activity.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Places sample -->
<activity
android:name=".activity.PlacesSampleActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/places_sample_activity_title" />
<!-- TV Shows sample -->
<activity
android:name=".activity.TvShowsActivity"
android:label="@string/tv_shows_sample_activity_title" />
<!-- Youtube Sample -->
<activity
android:name=".activity.YoutubeSampleActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/youtube_sample_activity_title" />
<!-- Video Sample -->
<activity
android:name=".activity.VideoSampleActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/video_sample_activity_title" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
样本活动xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:draggable_panel="http://schemas.android.com/apk/res-auto"
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Movie Thumbnail -->
<ImageView
android:id="@+id/iv_thumbnail"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="@style/image_view"/>
<!-- DraggablePanel -->
<com.github.pedrovgs.DraggablePanel
android:id="@+id/draggable_panel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
draggable_panel:x_scale_factor="@dimen/x_scale_factor"
draggable_panel:y_scale_factor="@dimen/y_scale_factor"
draggable_panel:top_fragment_height="@dimen/top_fragment_height"
draggable_panel:top_fragment_margin_right="@dimen/top_fragment_margin"
draggable_panel:top_fragment_margin_bottom="@dimen/top_fragment_margin"
draggable_panel:enable_horizontal_alpha_effect="false"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
您扩展了 aViewGroup而不是 a View,但尚未在 xml 或代码中添加任何child内容。DraggableView我在 中没有看到任何addChild或任何与子项相关的代码DraggableView。您已经使用过topFragment,bottomFragment等等,并且您曾经addFragmentToView()在ViewGroup. 馊主意!
我知道这很困难而且很耗时。但你需要退一步思考这个视图的设计。我强烈建议您将这些topFragment等视为孩子DraggableView,而不是在FragmentManager其中查看和执行addTransaction。
一旦您开始将片段作为子项添加到您的 中ViewGroup,许多并发症就会消失。我这样说是根据我过去的经验。onLayout()是用来排列Child Views里面的ViewGroup。一旦您旋转设备,onLayout()将再次调用并根据您的需要排列子视图。这会很简单。一旦你开始这样概念化,拖动操作就会变得非常简单。
如果您认为作为Framgents孩子在 中ViewGroup是荒谬的,请查看此处的ViewPager代码。这使用片段作为输入,但仍将它们视为布局的子级。
onLayout()本身相关的事情。onSaveInstanceState(). 在此仅保存text或数据。drawableonCofigurationChanged().您现在可能无法执行此操作,但从长远来看,您可能希望重构并以这种方式执行此操作。
记住真正的目的onLayout():
当此视图应为其每个子视图分配大小和位置时从布局中调用。具有子级的派生类应重写此方法并在其每个子级上调用布局。
| 归档时间: |
|
| 查看次数: |
410 次 |
| 最近记录: |