Wear Os基础入门

SnapHelper和RecyclerView

    • 布局
    • 适配器
    • 使用

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

布局

<androidx.recyclerview.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
</androidx.recyclerview.widget.RecyclerView>

适配器

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));
        if (position==0)
        holder.mTextView.setBackgroundResource(R.color.blue_a400);
        if (position==1)
        holder.mTextView.setBackgroundResource(R.color.red_a200);
        if (position==2)
        holder.mTextView.setBackgroundResource(R.color.green);
        if (position==3)
        holder.mTextView.setBackgroundResource(R.color.grey);
    }

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

        }

    }

}

使用

package com.chery.wearosdemo;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;

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

public class MainActivity extends WearableActivity {

    private RecyclerView mRv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        List<String> datas=new ArrayList();
        for (int i = 0; i <4; i++) {
            datas.add(i+"");
        }
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        MyAdapter myAdapter=new MyAdapter(this,datas);
        mRv.setLayoutManager(linearLayoutManager);
        mRv.setAdapter(myAdapter);
        LinearSnapHelper snapHelper=new LinearSnapHelper();
        snapHelper.attachToRecyclerView(mRv);
    }


    private void initView() {
        mRv = (RecyclerView) findViewById(R.id.rv);
    }
}


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