Android RecyclerView item click not working – MVVM

The RecyclerView is showing all data, but the item click is not working. Here I am attaching what I have done so far. For better understanding I am removing all the unnecessary code.

This is my recyclerview item xml.

<data>     <variable         name="model"         type="com.xyz.abc.pojo.EmployeeListWithDesignationSetGet" />      <variable         name="viewModel"         type="com.xyz.abc.viewmodels.EmpListWithDesigViewModel" />  </data>         <LinearLayout             android:id="@+id/ll_details"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:gravity="center"             android:clickable="true"             android:focusable="true"             android:onClick="@{() -> viewModel.itemClick(model)}">              <TextView                 android:id="@+id/tv_show_details"                 android:layout_width="70dp"                 android:layout_height="30dp"                 android:text="Show"                 android:textColor="#FFFFFF" />         </LinearLayout> 

The ViewModel class where I have written the click method.

public class EmpListWithDesigViewModel extends ViewModel { private MutableLiveData<List<EmployeeListWithDesignationSetGet>> mutableLiveData; private EmpListWithDesigClickListener listener; private EmpListWithDesigRepository empListWithDesigRepository;  public void setListener(EmpListWithDesigClickListener listener) {     this.listener = listener; }  public void init() {     if (mutableLiveData != null) {         return;     }     empListWithDesigRepository = EmpListWithDesigRepository.getInstance();     mutableLiveData = empListWithDesigRepository.getEmpList(); }  public MutableLiveData<List<EmployeeListWithDesignationSetGet>> getEmpList() {     return mutableLiveData; }  public void itemClick(EmployeeListWithDesignationSetGet employeeListWithDesignationSetGet) {     listener.onItemClick(employeeListWithDesignationSetGet); } } 

Now in activity I am implementing the click interface.

public class EmployeeDesignationActivity extends AppCompatActivity implements EmpListWithDesigClickListener {  private RecyclerView mRv_recyclerView; private List<EmployeeListWithDesignationSetGet> arrayList; private EmployeeListWithDesigAdapter employeeListWithDesigAdapter;  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_employee_designation);      setViewReferences();     arrayList = new ArrayList<>();      employeeListWithDesigAdapter = new EmployeeListWithDesigAdapter(this,arrayList);     mRv_recyclerView.setAdapter(employeeListWithDesigAdapter);      EmpListWithDesigViewModel empListWithDesigViewModel = new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(EmpListWithDesigViewModel.class);     empListWithDesigViewModel.setListener(this);     empListWithDesigViewModel.init();     empListWithDesigViewModel.getEmpList().observe(this, new Observer<List<EmployeeListWithDesignationSetGet>>() {         @Override         public void onChanged(List<EmployeeListWithDesignationSetGet> employeeListWithDesignationSetGets) {             arrayList.addAll(employeeListWithDesignationSetGets);             employeeListWithDesigAdapter.notifyDataSetChanged();         }     }); }  private void setViewReferences(){     mRv_recyclerView = findViewById(R.id.rv_activity_employee_designation); }    @Override public void onItemClick(EmployeeListWithDesignationSetGet employeeListWithDesignationSetGet) {     String phone = employeeListWithDesignationSetGet.getEmpPhone();     Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" +  phone));     startActivity(intent); } 

}

Pardon me if I have not provided enough info, this is my first SO post. Thanks

Add Comment
2 Answer(s)

You should remove the android:onClick="@{() -viewModel.itemClick(model)}" from Linearlayout. Also add the below properties.

            android:clickable="false"             android:focusable="false"             android:focusableInTouchMode="false" 

Then your item layout will be as below:

        <LinearLayout             android:id="@+id/ll_details"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:gravity="center"             android:clickable="false"             android:focusable="false"             android:focusableInTouchMode="false"             >              <TextView                 android:id="@+id/tv_show_details"                 android:layout_width="70dp"                 android:layout_height="30dp"                 android:text="Show"                 android:textColor="#FFFFFF" />         </LinearLayout> 
Add Comment

Problem fixed. I forgot to bind the viewmodel in recyclerview adapter.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.