简体   繁体   中英

Data binding doesn't work correctly on button when there is a recyclerview too

I have a Button and A RecyclerView on my MainActivity. I used retrofit to fill my recyclerView with data which was working fine until I wrote data Binding event for the Button. Now the Button works but the data is gone from the 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();
    }
}

I have checked. Once I comment the Data Binding part, the data will come back to the RecyclerView. Note: the RecyclerView just contains a simple TextView

I don't think you are using DataBinding properly. You don't need to use setContentView if you are using dataBinding. Please try the following edit and let me know what is the output.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM