Skip to content

Instantly share code, notes, and snippets.

@nschwermann
Last active December 22, 2016 15:16
Show Gist options
  • Save nschwermann/9a9b433a0fcc8390c2eb to your computer and use it in GitHub Desktop.
Save nschwermann/9a9b433a0fcc8390c2eb to your computer and use it in GitHub Desktop.
GridViewPager example
<?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/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect_activity_watch_face"
app:roundLayout="@layout/round_activity_watch_face"
tools:context="schwiz.net.clockfacelauncher.WatchFace"
tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextClock
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textClock"
android:layout_centerInParent="true"
android:layout_marginTop="58dp" />
</RelativeLayout>
package schwiz.net.example;
import android.content.Context;
import android.graphics.Color;
import android.support.wearable.view.GridPagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import java.util.Random;
public class ExampleAdapter extends GridPagerAdapter{
final Context mContext;
public ExampleAdapter(final Context context) {
mContext = context.getApplicationContext();
}
@Override
public int getRowCount() {
return 2;
}
@Override
public int getColumnCount(int i) {
if(i == 0) {
return 1;
} else {
return 4;
}
}
@Override
public int getCurrentColumnForRow(int row, int currentColumn) {
int ret = super.getCurrentColumnForRow(row, currentColumn);
Log.d("test", String.format("getCurColForRow: ret=%d row=%d curCol=%d", ret, row, currentColumn));
return super.getCurrentColumnForRow(row, currentColumn);
}
@Override
protected Object instantiateItem(ViewGroup viewGroup, int row, int col) {
Log.d("test", "instantiateItem: row=" + row + " col=" + col);
View v;
if(row == 0){
v = View.inflate(mContext, R.layout.digital_clock, null);
} else {
Random random = new Random();
v = View.inflate(mContext, R.layout.testview, null);
v.setBackgroundColor(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
}
viewGroup.addView(v);
return v;
}
@Override
protected void destroyItem(ViewGroup viewGroup, int i, int i2, Object o) {
Log.d("test", "destroyItem row=" + i + " col=" +i2);
viewGroup.removeView((View)o);
}
@Override
public boolean isViewFromObject(View view, Object o) {
Log.d("test", "isViewFromObject");
return view.equals(o);
}
}
package schwiz.net.example;
import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.GridViewPager;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watch_face);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
GridViewPager pager = (GridViewPager)stub.findViewById(R.id.pager);
pager.setAdapter(new ExampleAdapter(Main.this));
}
});
}
@Override
protected void onPause() {
super.onPause();
Log.d("test", "onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.d("test", "onResume");
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="schwiz.net.clockfacelauncher.WatchFace"
tools:deviceIds="wear_square">
<android.support.wearable.view.GridViewPager android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/pager"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testview"
android:layout_width="match_parent" android:layout_height="match_parent">
</FrameLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment