java - 当也有 recyclerview 时,数据绑定(bind)无法在按钮上正常工作

标签 java android xml android-databinding

我的 MainActivity 上有一个 Button 和一个 RecyclerView。在我为按钮编写数据绑定(bind)事件之前,我使用改造来填充我的 recyclerView 数据,这些数据工作正常。现在 Button 可以工作了,但是数据从 recyclerView 中消失了!

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.aminmemariani.apps.supportrequest.MainActivity">
    <data>
        <variable name="onReqCall" type="com.aminmemariani.apps.supportrequest.MainActivity"/>
    </data>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/addNewReq"
                android:layout_alignParentStart="true"
                android:scrollbars="vertical"
                tools:listitem="@layout/raw_request"
                android:layout_alignParentLeft="true" />

            <Button
                android:id="@+id/addNewReq"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentStart="true"
                android:layout_margin="10dp"
                android:background="@drawable/gradient"
                android:onClick="@{() -> onReqCall.onNewReqClick()}"
                android:text="+"
                android:textColor="@android:color/background_light"
                android:textSize="24sp"
                android:textStyle="bold"
                android:layout_alignParentRight="true"
                android:layout_alignParentLeft="true" />
        </RelativeLayout>
</layout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerAdapter recyclerAdapter;
    private List<Request> requests;

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

        recyclerView = findViewById(R.id.recyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);

        Call<List<Request>> call = apiInterface.getRequests();
        call.enqueue(new Callback<List<Request>>() {
            @Override
            public void onResponse(@NonNull Call<List<Request>> call, @NonNull Response<List<Request>> response) {
                requests = response.body();
                recyclerAdapter = new RecyclerAdapter(MainActivity.this, requests);
                recyclerView.setAdapter(recyclerAdapter);
            }
            @Override
            public void onFailure(@NonNull Call<List<Request>> call, @NonNull Throwable t) {}
        });
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setOnReqCall(this);
    }
    public void onNewReqClick(){
        Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.new_request);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }
}

我已经检查过了。一旦我评论了数据绑定(bind)部分,数据就会返回到 RecyclerView。注意:RecyclerView 只包含一个简单的 TextView

最佳答案

我认为您没有正确使用 DataBinding。如果您使用的是数据绑定(bind),则无需使用 setContentView。请尝试以下编辑,让我知道输出是什么。

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerAdapter recyclerAdapter;
    private List<Request> requests;
    private  ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        recyclerView = binding.recyclerView;

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);

        Call<List<Request>> call = apiInterface.getRequests();
        call.enqueue(new Callback<List<Request>>() {
            @Override
            public void onResponse(@NonNull Call<List<Request>> call, @NonNull Response<List<Request>> response) {
                requests = response.body();
                recyclerAdapter = new RecyclerAdapter(MainActivity.this, requests);
                recyclerView.setAdapter(recyclerAdapter);
            }
            @Override
            public void onFailure(@NonNull Call<List<Request>> call, @NonNull Throwable t) {}
        });
        binding.setOnReqCall(this);
    }
    public void onNewReqClick(){
        Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.new_request);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }
}

关于java - 当也有 recyclerview 时,数据绑定(bind)无法在按钮上正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46997482/

相关文章:

java - 如何修复 Checkstyle 空 block 警告

android - 如何在android中用不透明度填充矩形

xml - 在 Windows Phone 商店上传应用程序时出错

c# - 通过linq获取一个属性的集合

java - while循环不等待if语句完成

java - 如何使用代码生成跟踪 POJO 的状态

java - 为不同的 xml 子标签生成 xsd 和 Java 类 - JAXB

android - 为什么我的 Android 设备上的路由表有两个几乎相等的条目(不同指标)?

android - RecyclerView 适配器不适用于 butterknife

xml - Shopify XML API 响应 - 无效的元素名称 : 3DSecureStatus