Android Wear 之 WearableListView

公司昨天让搞android wear 然后下午就开始恶补。

找到官网上的关于Wear的开发教程。看到正好里面有一篇关于wearablelistview的教程。就学习了下。

在敲demo的时候还发现一个问题。就是它过时了!!!我擦,官方怎么还推荐过时的东西。但是没办法还得搞。第二天来了一看,发现已经更新为WearRecyclerView了。


先来张最终效果图:



先看布局 activity_wearlist:




    

        
        

    



list_item:




    

    


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

    }
}








你可能感兴趣的:(AndroidWear)