Java Android RecyclerView项目单击不工作-MVVM
RecyclerView显示了所有数据,但项目单击不起作用。这里我附上我迄今为止所做的工作。为了更好地理解,我删除了所有不必要的代码 这是我的recyclerview项目xmlJava Android RecyclerView项目单击不工作-MVVM,java,android,mvvm,android-recyclerview,android-viewmodel,Java,Android,Mvvm,Android Recyclerview,Android Viewmodel,RecyclerView显示了所有数据,但项目单击不起作用。这里我附上我迄今为止所做的工作。为了更好地理解,我删除了所有不必要的代码 这是我的recyclerview项目xml <data> <variable name="model" type="com.xyz.abc.pojo.EmployeeListWithDesignationSetGet" /> <variable
<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>
我在其中编写click方法的ViewModel类
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);
}
}
公共类EmpListWithDesigViewModel扩展了ViewModel{
私有MutableLiveData MutableLiveData;
具有设计单击侦听器的私人雇员;
私人雇员(含存款人)雇员(含存款人);
公共void setListener(EmpListWithDesigClickListener侦听器){
this.listener=listener;
}
公共void init(){
if(mutableLiveData!=null){
返回;
}
empListWithDesigRepository=empListWithDesigRepository.getInstance();
mutableLiveData=empListWithDesigRepository.getEmpList();
}
公共可变LiveData getEmpList(){
返回可变的livedata;
}
公共无效项单击(EmployeeListWithDesignationSetGet EmployeeListWithDesignationSetGet){
listener.onItemClick(指定为setget的员工列表);
}
}
现在在活动中,我正在实现单击界面
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);
}
公共类EmployeeDesignationActivity扩展AppCompatActivity实现EmpListWithDesigClickListener{
私人RecyclerView mRv_RecyclerView;
私有列表数组列表;
具有DesigaDapter的私人员工名单;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u employee\u designation);
setViewReferences();
arrayList=新的arrayList();
employeeListWithDesigAdapter=新的employeeListWithDesigAdapter(此为arrayList);
mRv_recyclerView.setAdapter(带有设计适配器的员工列表);
EmpListWithDesigViewModel EmpListWithDesigViewModel=new ViewModelProvider(这是新的ViewModelProvider.AndroidViewModelFactory(getApplication()).get(EmpListWithDesigViewModel.class);
使用desigviewmodel.setListener(此)的emplist;
empListWithDesigViewModel.init();
empListWithDesigViewModel.getEmpList().observe(这是新的观察者(){
@凌驾
更改后的公共void(列出employeeListWithDesignationSetGets){
arrayList.addAll(带有指定集合的EmployeeList);
带有DesigaDapter.notifyDataSetChanged()的员工列表;
}
});
}
私有void setViewReferences(){
mRv\u recyclerView=findViewById(R.id.rv\u活动\u员工\u指定);
}
@凌驾
public void onItemClick(指定为SetGet的员工列表指定为SetGet的员工列表){
String phone=employeeListWithDesignationSetGet.getEmpPhone();
Intent Intent=新的Intent(Intent.ACTION_DIAL,Uri.parse(“电话:”+phone));
星触觉(意向);
}
}
如果我没有提供足够的信息,请原谅,这是我的第一篇SO帖子。谢谢您应该从
Linearlayout
中删除android:onClick=“@{()-viewModel.itemClick(model)}”
。同时添加以下属性
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
然后,您的项目布局将如下所示:
<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>
问题已修复。我忘了在recyclerview适配器中绑定viewmodel。为什么要删除android:onClick=“@{()-viewmodel.itemClick(model)}”?