Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Wear 控件——WearableListView(附Demo)

Android Wear 控件——WearableListView(附Demo)

編輯:關於Android編程

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 listItems;
    static {
        listItems = new ArrayList();
        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




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved