您可以在 Android Wear 的 GridViewPager 中使用 WatchViewStub 吗? [英] Can you use a WatchViewStub in a GridViewPager for Android Wear?
问题描述
我有一个可以独立工作的 WatchViewStub - 它在适当的方形或圆形 android 磨损模拟器上显示正确的布局.但是当我尝试在 GridViewPager 中使用 WatchViewStub 时,它总是显示方形(或矩形)布局,即使在圆形模拟器上也是如此.两者一起使用能成功吗?
I have a WatchViewStub that works fine on its own - it displays the correct layout on the appropriate square or round android wear emulator. But when I try and use the WatchViewStub in a GridViewPager it always displays the square (or rectangle) layout even on the round emulator. Is it possible to use the two together successfully?
以下是一些代码片段:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear);
GridViewPager pager = (GridViewPager) findViewById(R.id.activity_wear_pager);
pager.setAdapter(new gridViewPagerAdapter());
DotsPageIndicator dotsPageIndicator = (DotsPageIndicator) findViewById(R.id.activity_wear_page_indicator);
dotsPageIndicator.setPager(pager);
}
这是 gridViewPagerAdapter:
Here's the gridViewPagerAdapter:
private class gridViewPagerAdapter extends GridPagerAdapter {
@Override
public int getColumnCount(int arg0) { return 2; }
@Override
public int getRowCount() { return 1; }
@Override
protected Object instantiateItem(ViewGroup container, int row, int col) {
View view = null;
if (col == 0) {
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.read_page_stub, container, false);
WatchViewStub stub = (WatchViewStub) view.findViewById(R.id.read_page_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
}
});
}
else {
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.find_page_stub, container, false);
WatchViewStub stub = (WatchViewStub) view.findViewById(R.id.find_page_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
}
});
}
container.addView(view);
return view;
}
@Override
protected void destroyItem(ViewGroup container, int row, int col, Object view) {
container.removeView((View)view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
}
这是activity_wear.xml:
Here is activity_wear.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.wearable.view.GridViewPager
android:id="@+id/activity_wear_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"/>
<android.support.wearable.view.DotsPageIndicator
android:id="@+id/activity_wear_page_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom">
</android.support.wearable.view.DotsPageIndicator>
</FrameLayout>
这里是 read_page_stub.xml:
Here is read_page_stub.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/read_page_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/read_page_rect"
app:roundLayout="@layout/read_page_round"
tools:context=".WearApplication"
tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>
然后我的视图有两个布局.他们开始于:
Then I have two layouts with my views. They start with:
read_page_rect.xml:
read_page_rect.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:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WearApplication"
tools:deviceIds="wear_square">
read_page_round.xml:
read_page_round.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:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WearApplication"
tools:deviceIds="wear_round">
我为 find_page_stub、find_page_rect 和 find_page_round 设置了相同类型的布局.
I have the same type of layout setup for find_page_stub, find_page_rect, and find_page_round.
推荐答案
如果 WatchViewStub
不显示圆形布局,则表示它没有收到 WindowInsets
对象适当地.GridViewPager
必须在途中的某个地方使用它.
If WatchViewStub
doesn't show the round layout it means it doesn't receive the WindowInsets
object properly. GridViewPager
must be consuming it somewhere on the way.
您可以尝试修复它.这样做的方法是(首先是一些理论):
You can try and fix it. The way to do this is (first some theory):
1) 首先阅读 <代码>View.dispatchApplyWindowInsets 和 View.onApplyWindowInsets
1) start by reading about View.dispatchApplyWindowInsets
and View.onApplyWindowInsets
2) 这两个方法绑定在 View
中的方式是这样的:dispatchApplyWindowInsets
被调用并检查是否有任何侦听器;如果是,它只将 WindowInsets
传递给 listener;如果没有,它会将 WindowInsets
传递给 onApplyWindowInsets
.
2) the way these two methods are tied in a View
is this: dispatchApplyWindowInsets
is called and checks if there is any listener; if yes, it delivers the WindowInsets
to the listener only; if no, it delivers the WindowInsets
to onApplyWindowInsets
.
3) 在 ViewGroup
(并且 GridViewPager
是一个 ViewGroup
)中,它遍历子元素并检查插入是否被消耗;如果是,它停止提供它们;
3) in a ViewGroup
(and GridViewPager
is a ViewGroup
) it iterates over the children and checks if the insets were consumed; if yes, it stops delivering them;
4) 在任何时候,任何 View
/ViewGroup
都可以决定停止传送 WindowInsets
;当它这样做时,任何依赖它的孩子在回合中都不会表现得很好;
4) at any point any View
/ViewGroup
can decide to stop delivering the WindowInsets
; when it does, any children depending on it won't behave properly on round;
现在一些练习:
5) 您可以为层次结构中的任何视图创建子类;使用 GridViewPager
执行此操作并开始尝试覆盖 dispatchApplyWindowInsets
和 onApplyWindowInsets
.我的猜测是第一个应该足够了.
5) you can create a subclass of any view you have in your hierarchy; do this with GridViewPager
and start experimenting with overriding dispatchApplyWindowInsets
and onApplyWindowInsets
. My guess is the first one should be enough.
6) 在 dispatchApplyWindowInsets
的覆盖中,迭代子项并调用 dispatchApplyWindowInsets
到每个子项;这样 WatchViewStub
应该使用之前的 isRound()->true
方法接收未改变的 WindowInsets
6) in the override of dispatchApplyWindowInsets
iterate over children and call dispatchApplyWindowInsets
to every of them; this way WatchViewStub
should receive unaltered WindowInsets
with the previous isRound()->true
method
7) 最后别忘了调用super.dispatchApplyWindowInsets(windowInsets)
;你想让 GridViewPager
用插图做它需要的事情.
7) finally, don't forget to call super.dispatchApplyWindowInsets(windowInsets)
; you want to let GridViewPager
do what it needs with the insets.
呼,很长,但应该可以帮助你,并让你了解为什么事情会以这种方式发生.我认为我们有一个错误可以解决这个 GridViewPager
问题,所以也许在下一个版本之后你就不必处理这个问题了.
Phew, pretty long, but should help you and also give you some understanding why things are happening the way are happening. I think we have a bug standing to fix this GridViewPager
issue, so maybe after the next release you won't have to deal with this.
这篇关于您可以在 Android Wear 的 GridViewPager 中使用 WatchViewStub 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!