Wear Os基础入门

WearableRecyclerView

    • 布局
    • 资源
    • 代码

Wear Os基础入门_第1张图片

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.wear.widget.WearableRecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recycler_launcher_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

资源

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="4dp"
    android:paddingEnd="?android:listPreferredItemPaddingEnd">
    <ImageView
        android:id="@+id/icon_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/a"/>
    <TextView
        android:id="@+id/icon_text_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:gravity="center_vertical"
        android:text="张三" />
</LinearLayout>

代码

package com.chery.wearosdemo;

/**
 * @author DcotorWei
 * @date :2020/3/19 9:58
 * @description :TODO
 * @email :[email protected]
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter<T> extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private Context context;
    private List<T> data;

    public MyAdapter(Context context, List<T> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.app_item_layout, parent, false);
        MyViewHolder viewHolder=new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
        holder.mTextView.setText((CharSequence) data.get(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView mTextView;
        ImageView mImageView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.icon_text_view);
            mImageView = itemView.findViewById(R.id.icon_image_view);
        }

    }

}

package com.chery.wearosdemo;

import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.wear.widget.WearableLinearLayoutManager;
import androidx.wear.widget.WearableRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends WearableActivity {


    private WearableRecyclerView wearableRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();


        // Enables Always-on
        setAmbientEnabled();
    }


    private void initView() {
        wearableRecyclerView = (WearableRecyclerView) findViewById(R.id.recycler_launcher_view);

        List<String> datas = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            datas.add("" + i);
        }

        CustomScrollingLayoutCallback customScrollingLayoutCallback =
                new CustomScrollingLayoutCallback();


        wearableRecyclerView.setLayoutManager(
                new WearableLinearLayoutManager(this,customScrollingLayoutCallback));
        // To align the edge children (first and last) with the center of the screen
        wearableRecyclerView.setEdgeItemsCenteringEnabled(true);
        wearableRecyclerView.setAdapter(new MyAdapter(this, datas));
    }
    public class CustomScrollingLayoutCallback extends WearableLinearLayoutManager.LayoutCallback {
        /** How much should we scale the icon at most. */
        private static final float MAX_ICON_PROGRESS = 0.65f;


        @Override
        public void onLayoutFinished(View child, RecyclerView parent) {

            // Figure out % progress from top to bottom.
            float centerOffset = (child.getHeight() / 2.0f) / parent.getHeight();
            float yRelativeToCenterOffset = (child.getY() / parent.getHeight()) + centerOffset;

            // Normalize for center.
            float progressToCenter = Math.abs(0.5f - yRelativeToCenterOffset);

            // Adjust to the maximum scale.
            progressToCenter = Math.min(progressToCenter, MAX_ICON_PROGRESS);

            child.setScaleX(1 - progressToCenter);
            child.setScaleY(1 - progressToCenter);
        }
    }

}


你可能感兴趣的:(Wear Os基础入门)