Android MVVM架構實現RecyclerView列表詳解流程

效果圖

導入引用

導入Recyclerview依賴

 implementation 'androidx.recyclerview:recyclerview:1.1.0'

導入dataBinding引用

 dataBinding {
            enabled = true
        }

代碼解析

建立實體類

繼承被觀察者模式BaseObservable

@Bindable綁定字段

刷新數據 notifyPropertyChanged( BR.mName );//BR自動生成

public class Information extends BaseObservable {
    public String mName;
    public String mSex;
    public String mAge;
    public List<Information> informationList;
    public Information(){

    }
    public Information(String mName,String mSex,String mAge){
        this.mName = mName;
        this.mSex = mSex;
        this.mAge = mAge;
    }
    @Bindable
    public String getmAge() {
        return mAge;
    }
    @Bindable
    public String getmName() {
        return mName;

    }
    @Bindable
    public String getmSex() {
        return mSex;
    }
    /**
     * @param mName*/
    public void setmName(String mName) {
        this.mName = mName;
        notifyPropertyChanged( BR.mName );
    }
    @Bindable
    public List<Information> getInformationList() {
        return informationList;
    }

    /**
     * @param mSex */
    public void setmSex(String mSex) {
        this.mSex = mSex;
        notifyPropertyChanged( BR.mSex );
    }
   /**
    * @param mAge */
    public void setmAge(String mAge) {
        this.mAge = mAge;
        notifyPropertyChanged( BR.mAge );
    }
    /**
     * @param informationList */
    public void setInformationList(List<Information> informationList) {
        this.informationList = informationList;
        notifyPropertyChanged( BR.informationList );
    }
}

將視圖層與數據層綁定

@{information.mName}

建立RecyclerView子項

導入實體類

 <data>
        <variable
            name="information"
            type="com.franzliszt.mvvmrecyclerview.model.Information" />
    </data>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="information"
            type="com.franzliszt.mvvmrecyclerview.model.Information" />
    </data>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:background="#F5F5F5">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:background="@drawable/item_bg_style"
        android:gravity="center"
        android:paddingLeft="20dp">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:hint="名字"
            android:text="@{information.mName}"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:hint="性別"
            android:text="@{information.mSex}"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:hint="年齡"
            android:text="@{information.mAge}"/>
    </LinearLayout>

</LinearLayout>
</layout>

適配器

建立適配器

引入數據源

public MyRecyclerView(List<Information> informationList){
        this.informationList = informationList;
    }

插入佈局文件

 @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate( LayoutInflater.from( parent.getContext()) , R.layout.recyclerview_item,parent,false);
        return new ViewHolder( binding,mOnItemClickListener );
    }

設置數據

 @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Information information = informationList.get( position );
        holder.binding.setVariable( BR.information,informationList.get( position ));
        holder.binding.executePendingBindings();
    }

自定義ViewHolder類

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        ViewDataBinding binding = null;
        OnItemClickListener listener;
        public ViewHolder(@NonNull ViewDataBinding binding,OnItemClickListener listener) {
            super( binding.getRoot() );
            this.listener = listener;
            this.binding = binding;
            binding.getRoot().setOnClickListener( this );
        }
        public ViewDataBinding getBinding(){
            return binding;
        }

設置子項點擊事件

定義接口

public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

繼承View.OnClickListener點擊事件

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener

獲取接口實例

public ViewHolder(@NonNull ViewDataBinding binding,OnItemClickListener listener)

調用接口

 @Override
        public void onClick(View v) {
            if (listener != null){
                listener.onItemClick( v,getPosition() );
            }
        }

開放接口

public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

adapter全部代碼

public class MyRecyclerView extends RecyclerView.Adapter<MyRecyclerView.ViewHolder>{
    private List<Information> informationList;
    private OnItemClickListener mOnItemClickListener;
    public MyRecyclerView(List<Information> informationList){
        this.informationList = informationList;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate( LayoutInflater.from( parent.getContext()) , R.layout.recyclerview_item,parent,false);
        return new ViewHolder( binding,mOnItemClickListener );
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Information information = informationList.get( position );
        holder.binding.setVariable( BR.information,informationList.get( position ));
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return informationList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        ViewDataBinding binding = null;
        OnItemClickListener listener;
        public ViewHolder(@NonNull ViewDataBinding binding,OnItemClickListener listener) {
            super( binding.getRoot() );
            this.listener = listener;
            this.binding = binding;
            binding.getRoot().setOnClickListener( this );
        }
        public ViewDataBinding getBinding(){
            return binding;
        }

        @Override
        public void onClick(View v) {
            if (listener != null){
                listener.onItemClick( v,getPosition() );
            }
        }
    }
}

建立VM層

子項點擊事件的使用

實現OnItemClickListener接口

@Override
    public void InitListener(){
        adapter.setOnItemClickListener( new MyRecyclerView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText( context,"位數:"+position,Toast.LENGTH_SHORT ).show();
            }
        } );
    }

VM層代碼

public class ViewModel implements IViewModel{
    public Information information;
    public List<Information> mList = new ArrayList<>(  );
    private RecyclerView recyclerView;
    private Context context;
    private MyRecyclerView adapter;
    public ViewModel(Information information, RecyclerView recyclerView, Context context){
        this.information = information;
        this.recyclerView = recyclerView;
        this.context = context;
        InitRecyclerView();
        InitData();
        InitListener();
    }
    private void InitRecyclerView(){
        recyclerView.setLayoutManager( new LinearLayoutManager( context ) );
        adapter = new MyRecyclerView( mList );
        recyclerView.setAdapter( adapter );
    }
    @Override
    public void InitData() {
        for (int i = 0; i < 10; i++) {
            mList.add( new Information( "張三"+i,"男",i+"" ) );
            Log.d( "TAG",mList.get( i ).mName+"" );
        }
        information.setInformationList( mList );
    }
    @Override
    public void InitListener(){
        adapter.setOnItemClickListener( new MyRecyclerView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText( context,"位數:"+position,Toast.LENGTH_SHORT ).show();
            }
        } );
    }
}

數據與視圖交互

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        binding = DataBindingUtil.setContentView( this,R.layout.activity_main );
        binding.setViewmodel( new ViewModel(new Information(  ),binding.myRecyclerView,MainActivity.this) );
    }
}

到此這篇關於Android MVVM架構實現RecyclerView列表詳解流程的文章就介紹到這瞭,更多相關Android RecyclerView列表內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: