android MVVM 개발 프레임워크 - (3) DataBinding은 RecyclerView에 적용

android MVVM 개발 프레임워크 - (1) DataBinding 기본 응용 프로그램
android MVVM 개발 프레임워크 - (2) DataBinding은ListView에 적용
만약 mvvm나 데이터 binding에 익숙하지 않다면, 먼저 앞의 두 편의 블로그를 보러 가는 것을 권장합니다
1,item 레이아웃은 전편의listview 레이아웃을 그대로 사용하면 여기에 붙이지 않습니다
2, recyclervice 레이아웃

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <import type="com.lh.mvvm.adapter.RecyclerViewAdapter" />

        <variable
            name="adapter"
            type="RecyclerViewAdapter" />
        
        <variable
            name="image"
            type="int" />
    data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_test"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.1"
            android:background="@{image}"
            app:adapter="@{adapter}" />
    LinearLayout>
layout>

3,RecyclerViewHolder
public class RecyclerViewHolder extends RecyclerView.ViewHolder {

    //     RecyclerView.ViewHolder    view   ,            databinding
    ItemMvvmBinding itemMvvmBinding;

    public RecyclerViewHolder(ItemMvvmBinding itemMvvmBinding) {
        super(itemMvvmBinding.getRoot());
        this.itemMvvmBinding = itemMvvmBinding;
    }

    public ItemMvvmBinding getBinding() {
        return itemMvvmBinding;
    }

    public void setBinding(ItemMvvmBinding itemMvvmBinding) {
        this.itemMvvmBinding = itemMvvmBinding;
    }
}

4,RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> implements View.OnClickListener {
    private List data;
    /**
     *     listview itemclick  ,recyclerview  itemclick
     */
    private OnItemClickListener mOnItemClickListener;


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

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

    public RecyclerViewAdapter(List data) {
        this.data = data;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemMvvmBinding itemMvvmBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_mvvm, parent, false);
        itemMvvmBinding.getRoot().setOnClickListener(this);
        return new RecyclerViewHolder(itemMvvmBinding);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        ItemMvvmBinding itemMvvmBinding = holder.getBinding();
        UserBean userBean = data.get(position);
        itemMvvmBinding.setUser(userBean);
        // position   itemView Tag ,         
        itemMvvmBinding.getRoot().setTag(position);
        itemMvvmBinding.btnUpdate.setOnClickListener(new OnBtnClickListener(1, userBean));
        itemMvvmBinding.btnDelete.setOnClickListener(new OnBtnClickListener(2, position));
        //       
        itemMvvmBinding.executePendingBindings();
    }

    private class OnBtnClickListener implements View.OnClickListener {
        private int stats;//1,  ;2,  
        private UserBean userBean;
        private int position;

        OnBtnClickListener(int stats, UserBean userBean) {
            this.stats = stats;
            this.userBean = userBean;
        }

        OnBtnClickListener(int stats, int position) {
            this.stats = stats;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (stats) {
                case 1:
                    userBean.userName.set("      ");
                    break;
                case 2:
                    data.remove(position);
                    notifyDataSetChanged();
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null)
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
    }
}

5,RecyclerViewActivity
public class RecyclerViewActivity extends Activity {

    ActivityRecyclerViewBinding recyclerViewBinding;
    ArrayList data;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        recyclerViewBinding = DataBindingUtil.setContentView(this, R.layout.activity_recycler_view);
        data = initData();
        //    recyclerview layoutmanager,        
        //           ,           ,       
        //new GridLayoutManager(this,3,GridLayoutManager.HORIZONTAL,false)); //    
        //new LinearLayoutManager(recyclerView.getContext(), LinearLayoutManager.VERTICAL, true));
        //new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));//   
        recyclerViewBinding.recyclerTest.setLayoutManager(new LinearLayoutManager(this, GridLayoutManager.VERTICAL, false));
        //      
        RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(data);
        //     
        recyclerViewBinding.setAdapter(recyclerViewAdapter);
        //item     
        recyclerViewAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(RecyclerViewActivity.this, "  :" + position, Toast.LENGTH_LONG).show();
            }
        });
        //      view    
        recyclerViewBinding.setImage(getResources().getColor(R.color.colorAccent));
    }

    //       
    private ArrayList initData() {
        ArrayList list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            UserBean userBean = new UserBean();
            userBean.userId.set(i);
            userBean.userName.set(i + "aaa");
            userBean.userAge.set(18 + i);
            userBean.userSex.set(i % 2 == 0 ? 1 : 0);
            list.add(userBean);
        }
        return list;
    }
}

recyclerview와 데이터 binding 사이의 귀속은 이렇다
데모 다운로드

좋은 웹페이지 즐겨찾기