java - Android RecyclerView 项目点击不起作用 - MVVM

标签 java android mvvm android-recyclerview android-viewmodel

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>

我在其中编写了 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);
}
}

现在在 Activity 中我正在实现点击界面。

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);
}

}

如果我没有提供足够的信息,请原谅我,这是我的第一篇帖子。谢谢

最佳答案

您应该从 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>

关于java - Android RecyclerView 项目点击不起作用 - MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62851913/

相关文章:

android - 如何在我们的 Android 应用程序中单击按钮打开 Android 手机的设置

java - 为什么直接内存 'array' 的清除速度比普通的 Java 数组要慢?

java - 从网站解析 JSON

java - 如何运行Web服务器上的Jar

java - Retrofit 不包含参数

android - 如何在 Android OpenGL ES 中应用拖放和缩放

java - 在资源路径中找不到 JNA native 支持 (/com/sun/jna/linux-arm/libjnidispatch.so)

android - 使用 kotlin 在 android 中与底部工作表进行数据绑定(bind)

c# - "FreshIOC.Container.Register"有什么作用?

c# - 如何处理 MVVM 中并行任务的 ObservableCollection<> 结果?