2017-10-29 144 views
1

我的MainActivity上有一個按鈕和一個RecyclerView。我使用retrofit來填充我的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(); 
    } 
} 

我已經檢查。一旦我評論數據綁定部分,數據將回到RecyclerView。注意:RecyclerView只包含一個簡單的TextView

回答

2

我不認爲你正在使用DataBinding。如果您使用dataBinding,則不需要使用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(); 
    } 
} 
+0

我明白了。謝謝 –