带数据绑定的Recyclerview无法检测onClick操作

时间:2017-11-04 17:44:22

标签: java android mvvm data-binding android-recyclerview

我正在开发一个小项目,我正在尝试使用数据绑定到已存在的应用程序来实现MVVM,并且我遇到了onClick事件。基本上我试图在ViewModel中调用的方法被突出显示为黄色,因此绑定就位(这将是"onCLickS"方法)。我在ViewModel中放置的@BindingAdapter来检查绑定也可以正常工作。

我试图将android:onClick基本上放在布局中的任何地方,但它不起作用。已经尝试了我能在网上找到的所有东西。

在Gradle中启用数据绑定。 使用recyclerview的主要活动:

public class MainListActivity extends AppCompatActivity {

private MainListAdapter myAdapter;
private MainListViewModel mainListViewModel;
private List<MainListModel> myGridList;
private RecyclerView recyclerView;
private ActivityMainListBinding binding;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main_list);

    binding= DataBindingUtil.setContentView(this, R.layout.activity_main_list);
    mainListViewModel = new MainListViewModel();

    recyclerView = (RecyclerView) findViewById(R.id.myRecView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    myGridList = mainListViewModel.passGridList();

    myAdapter = new MainListAdapter(myGridList);
    recyclerView.setAdapter(myAdapter);
    binding.setViewModel(mainListViewModel);
    binding.setAdapter(myAdapter);

    }
}

我的行项目的活动:

public class MainListItemActivity extends AppCompatActivity {

private MainListViewModel mainListViewModel;
private MainListItemBinding binding;

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main_list);

    binding= DataBindingUtil.setContentView(this, R.layout.main_list_item);
    mainListViewModel = new MainListViewModel();

    binding.setViewModel(mainListViewModel);


    }
}

视图模型:

public class MainListViewModel extends BaseObservable{

    private final ObservableField<String> text;
//    private final Context context;
    public ObservableArrayList<MainListModel> gridList;
    iMainListItems iMainListItems;


    private static final String TAG = "MainListViewModel";

    public MainListViewModel(){

        text = new ObservableField<String>();
        gridList = new ObservableArrayList<MainListModel>();
//        this.context = context;
        this.iMainListItems = iMainListItems;

    }

    @BindingAdapter({"android:newActivity"})
    public static void lounchNewActivity(View view, String category) {
        Intent intent = new Intent(view.getContext(), LinkListActivity.class);
        intent.putExtra("category",category);
        view.getContext().startActivity(intent);
    }
    public List<MainListModel> passGridList(){

        gridList.add(new MainListModel(R.drawable.books, "Books"));
        gridList.add(new MainListModel(R.drawable.business,  "Business"));
        gridList.add(new MainListModel(R.drawable.cinema, "Cinema"));
        gridList.add(new MainListModel(R.drawable.politics,  "Politics"));
        gridList.add(new MainListModel(R.drawable.technology,  "Technology"));
        gridList.add(new MainListModel(R.drawable.gaming,  "Gaming"));
        gridList.add(new MainListModel(R.drawable.cooking,  "Cooking"));
        gridList.add(new MainListModel(R.drawable.datascience,  "Data Science"));
        gridList.add(new MainListModel(R.drawable.design,  "Design"));
        gridList.add(new MainListModel(R.drawable.diy,  "DIY"));
        gridList.add(new MainListModel(R.drawable.sports,  "Sports"));

        notifyPropertyChanged(BR.model);

        return gridList;

    }

    public  void onClickL(){

        Log.e(TAG,"Long Clicked!!!!!!!!!!!1");

    }

    public  void onClickS(View view,String category){


        Intent intent = new Intent(view.getContext(), LinkListActivity.class);
        intent.putExtra("category",category);
        view.getContext().startActivity(intent);

        Log.d(TAG,"Clicked!!!!!!");

    }

}

行项目布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="android.view.View" />
        <variable
            name= "model"
            type = "com.example.psotor.zbiq.models.MainListModel"/>
        <variable
            name= "viewModel"
            type = "com.example.psotor.zbiq.viewmodels.MainListViewModel"/>
    </data>
            <android.support.v7.widget.CardView
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:paddingTop="26dp"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:paddingBottom="2dp"

                >
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                   >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="#ffffff"
                        android:background="@color/colorPrimaryFade"
                        android:layout_alignParentBottom="true"
                        android:textSize="24sp"
                        android:textAlignment="center"
                        android:elevation="2dp"
                        android:text="@{model.categoryName}"
                        android:id="@+id/gridName"

                        />
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/gridImage"
                        android:scaleType="centerCrop"
                        android:background="@color/colorPrimary"
                        android:src="@{model.categoryImage}"
                        android:onClick="@{(v) -> viewModel.onClickS(v,model.categoryName)}"
                        android:clickable="true"
                        android:focusable="true"
                    />
                </RelativeLayout>
    </android.support.v7.widget.CardView>
</layout>

适配器:

public class MainListAdapter extends RecyclerView.Adapter<MainListViewHolder> {


    private List<MainListModel> gridList;


    public MainListAdapter(List<MainListModel> gridList) {
        this.gridList = gridList;
    }

    @Override
    public MainListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        MainListItemBinding binding = MainListItemBinding.inflate(inflater,parent,false);
        return new MainListViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(MainListViewHolder holder,int position) {

        final MainListModel mainListModel = gridList.get(position);
        holder.bind(mainListModel);
    }

    @Override
    public int getItemCount() {

        return (null != gridList ? gridList.size() : 0);
    }
    @BindingAdapter({"android:src"})
    public static void setImageViewResource(ImageView imageView, int resource) {
        Picasso.with(imageView.getContext())
                .load(resource)
                .centerCrop()
                .resize(500,500)
                .into(imageView);
    }

}

ViewHolder:

public class MainListViewHolder extends RecyclerView.ViewHolder  {

    private final MainListItemBinding mBinding;

    public MainListViewHolder(MainListItemBinding binding) {
        super(binding.getRoot());
        this.mBinding = binding;

    }
    public void bind (MainListModel mainListModel){
        mBinding.setModel(mainListModel);
        mBinding.executePendingBindings();
    }
}

1 个答案:

答案 0 :(得分:0)

调试你的数据绑定。

  • 在Android Studio中启用数据绑定调试
    • 打开设置&gt;编辑&gt;数据绑定&gt; “编译器生成的参考代码
  • 构建项目。
  • 打开布局的数据绑定文件(应该类似于 { xtype:'button', iconCls:'fa fa-bars', } //fa fa-bars is the link from the icon source, fa fa-bars represents bars icon, try this example in your code for show the icon
  • 找到MainListItemBinding方法调用
  • 在那里放置一个断点并在它所居住的方法的顶部(类似onClickS
  • 调试应用程序并等待顶部断点被点击,然后逐步调整到应该调用方法的位置。

如果您仍然无法弄明白,请在调试中发布您的发现。