Android Wear 之 WearableListView
2017-02-10 16:50
477 查看
公司昨天让搞android wear 然后下午就开始恶补。
找到官网上的关于Wear的开发教程。看到正好里面有一篇关于wearablelistview的教程。就学习了下。
在敲demo的时候还发现一个问题。就是它过时了!!!我擦,官方怎么还推荐过时的东西。但是没办法还得搞。第二天来了一看,发现已经更新为WearRecyclerView了。
先来张最终效果图:
先看布局 activity_wearlist:<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout
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/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.acb.myapplication104.MainActivity"
tools:deviceIds="wear">
<FrameLayout
android:id="@+id/frame_layout"
app:layout_box="left|bottom|right"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.wearable.view.WearableListView
android:id="@+id/wearlistview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.wearable.view.WearableListView>
</FrameLayout>
</android.support.wearable.view.BoxInsetLayout>
list_item:
<?xml version="1.0" encoding="utf-8"?>
<com.example.acb.myapplication104.WearableListItemLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:background="@color/hui"
android:layout_height="80dp">
<ImageView
android:id="@+id/circle"
android:layout_height="20dp"
android:layout_margin="16dp"
android:layout_width="20dp"
android:src="@drawable/ic_cc_clear"/>
<TextView
android:id="@+id/name"
android:gravity="center_vertical|left"
android:layout_width="wrap_content"
android:layout_marginRight="16dp"
android:layout_height="match_parent"
android:fontFamily="sans-serif-condensed-light"
android:lineSpacingExtra="-4sp"
android:textColor="@color/text_color"
android:textSize="16sp"/>
</com.example.acb.myapplication104.WearableListItemLayout>
WearableListItemLayout: 自定义item相关
public class WearableListItemLayout extends LinearLayout implements WearableListView.OnCenterProximityListener {
private ImageView mCircle;
private TextView mName;
private final float mFadedTextAlpha;
private final int mFadedCircleColor;
private final int mChosenCircleColor;
public WearableListItemLayout(Context context) {
this(context, null);
}
public WearableListItemLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public WearableListItemLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
//这个是未选中选项的样式
mFadedTextAlpha = getResources()
.getInteger(R.integer.action_text_faded_alpha) / 100f;
mFadedCircleColor = getResources().getColor(R.color.grey);
//选中的
mChosenCircleColor = getResources().getColor(R.color.blue);
}
// Get references to the icon and text in the item layout definition
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// These are defined in the layout file for list items
// (see next section)
mCircle = (ImageView) findViewById(R.id.circle);
mName = (TextView) findViewById(R.id.name);
}
@Override
public void onCenterPosition(boolean animate) {
mName.setAlpha(1f);
// ((GradientDrawable) mCircle.getDrawable()).setColor(mChosenCircleColor);
}
@Override
public void onNonCenterPosition(boolean animate) {
// ((GradientDrawable) mCircle.getDrawable()).setColor(mFadedCircleColor);
mName.setAlpha(mFadedTextAlpha);
}
}
找到官网上的关于Wear的开发教程。看到正好里面有一篇关于wearablelistview的教程。就学习了下。
在敲demo的时候还发现一个问题。就是它过时了!!!我擦,官方怎么还推荐过时的东西。但是没办法还得搞。第二天来了一看,发现已经更新为WearRecyclerView了。
先来张最终效果图:
先看布局 activity_wearlist:<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout
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/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.acb.myapplication104.MainActivity"
tools:deviceIds="wear">
<FrameLayout
android:id="@+id/frame_layout"
app:layout_box="left|bottom|right"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.wearable.view.WearableListView
android:id="@+id/wearlistview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.wearable.view.WearableListView>
</FrameLayout>
</android.support.wearable.view.BoxInsetLayout>
list_item:
<?xml version="1.0" encoding="utf-8"?>
<com.example.acb.myapplication104.WearableListItemLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:background="@color/hui"
android:layout_height="80dp">
<ImageView
android:id="@+id/circle"
android:layout_height="20dp"
android:layout_margin="16dp"
android:layout_width="20dp"
android:src="@drawable/ic_cc_clear"/>
<TextView
android:id="@+id/name"
android:gravity="center_vertical|left"
android:layout_width="wrap_content"
android:layout_marginRight="16dp"
android:layout_height="match_parent"
android:fontFamily="sans-serif-condensed-light"
android:lineSpacingExtra="-4sp"
android:textColor="@color/text_color"
android:textSize="16sp"/>
</com.example.acb.myapplication104.WearableListItemLayout>
WearableListItemLayout: 自定义item相关
public class WearableListItemLayout extends LinearLayout implements WearableListView.OnCenterProximityListener {
private ImageView mCircle;
private TextView mName;
private final float mFadedTextAlpha;
private final int mFadedCircleColor;
private final int mChosenCircleColor;
public WearableListItemLayout(Context context) {
this(context, null);
}
public WearableListItemLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public WearableListItemLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
//这个是未选中选项的样式
mFadedTextAlpha = getResources()
.getInteger(R.integer.action_text_faded_alpha) / 100f;
mFadedCircleColor = getResources().getColor(R.color.grey);
//选中的
mChosenCircleColor = getResources().getColor(R.color.blue);
}
// Get references to the icon and text in the item layout definition
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// These are defined in the layout file for list items
// (see next section)
mCircle = (ImageView) findViewById(R.id.circle);
mName = (TextView) findViewById(R.id.name);
}
@Override
public void onCenterPosition(boolean animate) {
mName.setAlpha(1f);
// ((GradientDrawable) mCircle.getDrawable()).setColor(mChosenCircleColor);
}
@Override
public void onNonCenterPosition(boolean animate) {
// ((GradientDrawable) mCircle.getDrawable()).setColor(mFadedCircleColor);
mName.setAlpha(mFadedTextAlpha);
}
}
public class WearListViewActivity extends WearableActivity implements WearableListView.ClickListener { // Sample dataset for the list String[] elements = { "List Item 1", "List Item 2", "List Item 3","List Item 4","List Item 5",}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wearlist); setAmbientEnabled(); WearableListView listView = (WearableListView) findViewById(R.id.wearlistview); listView.setAdapter(new Adapter(this , elements)); listView.setClickListener(this); } @Override public void onClick(WearableListView.ViewHolder viewHolder) { Integer tag = (Integer) viewHolder.itemView.getTag(); } @Override public void onTopEmptyRegionClick() { } private static final class Adapter extends WearableListView.Adapter{ private String[] mDataset; private final Context mContext; private final LayoutInflater mInflater; private Adapter(Context mContext, String[] dataset) { this.mContext = mContext; this.mInflater = LayoutInflater.from(mContext); this.mDataset = dataset; } @Override public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ItemViewHolder(mInflater.inflate(R.layout.list_item,null)); } @Override public void onBindViewHolder(WearableListView.ViewHolder holder, int position) { ItemViewHolder itemHolder = (ItemViewHolder) holder; TextView view = itemHolder.textView; view.setText(mDataset[position]); ((ItemViewHolder) holder).itemView.setTag(position); } @Override public int getItemCount() { return mDataset.length; } public static class ItemViewHolder extends WearableListView.ViewHolder{ private TextView textView; public ItemViewHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.name); } } } }
相关文章推荐
- 神兵利器-Android性能调优工具Hugo
- Android Studio第三十期 - 介绍几种网络请求方式写法
- java.lang.NoSuchMethodError: android.view.ViewTreeObserver.removeOnGlobalLay的解决方案
- Android 视频录制
- Gradle实战:Android多渠道打包方案汇总
- 创造设计模式
- Android 黑客入门--小知识
- Android给ListView添加一个入场动画
- Android--实现点击一次返回键返回桌面而不是退出应用
- Android源码编译之Vim命令合集
- android contentprovider简单讲解
- 收藏的那些Android干货
- 基于业务模拟的自动化测试实践
- Android PopupWindow 隐藏软键盘的方法
- Android中使用MediaCodec硬件解码,高效率得到YUV格式帧,快速保存JPEG图片(不使用OpenGL)(附Demo)
- android HDCP移植开发小结
- Android四大基本组件之一 ,广播(broadcast receiver)---有序广播
- 组件化开发Android应用及SDK
- Singleton设计模式 - 创造性设计模式
- Android 自定义View 9Path图片绘制