android:recyclerview中的条目 [英] android:entries in recyclerview
问题描述
我已经将listview
用于条目属性,如下所示:
I have used listview
with entries attribute like below :
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="@array/fi"/>
现在我正在将其转换为RecyclerView
Now i am converting it to RecyclerView
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
我想知道在RecyclerView
中是否具有android:entries
属性?还是其他任何属性而不是条目?
I want to know whether we have android:entries
attribute in RecyclerView
? Or any other attribute instead of entries?
推荐答案
正如在其他答案中正确解释的那样,没有属性可以从xml填充RecyclerView.但是,使用 Android数据绑定,您可以轻松创建类似的属性:>
As correctly explained in the other answers there isn't an attribute to fill the RecyclerView from xml. However using the Android Databinding you can create a similar attribute quite easily:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:entries="@{@stringArray/fi}"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
</layout>
以下是绑定适配器定义:
Here the binding adapter definition:
import android.databinding.BindingAdapter;
public class RecyclerViewBindings {
@BindingAdapter("entries")
public static void entries(RecyclerView recyclerView, String[] array) {
recyclerView.setAdapter(new SimpleArrayAdapter(array));
}
static class SimpleArrayAdapter extends RecyclerView.Adapter<SimpleHolder> {
private final String[] mArray;
public SimpleArrayAdapter(String[] array) {
mArray = array;
}
@Override
public SimpleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
return new SimpleHolder(view);
}
@Override
public void onBindViewHolder(SimpleHolder holder, int position) {
holder.mTextView.setText(mArray[position]);
}
@Override
public int getItemCount() {
return mArray.length;
}
}
static class SimpleHolder extends RecyclerView.ViewHolder {
private final TextView mTextView;
public SimpleHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(android.R.id.text1);
}
}
}
然后,您必须使用DataBindingUtil
方法来增加布局.
Then you have to use the DataBindingUtil
methods for inflate the layout.
在活动内充气:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DataBindingUtil.setContentView(this, R.layout.content_main);
}
在片段内充气:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ContentMainBinding bindings = DataBindingUtil.inflate(inflater, R.layout.content_main, container, false);
return bindings.getRoot();
}
这篇关于android:recyclerview中的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!