我想创建一个弯曲的布局来列出一些元素,并适应Android Wear,但是有关此问题的Google Android开发者页面非常不清楚。有人成功创建了弯曲的布局吗?
这是我所说的Google开发者页面:
这是我所说的Google开发者页面:
https://developer.android.com/training/wearables/ui/lists.html#creating
如果您有任何实现此目标的技巧,我将非常乐意了解。
https://developer.android.com/training/wearables/ui/lists.html#creating
如果您有任何实现此目标的技巧,我将非常乐意了解。
我希望你已经理解了,如果没有,以下是步骤及示例代码:
在您的round\activity_main.xml
文件中,使用android.support.wear.widget.WearableRecyclerView
作为根/主要元素:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.WearableRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rv_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- ... -->
</android.support.wear.widget.WearableRecyclerView>
然后在MainActivity
中扩展WearableActivity
:
import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.support.wear.widget.WearableRecyclerView
public class MainActivity extends WearableActivity {
private android.support.wear.widget.WearableRecyclerView wearableRecyclerView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wearableRecyclerView = findViewById(R.id.rv_test);
wearableRecyclerView.setLayoutManager(new WearableLinearLayoutManager(MainActivity.this));
wearableRecyclerView.setEdgeItemsCenteringEnabled(true);
}
...
}
您需要确保在xml和java文件中都使用正确的导入WearableRecyclerView
(android.support.wear.widget.WearableRecyclerView
)
接下来(开发者网页中没有解释)需要创建一个ViewHolder
:
1 - 首先创建一个xml布局(这里称为circle_view.xml
):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/close_button"
android:id="@+id/circledImageView"/>
</LinearLayout>
2 - 然后创建一个Java类(这里称为TestHolder.java
),并将其扩展到RecyclerView.ViewHolder
:
public class TestHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
public TestHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.circledImageView);
}
}
接着在MainActivity.java
中,在onCreate
方法中wearableRecyclerView.setEdgeItemsCenteringEnabled(true);
下面添加以下内容:
RecyclerView.Adapter<TestHolder> testHolderAdapter = new RecyclerView.Adapter<TestHolder>() {
@Override
public TestHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.circle_view, parent, false);
return new TestHolder(view);
}
@Override
public void onBindViewHolder(TestHolder holder, int position) {
}
@Override
public int getItemCount() {
//change return number to desired number of items or an array size or list length
return 5;
}
};
wearableRecyclerView.setAdapter(testHolderAdapter);
就这些了,将您的应用程序在圆形手表模拟器或圆形/带下巴的手表上运行,并享受曲线列表。