Git Product home page Git Product logo

databinding-librar-recyclerview-all-purpose-adapter's Introduction

RecyclerView万能的Adapter

如果使用了Data Binding Library框架,并使用了RecyclerView.可以使用这个Adapter
不用在为每一个不同的布局写单独的Adapter

功能

  • 添加HeaderView和FooterView
  • Item的Click事件处理
  • 也可以添加额外的逻辑处理

使用

RecyclerView.setAdapter

item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>
        <import type="android.view.View"/>
        <variable
            name="yourVariable"
            type="yourpacket.mode.XXX"/>

    </data>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
          .....................
          android:text="@{yourVariable.xxx}"
          .....................
          >
    </LinearLayout>

</layout>
DataBindingRecyclerViewAdapter adapter = new DataBindingRecyclerViewAdapter
(mContext,R.layout.item,BR.yourVariable,variableList);
mRecyclerView.setAdapter(adapter);

setOnItemClick

adapter.setOnItemCkickListener(new DataBindingRecyclerViewAdapter.ItemClickListener(){
                        @Override
                        public void itemClick(View view, int position) {
                            //..................
                        }
                      });

addHeader And addFooter

mRecyclerView.setAdapter(adapter);
.....
LayoutInflater inflater = LayoutInflater.from(getContext());
View footerView = inflater.inflate(R.layout.recycler_view_load_more,binding.recyclerView,false);
adapter.addFooterView(footerView);
adapter.notifyDataSetChanged();
LayoutInflater inflater = LayoutInflater.from(getContext());
View footerView = inflater.inflate(R.layout.recycler_view_load_more,binding.recyclerView,false);
adapter.addFooterView(footerView);
mRecyclerView.setAdapter(adapter);

额外的逻辑处理

如果只给item添加一个variable还不够的话可以另外添加

adapter.setOnBindingViewHolderListener(new DataBindingRecyclerViewAdapter.OnBindingViewHolderListener() {
                            @Override
                            public void onHolderBinding(DataBindingRecyclerViewAdapter.DataBindingViewHolder holder, int position) {
                                ViewDataBinding binding = DataBindingUtil.getBinding(holder.itemView);
                                binding.setVariable(BR.presenter,new MyPresenter());

                            }
                        });

item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>
        <import type="android.view.View"/>
        <variable
            name="yourVariable"
            type="yourpacket.mode.XXX"/>
        <variable
              name="presenter"
              type="yourpacket.MyPresenter"/>
    </data>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
          .....................
          android:text="@{yourVariable.xxx}"
          .....................
          >
          <Button
              android:id="@+id/txtEdit"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"              
              android:visibility="@{userCode.equals(story.createUCode)?View.VISIBLE:View.GONE}"
              android:onClick="@{(view)->presenter.edit(view,yourVariable)}"
              android:text="EDIT"/>       
          <Button
            android:id="@+id/txtDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"            
            android:visibility="@{userCode.equals(story.createUCode)?View.VISIBLE:View.GONE}"
            android:onClick="@{(view) -> presenter.delete(view,yourVariable)}"
            android:text="DELETE"/>

    </LinearLayout>

</layout>

databinding-librar-recyclerview-all-purpose-adapter's People

Contributors

garveydou avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.