2017-10-29 114 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

我明白了。谢谢 –