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>