android:entries in recyclerview

前端 未结 4 1506
眼角桃花
眼角桃花 2020-12-04 19:55

I have used listview with entries attribute like below :



        
相关标签:
4条回答
  • 2020-12-04 20:10

    No,In android, Recyclerview doesn't hold android:entries or like attributes. RecyclerView is successor of listview but still missing entries attribute and onclicklistener

    here is android official documentation link

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

    This site also describes about the android:entries attribute.

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

    0 讨论(0)
  • 2020-12-04 20:19

    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();
    }
    
    0 讨论(0)
  • 2020-12-04 20:25

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

    REASON: As we know, RecyclerView won't inflate until we set a LayoutManager to it. Also, LayoutManager is necessary to inflate individual item views of RecyclerView. These individual item views are retrieved from the RecyclerView.Adapter. So, until you set both the LayoutManager and RecyclerView.Adapter to RecyclerView, you can't use RecyclerView.

    I hope this answer helps you.

    0 讨论(0)
  • 2020-12-04 20:30

    I'm afraid this is not possible out of the box, you can extend the RecyclerView and define your own custom attribute which accepts a string array, then you would populate your RecyclerView Adapter with these values.

    Check this link: http://developer.android.com/training/custom-views/create-view.html#customattr

    0 讨论(0)
提交回复
热议问题