简体   繁体   English

Android RecyclerView item click not working - MVVM

[英]Android RecyclerView item click not working - MVVM

The RecyclerView is showing all data, but the item click is not working. RecyclerView 显示所有数据,但项目单击不起作用。 Here I am attaching what I have done so far.在这里,我附上我到目前为止所做的事情。 For better understanding I am removing all the unnecessary code.为了更好地理解,我删除了所有不必要的代码。

This is my recyclerview item xml.这是我的 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>

The ViewModel class where I have written the click method. ViewModel class 我写了点击方法。

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

Now in activity I am implementing the click interface.现在在活动中,我正在实现点击界面。

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

} }

Pardon me if I have not provided enough info, this is my first SO post.如果我没有提供足够的信息,请原谅我,这是我的第一个 SO 帖子。 Thanks谢谢

You should remove the android:onClick="@{() -viewModel.itemClick(model)}" from Linearlayout .您应该从 Linearlayout 中删除android:onClick="@{() -viewModel.itemClick(model)}" Linearlayout Also add the below properties.还要添加以下属性。

            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"

Then your item layout will be as below:然后您的项目布局将如下所示:

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

Problem fixed.问题已解决。 I forgot to bind the viewmodel in recyclerview adapter.我忘了在 recyclerview 适配器中绑定视图模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM