WearableRecyclerView Android Wear操作系统

时间:2018-08-06 21:56:44

标签: java android wear-os android-wear-2.0

我正在尝试创建一个与我的应用程序中的默认Wear OS相似的菜单:Wear OS menu

现在不建议使用其他各种方法,不幸的是,Android文档https://developer.android.com/training/wearables/ui/lists似乎非常不完整。

我找不到任何可以遵循的好示例,因此有人能穿戴这样的WearableRecyclerView示例来设置菜单项和图标并捕获点击事件吗?

谢谢

1 个答案:

答案 0 :(得分:8)

经过几天的尝试,我自己弄清楚了。它可能不是最好的,但至少可以正常工作。我在res / drawable目录中使用了Android Studio创建的矢量图标。对于任何寻求可靠示例的人:

main_menu.xml:

<android.support.wear.widget.WearableRecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_menu_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

main_menu_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_container"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingTop="5dp">

    <ImageView
        android:id="@+id/menu_icon"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/menu_item"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/menu_icon"
        android:layout_toRightOf="@+id/menu_icon"
        android:text="Menu Item"
        android:textColor="#000000"
        android:textSize="24sp"
        tools:ignore="HardcodedText" />
</RelativeLayout>

MainMenuActivity.java:

public class MainMenuActivity extends WearableActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);
    WearableRecyclerView recyclerView = findViewById(R.id.main_menu_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setEdgeItemsCenteringEnabled(true);
    recyclerView.setLayoutManager(new WearableLinearLayoutManager(this));

    ArrayList<MenuItem> menuItems = new ArrayList<>();
    menuItems.add(new MenuItem(R.drawable.icon_1, "Item 1"));
    menuItems.add(new MenuItem(R.drawable.icon_2, "Item 2"));
    menuItems.add(new MenuItem(R.drawable.icon_3, "Item 3"));
    menuItems.add(new MenuItem(R.drawable.icon_4, "Item 4"));

    recyclerView.setAdapter(new MainMenuAdapter(this, menuItems, new MainMenuAdapter.AdapterCallback() {
        @Override
        public void onItemClicked(final Integer menuPosition) {
            switch (menuPosition){
                case 0:  action_1(); break;
                case 1:  action_2(); break;
                case 2:  action_3(); break;
                case 3:  action_4(); break;
                default : cancelMenu();
            }
        }
    }));
}

MainMenuAdapter.java:

public class MainMenuAdapter extends RecyclerView.Adapter<MainMenuAdapter.RecyclerViewHolder> {

    private ArrayList<MenuItem> dataSource = new ArrayList<MenuItem>();
    public interface AdapterCallback{
        void onItemClicked(Integer menuPosition);
    }
    private AdapterCallback callback;

    private String drawableIcon;
    private Context context;


    public MainMenuAdapter(Context context, ArrayList<MenuItem> dataArgs, AdapterCallback callback){
        this.context = context;
        this.dataSource = dataArgs;
        this.callback = callback;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =LayoutInflater.from(parent.getContext()).inflate(R.layout.main_menu_item,parent,false);

        RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);

        return recyclerViewHolder;
    }

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder
    {
        RelativeLayout menuContainer;
        TextView menuItem;
        ImageView menuIcon;

        public RecyclerViewHolder(View view) {
            super(view);
            menuContainer = view.findViewById(R.id.menu_container);
            menuItem = view.findViewById(R.id.menu_item);
            menuIcon = view.findViewById(R.id.menu_icon);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, final int position) {
        MenuItem data_provider = dataSource.get(position);

        holder.menuItem.setText(data_provider.getText());
        holder.menuIcon.setImageResource(data_provider.getImage());
        holder.menuContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {

                if(callback != null) {
                    callback.onItemClicked(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataSource.size();
    }
}

class MenuItem {
    private String text;
    private int image;

    public MenuItem(int image, String text) {
        this.image = image;
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public int getImage() {
        return image;
    }
}