Skip to content

Instantly share code, notes, and snippets.

@PomepuyN
Last active June 5, 2018 09:02
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PomepuyN/c30760eaee1e58fdd8fa to your computer and use it in GitHub Desktop.
Save PomepuyN/c30760eaee1e58fdd8fa to your computer and use it in GitHub Desktop.
Functional example of WearableListView
<?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_wear"
app:roundLayout="@layout/round_activity_wear"
tools:context=".WearActivity"
tools:deviceIds="wear"
android:background="@color/material_deep_teal_700">
</android.support.wearable.view.WatchViewStub>
<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=".WearActivity"
tools:deviceIds="wear_square"
android:padding="5dp">
<android.support.wearable.view.WearableListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sample_list_view" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout 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=".WearActivity">
<android.support.wearable.view.WearableListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sample_list_view"
app:layout_box="all"
/>
</android.support.wearable.view.BoxInsetLayout>
public class SettingsAdapter extends WearableListView.Adapter {
private final Context context;
private final List<SettingsItems> items;
public SettingsAdapter(Context context, List<SettingsItems> items) {
this.context = context;
this.items = items;
}
@Override
public WearableListView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new WearableListView.ViewHolder(new SettingsItemView(context));
}
@Override
public void onBindViewHolder(WearableListView.ViewHolder viewHolder, final int position) {
SettingsItemView SettingsItemView = (SettingsItemView) viewHolder.itemView;
final SettingsItems item = items.get(position);
TextView textView = (TextView) SettingsItemView.findViewById(R.id.text);
textView.setText(item.title);
final ImageView imageView = (ImageView) SettingsItemView.findViewById(R.id.image);
imageView.setImageResource(item.iconRes);
}
@Override
public int getItemCount() {
return items.size();
}
}
public class SettingsItems {
public SettingsItems(int iconRes, String title) {
this.iconRes = iconRes;
this.title = title;
}
public int iconRes;
public String title;
}
public final class SettingsItemView extends FrameLayout implements WearableListView.OnCenterProximityListener {
final ImageView image;
final TextView text;
public SettingsItemView(Context context) {
super(context);
View.inflate(context, R.layout.wearablelistview_item, this);
image = (ImageView) findViewById(R.id.image);
text = (TextView) findViewById(R.id.text);
}
@Override
public void onCenterPosition(boolean b) {
//Animation example to be ran when the view becomes the centered one
image.animate().scaleX(1f).scaleY(1f).alpha(1);
text.animate().scaleX(1f).scaleY(1f).alpha(1);
}
@Override
public void onNonCenterPosition(boolean b) {
//Animation example to be ran when the view is not the centered one anymore
image.animate().scaleX(0.8f).scaleY(0.8f).alpha(0.6f);
text.animate().scaleX(0.8f).scaleY(0.8f).alpha(0.6f);
}
}
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/image"
android:layout_height="52dp"
android:layout_width="52dp"
android:scaleX="0.8"
android:scaleY="0.8"
android:alpha="0.6"
/>
<TextView
android:id="@+id/text"
android:gravity="center_vertical"
android:layout_height="52dp"
android:layout_marginLeft="72dp"
android:layout_marginRight="16dp"
android:layout_width="wrap_content"
android:textColor="@color/white"
android:scaleX="0.8"
android:scaleY="0.8"
android:alpha="0.6"
android:textSize="18sp"
/>
</merge>
public class WearActivity extends Activity implements WearableListView.ClickListener {
private WearableListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
listView = (WearableListView) stub.findViewById(R.id.sample_list_view);
loadAdapter();
}
});
}
private void loadAdapter() {
List<SettingsItems> items = new ArrayList<>();
items.add(new SettingsItems(R.drawable.ic_color, getString(R.string.theme)));
items.add(new SettingsItems(R.drawable.ic_more, getString(R.string.more_on_phone)));
SettingsAdapter mAdapter = new SettingsAdapter(this, items);
listView.setAdapter(mAdapter);
listView.setClickListener(this);
}
@Override
public void onClick(WearableListView.ViewHolder viewHolder) {
switch (viewHolder.getPosition()) {
case 0:
//Do something
break;
case 1:
//Do something else
break;
}
}
@Override
public void onTopEmptyRegionClick() {
//Prevent NullPointerException
}
}
@Rezar
Copy link

Rezar commented Dec 13, 2014

Thanks a lot. But there are some classes and layout missing such as SettingsItems and simple_list_view

@PomepuyN
Copy link
Author

@Rezar I just updated the gist with all the classes and layouts (and finally found why the coloration didn't work).

@Rezar
Copy link

Rezar commented Dec 15, 2014

Thanks :) but still there are small problems:
round_activity_wear.xml --> app:layout_box="all" raise error.
there is no sample_list_view. Could you add sample_list_view.xml here too ?

@takesomecode
Copy link

This needs to be added to the merge tag xmlns:app="http://schemas.android.com/apk/res-auto"

@takesomecode
Copy link

Anyone know how to have different height for each list item by letting them expand to fit their text content?

@Frankzor
Copy link

Awesome gist! Based on your code, I was finally able to make, on my Android Wear app, a WearableListView of CheckBoxes that remembers which items were checked when scrolling without that dreaded CheckBox recycling issue/feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment