我的 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/