【问题标题】:android:entries in recyclerviewandroid:recyclerview中的条目
【发布时间】:2015-11-24 11:19:32
【问题描述】:

我使用了 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属性?或者任何其他属性而不是条目?

【问题讨论】:

  • 没有选项,因为recyclerview没有基本的默认itemxml文件。你必须为此创建适配器
  • 没有选项?以及ListView 是如何做到这一点的?请参阅ListView 的来源,然后以here 为例
  • @pskink thnks 提出宝贵意见,所以除了创建适配器之外别无他法?我们不能从 xml 中分配项目值,对吧?
  • 查看我链接的源代码,ListView 像这样从 xml 资源文件中获取值:a.getTextArray(R.styleable.ListView_entries);

标签: android android-recyclerview android-databinding


【解决方案1】:

正如其他答案中正确解释的那样,没有从 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();
}

【讨论】:

  • 这对我来说是新信息,仅凭这个答案,您就将一个绝望的线程变成了一个非常有用的线程!好极了。我想知道 BindingAdapter 如何链接到我们特定的回收器视图?
  • @Riccardo :未连接适配器;跳过布局。收到此错误
  • 您需要使用数据绑定方法来膨胀视图。我会更新我的答案
【解决方案2】:

没有。 RecyclerView 中没有这样的直接可用属性。您必须使用 LayoutManagerRecyclerView.Adapter 以编程方式从 java 代码中执行此操作。 Refer this answer.

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

希望这个回答对你有帮助。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      不,在 android 中,Recyclerview 不包含 android:entries 或类似属性。 RecyclerView 是 listview 的继承者,但仍然缺少条目属性和 onclicklistener

      这里是android官方文档链接

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

      本网站还介绍了 android:entries 属性。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-08
        相关资源
        最近更新 更多