Android Wear 之 WearableListView

公司昨天让搞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);
    }

}

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);
            }
        }

    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值