gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:15:32 25 4
gpt4 key购买 nike

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);
}
}
如果我没有提供足够的信息,请原谅我,这是我的第一篇 SO 帖子。谢谢

最佳答案

您应该删除 android:onClick="@{() -viewModel.itemClick(model)}"来自 Linearlayout .还要添加以下属性。

            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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com