android:entries 在回收站视图中

android:entries in recyclerview

我使用了 listview 和如下条目属性:

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:entries="@array/fi"/>

现在我将其转换为 RecyclerView

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我想知道 RecyclerView 中是否有 android:entries 属性?或者任何其他属性而不是条目?

No. There is no such directly available attribute in RecyclerView. You have to do it from java code programatically using LayoutManager and RecyclerView.Adapter. .

原因: 正如我们所知,RecyclerView 在我们设置 LayoutManager 之前不会膨胀。此外,LayoutManager 是膨胀 RecyclerView 的单个项目视图所必需的。这些单独的项目视图是从 RecyclerView.Adapter 中检索的。因此,除非您将 LayoutManagerRecyclerView.Adapter 都设置为 RecyclerView,否则您无法使用 RecyclerView

希望这个回答对您有所帮助。

恐怕这不可能开箱即用,您可以扩展 RecyclerView 并定义您自己的接受字符串数组的自定义属性,然后您将使用这些值填充 RecyclerView 适配器。

检查此 link: http://developer.android.com/training/custom-views/create-view.html#customattr

正如其他答案中正确解释的那样,xml 中没有用于填充 RecyclerView 的属性。但是使用 Android Databinding 你可以很容易地创建一个类似的属性:

<?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>

这里是绑定适配器定义:

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 方法来膨胀布局。

在 Activity 内膨胀:

@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 不包含 android:entries 或类似属性。 RecyclerView 是 listview 的继任者,但仍然缺少 entries 属性和 onclicklistener

这里是android官方文档link

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

该站点还介绍了 android:entries 属性。

http://androidride.com/easiest-way-make-listview-android/