首页 > 代码库 > Android Wear 控件——WearableListView(附Demo)

Android Wear 控件——WearableListView(附Demo)

WearableListView是适用于android小型设备如智能手表显示列表的组件,可以村子滚动,非常方便的在只能手表上显示列表控件,每次显示三个列表在屏幕中间位置。

它继承RecyclerView,实现了OnScrollListener接口。

public class WearableListView extends android.support.v7.widget.RecyclerView 
          implements android.support.v7.widget.RecyclerView.OnScrollListener {
  
  ….
  ….
}
下面是使用的例子


package com.technotalkative.wearablelistviewdemo;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.support.wearable.view.WearableListView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
 
import java.util.ArrayList;
 
public class SimpleListActivity extends Activity implements WearableListView.ClickListener{
 
    private WearableListView mListView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mListView = (WearableListView) stub.findViewById(R.id.listView1);
                mListView.setAdapter(new MyAdapter(SimpleListActivity.this));
                mListView.setClickListener(SimpleListActivity.this);
            }
        });
    }
 
    private static ArrayList<String> listItems;
    static {
        listItems = new ArrayList<String>();
        listItems.add("Monday");
        listItems.add("Tuesday");
        listItems.add("Wednesday");
        listItems.add("Thursday");
        listItems.add("Friday");
        listItems.add("Saturday");
    }
 
    @Override
    public void onClick(WearableListView.ViewHolder viewHolder) {
 
    }
 
    @Override
    public void onTopEmptyRegionClick() {
 
    }
 
    private class MyAdapter extends WearableListView.Adapter {
        private final LayoutInflater mInflater;
 
        private MyAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }
 
        @Override
        public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new WearableListView.ViewHolder(
                    mInflater.inflate(R.layout.row_simple_item_layout, null));
        }
 
        @Override
        public void onBindViewHolder(WearableListView.ViewHolder holder, int position) {
            TextView view = (TextView) holder.itemView.findViewById(R.id.textView);
            view.setText(listItems.get(position).toString());
            holder.itemView.setTag(position);
        }
 
        @Override
        public int getItemCount() {
            return listItems.size();
        }
    }
}


可以为该列表加入图标,更加漂亮


private final class MyItemView extends FrameLayout implements WearableListView.Item {
 
        final CircledImageView imgView;
        final TextView txtView;
        private float mScale;
        private final int mFadedCircleColor;
        private final int mChosenCircleColor;
 
        public MyItemView(Context context) {
            super(context);
            View.inflate(context, R.layout.row_advanced_item_layout, this);
            imgView = (CircledImageView) findViewById(R.id.image);
            txtView = (TextView) findViewById(R.id.text);
            mFadedCircleColor = getResources().getColor(android.R.color.darker_gray);
            mChosenCircleColor = getResources().getColor(android.R.color.holo_blue_dark);
        }
 
        @Override
        public float getProximityMinValue() {
            return mDefaultCircleRadius;
        }
 
        @Override
        public float getProximityMaxValue() {
            return mSelectedCircleRadius;
        }
 
        @Override
        public float getCurrentProximityValue() {
            return mScale;
        }
 
        @Override
        public void setScalingAnimatorValue(float value) {
            mScale = value;
            imgView.setCircleRadius(mScale);
            imgView.setCircleRadiusPressed(mScale);
        }
 
        @Override
        public void onScaleUpStart() {
            imgView.setAlpha(1f);
            txtView.setAlpha(1f);
            imgView.setCircleColor(mChosenCircleColor);
        }
 
        @Override
        public void onScaleDownStart() {
            imgView.setAlpha(0.5f);
            txtView.setAlpha(0.5f);
            imgView.setCircleColor(mFadedCircleColor);
        }
    }

下面的连接是该demo的完整工程WearableListView Demo




Android Wear 控件——WearableListView(附Demo)