1. 程式人生 > >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