Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

android:entries in recyclerview

I have used listview with entries attribute like below :

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

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" />

I want to know whether we have android:entries attribute in RecyclerView? Or any other attribute instead of entries?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
        }
    }
}

Then you have to use the DataBindingUtil methods for inflate the layout.

Inflate inside an Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DataBindingUtil.setContentView(this, R.layout.content_main);
}

Inflate inside a Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ContentMainBinding bindings = DataBindingUtil.inflate(inflater, R.layout.content_main, container, false);
    return bindings.getRoot();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...