【问题标题】:Android RecyclerView item click not working - MVVMAndroid RecyclerView 项目点击不起作用 - MVVM
【发布时间】:2020-07-11 16:39:01
【问题描述】:

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 类。

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 帖子。谢谢

【问题讨论】:

    标签: java android mvvm android-recyclerview android-viewmodel


    【解决方案1】:

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

    【讨论】:

    • 我为什么要删除 android:onClick="@{() -viewModel.itemClick(model)}"?
    【解决方案2】:

    问题已解决。我忘记在 recyclerview 适配器中绑定视图模型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多