我找不到帮助我在 Wearable 中创建菜单的教程RecyclerView
。弹出的所有内容都与Recyclerview
手机有关。所以我问的是可穿戴版本是否以相同的方式工作?
我遵循了文档中的内容,但我不知道如何添加项目,以便最终得到如下内容:
编辑:
所以在搞砸了一点并看到Recyclerview
这里的代码之后:https ://codinginflow.com/tutorials/android/simple-recyclerview-java/part-2-adapter (顺便说一句很有帮助)
我设法创建了一个项目列表。问题是它们垂直排列,它们之间有很大的空间。我已将代码行放在文档中,这些代码应该像图像中一样排列,但我不知道问题出在哪里。
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.wear.ambient.AmbientModeSupport;
import androidx.wear.widget.WearableLinearLayoutManager;
import androidx.wear.widget.WearableRecyclerView;
import java.util.ArrayList;
import java.util.List;
public class circular_menu_external extends FragmentActivity
implements AmbientModeSupport.AmbientCallbackProvider, MenuItem.OnMenuItemClickListener {
// Fragment toolsfragment = new Tools();
// Fragment modesfragment = new Modes();
// Fragment keysfragment = new Keys();
// Fragment flowratefragment = new Flowrates();
private WearableRecyclerView wearableRecyclerView;
private Button btn;
private ArrayList<ExampleItem> mExampleList;
private RecyclerView.Adapter mAdapter;
private WearableRecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_circular_menu_external);
wearableRecyclerView = findViewById(R.id.main_recycler_view);
mAdapter = new MenuRecyclerViewAdapter(mExampleList);
//Create a curved layout
CustomScrollingLayoutCallback customScrollingLayoutCallback = new CustomScrollingLayoutCallback();
// WearableLinearLayoutManager wearableLinearLayoutManager = new WearableLinearLayoutManager(this);
// wearableRecyclerView.setLayoutManager(wearableLinearLayoutManager);
// CustomScrollingLayoutCallback customScrollingLayoutCallback = new CustomScrollingLayoutCallback();
wearableRecyclerView.setLayoutManager(
new WearableLinearLayoutManager(this, customScrollingLayoutCallback));
// wearableRecyclerView.setLayoutManager(new WearableLinearLayoutManager(this));
wearableRecyclerView.setEdgeItemsCenteringEnabled(true);
createExampleList();
buildRecyclerView();
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
}
private void action_1() {
Intent i1 = new Intent(circular_menu_external.this, Settings.class);
startActivity(i1);
}
private void action_2(){
Intent i1 = new Intent(circular_menu_external.this, tool1mode1.class);
startActivity(i1);
}
private void action_3(){
Intent i1 = new Intent(circular_menu_external.this, tool1mode2.class);
startActivity(i1);
}
private void action_4(){
Intent i1 = new Intent(circular_menu_external.this, tool1mode3.class);
startActivity(i1);
}
private void cancelMenu(){
Intent i1 = new Intent(circular_menu_external.this, Main_menu.class);
startActivity(i1);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
@Override
public AmbientModeSupport.AmbientCallback getAmbientCallback() {
return null;
}
public void createExampleList() {
mExampleList = new ArrayList<>();
mExampleList.add(new ExampleItem(R.drawable.tool_ic, "Line 1", "Line 2"));
mExampleList.add(new ExampleItem(R.drawable.mode_ic, "Line 3", "Line 4"));
mExampleList.add(new ExampleItem(R.drawable.key, "Line 5", "Line 6"));
mExampleList.add(new ExampleItem(R.drawable.flow_ic, "Line 7", "Line 8"));
}
public void buildRecyclerView() {
wearableRecyclerView.setEdgeItemsCenteringEnabled(true);
wearableRecyclerView.setHasFixedSize(true);
wearableRecyclerView.setEdgeItemsCenteringEnabled(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new MenuRecyclerViewAdapter(mExampleList);
wearableRecyclerView.setLayoutManager(mLayoutManager);
wearableRecyclerView.setAdapter(mAdapter);
//Add a circular scrolling gesture
wearableRecyclerView.setCircularScrollingGestureEnabled(true);
wearableRecyclerView.setBezelFraction(0.5f);
wearableRecyclerView.setScrollDegreesPerScreen(90);
}
public class CustomScrollingLayoutCallback extends WearableLinearLayoutManager.LayoutCallback {
/** How much should we scale the icon at most. */
private static final float MAX_ICON_PROGRESS = 0.65f;
private float progressToCenter;
@Override
public void onLayoutFinished(View child, RecyclerView parent) {
// Figure out % progress from top to bottom
float centerOffset = ((float) child.getHeight() / 2.0f) / (float) parent.getHeight();
float yRelativeToCenterOffset = (child.getY() / parent.getHeight()) + centerOffset;
// Normalize for center
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);
}
}
}
这是适配器:
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.wear.widget.WearableRecyclerView;
import java.util.ArrayList;
public class MenuRecyclerViewAdapter extends WearableRecyclerView.Adapter<MenuRecyclerViewAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends WearableRecyclerView.ViewHolder {
public ImageView mImageView1,mImageView2,mImageView3,mImageView4;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView1 = itemView.findViewById(R.id.imageView1);
mImageView2 = itemView.findViewById(R.id.imageView2);
mImageView3 = itemView.findViewById(R.id.imageView3);
mImageView4 = itemView.findViewById(R.id.imageView4);
}
}
public MenuRecyclerViewAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView1.setImageResource(currentItem.getImageResource());
holder.mImageView2.setImageResource(currentItem.getImageResource());
holder.mImageView3.setImageResource(currentItem.getImageResource());
holder.mImageView4.setImageResource(currentItem.getImageResource());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}
我的物品的xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.wear.widget.CircularProgressLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">
<ImageButton
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_shape"
android:src="@drawable/homeicon4"
tools:ignore="MissingConstraints" />
<ImageButton
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_shape"
android:src="@drawable/bypass3"
tools:ignore="MissingConstraints" />
<ImageButton
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_shape"
android:src="@drawable/dualflow3"
tools:ignore="MissingConstraints" />
<ImageButton
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_shape"
android:src="@drawable/isolation3"
tools:ignore="MissingConstraints" />
</RelativeLayout>
</androidx.wear.widget.CircularProgressLayout>