Last active
June 5, 2018 09:02
-
-
Save PomepuyN/c30760eaee1e58fdd8fa to your computer and use it in GitHub Desktop.
Functional example of WearableListView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SettingsItems { | |
public SettingsItems(int iconRes, String title) { | |
this.iconRes = iconRes; | |
this.title = title; | |
} | |
public int iconRes; | |
public String title; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
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 ?
This needs to be added to the merge tag xmlns:app="http://schemas.android.com/apk/res-auto"
Anyone know how to have different height for each list item by letting them expand to fit their text content?
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
@Rezar I just updated the gist with all the classes and layouts (and finally found why the coloration didn't work).