【发布时间】:2015-05-12 13:53:40
【问题描述】:
我有一个独立工作的 WatchViewStub - 它在适当的方形或圆形 android wear 模拟器上显示正确的布局。但是当我尝试在 GridViewPager 中使用 WatchViewStub 时,即使在圆形模拟器上,它也总是显示方形(或矩形)布局。两者一起使用成功吗?
以下是一些代码片段:
@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:
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:
<?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:
<?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>
然后我的视图有两个布局。他们开始于:
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:
<?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 设置了相同类型的布局设置。
【问题讨论】:
-
另一种方法是使用 FragmentGridPagerAdapter(扩展 GridPagerAdapter)并以 SomeFragment.newInstance(listOfObjects.get(row)); 的方式在 getFragment(...) 中填充片段;然后在 SomeFragment 的 onCreateView 中执行 WatchViewStub 的工作。这对我来说效果很好,我不需要处理 WindowInsets。