片段在第二轮磨损腕表变成黑色的仿真器/表

编程入门 行业动态 更新时间:2024-10-10 13:21:24
本文介绍了片段在第二轮磨损腕表变成黑色的仿真器/表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

即时得到一个黑色的屏幕上我的Andr​​oid Wear圆形腕表(上方形它的工作原理)。 这两种布局出现在Android工作室的布局设计确定。

Im getting a black screen on my Android Wear round watch (on square it works). Both layouts show up OK in the Android Studio layout design.

项目测试:

bitbucket/powder366/motoblack

即时通讯使用以下code:

Im using the following code:

public class MyFragment extends Fragment { ... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View contentView = inflater.inflate(R.layout.my_fragment, container, false); ...

布局my_fragment:

Layout my_fragment:

<LinearLayout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" android:id="@+id/my_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:keepScreenOn="true" tools:context=".WearFragment"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/text1" android:text="Hi1" android:layout_gravity="center_horizontal" style="@style/UnitsStyle"/> <TextView android:id="@+id/text2" android:text="Hi2" android:layout_gravity="center_horizontal" style="@style/BaseStyle"/> <TextView android:id="@+id/text3" android:text="Hi3" android:layout_gravity="center_horizontal" style="@style/UnitsStyle"/> </LinearLayout> </LinearLayout>

这是调用它:

public class MenuAdapter extends FragmentGridPagerAdapter ... public MenuAdapter(Context context, FragmentManager fm, Handler handler) { super(fm); mContext = context; mHandler = handler; myFragment = new MyFragment(); } ... @Override public Fragment getFragment(int rowNum, int colNum) { Log.d(TAG, String.format("getFragment(%d, %d)", rowNum, colNum)); if(colNum == 0) { return myFragment; } if(colNum == 1) { final SecondFragment switchAction = SecondFragment.newInstance(mHandler); return switchAction; } return null; } ...

这是从所谓的:

Which is called from:

public class WearActivity extends Activity { ... private GridViewPager gridViewPager; private MenuAdapter menuAdapter; @Override protected void onCreate(Bundle savedInstanceState) { Log.d(TAG, "Create(Wear)"); super.onCreate(savedInstanceState); setContentView(R.layout.wear_activity); final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { @Override public void onLayoutInflated(WatchViewStub stub) { menuAdapter = new MenuAdapter(WearActivity.this, getFragmentManager(), mHandler); gridViewPager = (GridViewPager) findViewById(R.id.pager); gridViewPager.setAdapter(menuAdapter); } }); } ...

有没有人见过这个,也可以给我如何解决它的提示?

Have anyone seen this or can give me a hint on how to solve it?

更新:

wear_activity.xml

wear_activity.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.wearable.view.WatchViewStub xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto" xmlns:tools="schemas.android/tools" android:id="@+id/watch_view_stub" android:layout_width="match_parent" android:layout_height="match_parent" app:rectLayout="@layout/rect_activity" app:roundLayout="@layout/round_activity" tools:context=".WearActivity" tools:deviceIds="wear"> </android.support.wearable.view.WatchViewStub>

rect_activity.xml

rect_activity.xml

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" tools:deviceIds="wear_square"> <com.xyz.my.ScrollableGridViewPager xmlns:android="schemas.android/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true"/> </FrameLayout>

round_activity.xml

round_activity.xml

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" tools:deviceIds="wear_round"> <com.xyz.my.ScrollableGridViewPager xmlns:android="schemas.android/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true"/> </FrameLayout>

额外的信息:

Extra info:

public class ScrollableGridViewPager extends GridViewPager { ...

推荐答案

这个问题也不是那么微不足道,所以我希望我所描述它pretty的清晰。

The problem is not so trivial, so I hope that I've described it pretty clearly.

这个问题的真正原因是, onLayoutInflated(WatchViewStub)方法叫了两声圆形设备上。

The real reason of this issue is that the onLayoutInflated(WatchViewStub) method is called twice on the round device.

据调用首次当 rect_activity.xml 充气:

WearActivity$1.onLayoutInflated(WatchViewStub) line: 26 WatchViewStub.inflate() line: 133 WatchViewStub.onMeasure(int, int) line: 141 WatchViewStub(View).measure(int, int) line: 16648

但经过正确的,这个方法的第二个呼叫与新充气 round_activity.xml 在 onApplyWindowInsets(WindowInsets)被分派:

WearActivity$1.onLayoutInflated(WatchViewStub) line: 26 WatchViewStub.inflate() line: 133 WatchViewStub.onApplyWindowInsets(WindowInsets) line: 112 WatchViewStub(View).dispatchApplyWindowInsets(WindowInsets) line: 6051 WatchViewStub(ViewGroup).dispatchApplyWindowInsets(WindowInsets) line: 5435 SwipeDismissLayout(ViewGroup).dispatchApplyWindowInsets(WindowInsets) line: 5439 PhoneWindow$DecorView(ViewGroup).dispatchApplyWindowInsets(WindowInsets) line: 5439 ViewRootImpl.dispatchApplyInsets(View) line: 1170

我不知道是不是只发生在仿真器或也真实的设备这个问题,但我认为这应该被报告为可穿戴支持-lib中的错误(在 WatchViewStub 组件是precise)。这样的行为是不记录任何地方。

I don't know whether this issue occurs only on emulator or on real device also, but I think this should be reported as a bug in wearable-support-lib (in WatchViewStub component to be precise). Such behavior is not documented anywhere.

这个问题上面听起来像它应该做的pretty的什么?新的布局充气(所以旧的被删除),新的 MenuAdapter 将创建(旧的不会再使用谁在乎),而这个新创建 MenuAdapter 应设置为在新的适配器 GridViewPager 。一切都应该重置,所以应该还是能正常工作(除了其性能问题,因为这样的事实,布局膨胀的只有一次,而不是两次)。

The issue above sounds like it should do pretty nothing... New layout is inflated (so the old one is removed), new MenuAdapter will be created (the old one will not be used any more so who cares), and this newly created MenuAdapter should be set as an adapter in new GridViewPager. Everything should "reset" so it should still works fine (apart that the performance issue due to the fact that layout is inflated two times instead of just once).

不完全是......因为这里是棘手的部分与 GridViewPager 。

Not exactly... because here is the tricky part with GridViewPager.

我不知道确切来源$ C ​​$ GridViewPager 的CS和 FragmentGridPagerAdapter ,但它似乎是类似于 ViewPager 和 FragmentPagerAdapter code。 FragmentPagerAdapter 添加了新创建的片段到 FragmentManager 特别标记这是由与 ID 的 ViewPager 并给予孩子的片段的位置:

I don't know the exact source codes of GridViewPager and FragmentGridPagerAdapter but it seems to be similar to ViewPager and FragmentPagerAdapter code. FragmentPagerAdapter adds newly created fragment to the FragmentManager with special tag that is composed with id of the ViewPager and position of given child fragment:

122 private static String More ...makeFragmentName(int viewId, int index) { 123 return "android:switcher:" + viewId + ":" + index; 124 }

在一个片段添加到 FragmentManager (当前活动),可以通过这个引用标记名以后。 真正的问题是一个事实,即 onLayoutInflated(WatchViewStub)方法被调用两次造成的。第一个片段是在第一次创建 MenuAdapter 并添加到容器的id R.id.pager (从 rect_activity.xml )。然后 round_activity.xml 充气,第二次调用 onLayoutInflated(WatchViewStub)被调用 - 新片段在新创建 MenuAdapter ,并加入新的充气 GridViewPager 用相同的ID: R.id.pager 。所以第一个片段和第二个想拥有完全相同的的标签 FragmentManager 。

Once a fragment is added to the FragmentManager (in current Activity) it can be referenced by this tag name in future. The real problem is caused by the fact that onLayoutInflated(WatchViewStub) method is called twice. The first fragment is created in first MenuAdapter and added to the container with id R.id.pager (from the rect_activity.xml). Then round_activity.xml is inflated and second call to onLayoutInflated(WatchViewStub) is invoked - new fragment is created in new MenuAdapter and added to newly inflated GridViewPager with the same id: R.id.pager. So the first fragment and second one want to have exactly the same tag in FragmentManager.

在标准 FragmentPagerAdapter 如果片段,给定标记已美元的点$ psent FragmentManager 它连接到它的previous父母了。你可以看到源$ C ​​$ CS位置:

In standard FragmentPagerAdapter if fragment with given tag is already present in FragmentManager it is attached to it's previous parent again. You can see source codes here:

50 @Override 51 public Object More ...instantiateItem(View container, int position) { 52 if (mCurTransaction == null) { 53 mCurTransaction = mFragmentManager.beginTransaction(); 54 } 55 56 // Do we already have this fragment? 57 String name = makeFragmentName(container.getId(), position); 58 Fragment fragment = mFragmentManager.findFragmentByTag(name); 59 if (fragment != null) { 60 if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment); 61 mCurTransaction.attach(fragment); 62 } else { 63 fragment = getItem(position); 64 if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment); 65 mCurTransaction.add(container.getId(), fragment, 66 makeFragmentName(container.getId(), position)); 67 } 68 if (fragment != mCurrentPrimaryItem) { 69 fragment.setMenuVisibility(false); 70 } 71 72 return fragment; 73 }

所以,可能的情况是类似的,第一个片段被重用,这是添加到第一个 GridViewPager id为 R.id.pager - 但这父母是不是有更多的这样的片段不能在任何地方连接(和显示)。这是实现的事情,只能有一个 ViewPager 般父母带指定id在一个活动。

So probably the situation is similar and the first fragment is reused, which was added to the first GridViewPager with id R.id.pager - but this parent is not there any more so the fragment cannot be attached anywhere (and be displayed). This is the implementation thing that there can be only one ViewPager-like parent with given id in one Activity.

有关多了一个实验,我为设置不同的ID GridViewPagers 。一个用 rect_activity.xml 具有ID R.id.pager1 和另一位在 round_activity。 XML 有`R.id.pager2。

For one more experiment I've set different ids for both GridViewPagers. One with rect_activity.xml has id R.id.pager1 and another one in round_activity.xml has `R.id.pager2'.

final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { @Override public void onLayoutInflated(WatchViewStub stub) { menuAdapter = new MenuAdapter(WearActivity.this, getFragmentManager()); GridViewPager gridViewPager1 = (GridViewPager) findViewById(R.id.pager1); if(gridViewPager1!=null) { // rect_activity gridViewPager1.setAdapter(menuAdapter); } GridViewPager gridViewPager2 = (GridViewPager) findViewById(R.id.pager2); if(gridViewPager2!=null) { // round_activity gridViewPager2.setAdapter(menuAdapter); } } });

在第一遍仅 gridViewPager1 被发现,只有它的适配器设置。类似的,在第二遍 - 仅适用于 gridViewPager2适配器设置。 因为 GridViewPagers 有不同的ID出现在片段标签,所以最后的结果如预期未发生冲突。 但是,这只能证明上述理论:)

In first pass only gridViewPager1 is found and only its adapter is set. Similar in the second pass - only adapter for gridViewPager2 is set. Because GridViewPagers have different ids there is no collision in fragment tags, so the end result is as expected. But this is only to prove the theory described above:)

WatchViewStub 用于为矩形提供不同的布局和圆屏幕。我可以看到你使用完全相同的布局两个形状。你可以摆脱 WatchViewStub 的,只是这样做:

WatchViewStub is used to provide different layouts for rect and round screens. I can see that you use exactly the same layouts for both shapes. You can get rid of the WatchViewStub and just do like this:

public class WearActivity extends Activity { private GridViewPager gridViewPager; private MenuAdapter menuAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.rect_activity); menuAdapter = new MenuAdapter(WearActivity.this, getFragmentManager()); gridViewPager = (GridViewPager) findViewById(R.id.pager); gridViewPager.setAdapter(menuAdapter); } }

这会使你的例子很好地工作,而无需使用 WatchViewStub 。

this will make your example work perfectly without the use of WatchViewStub.

更多推荐

片段在第二轮磨损腕表变成黑色的仿真器/表

本文发布于:2023-11-12 11:14:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581339.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:仿真器   磨损   第二轮   片段   腕表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!