RecyclerView показывает все данные, но щелчок по элементу не работает. Здесь я прилагаю то, что сделал до сих пор. Для лучшего понимания я удаляю весь ненужный код.
Это мой элемент recyclerview 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>
Класс ViewModel, в котором я написал метод click.
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);
}
}
Сейчас я использую интерфейс щелчка.
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);
}
}
Простите меня, если я не предоставил достаточно информации, это мой первый пост SO. Спасибо