Android 如何使用数据绑定从适配器布局调用方法
我尝试使用Android 如何使用数据绑定从适配器布局调用方法,android,mvvm,data-binding,android-recyclerview,adapter,Android,Mvvm,Data Binding,Android Recyclerview,Adapter,我尝试使用onClick=“@{()->RegAdapter.getCallFunction()}”调用适配器中的函数,但它不起作用。 如何使用数据绑定概念获取特定的位置视图,因为我对这个概念还不熟悉,所以可以参考一下。 先谢谢你 这是RegistrationtActivity类 public class RegistrationOtpActivity extends AppCompatActivity { RecyclerView re
onClick=“@{()->RegAdapter.getCallFunction()}”
调用适配器中的函数,但它不起作用。
如何使用数据绑定概念获取特定的位置视图,因为我对这个概念还不熟悉,所以可以参考一下。
先谢谢你
这是RegistrationtActivity
类
public class RegistrationOtpActivity extends AppCompatActivity {
RecyclerView recyclerview;
RegOtpViewModel viewModel;
RegOtpAdapter adapter;
public ProgressBar progressbar;
ActivityRegBinding activityRegBinding;
LinearLayoutManager linearLayoutManager;
public boolean onLoading = false;
public boolean onLoading3 = false;
private int thresholdItem = 50;
public int totalcount = 1;
public String userId = "";
List<RegOtpPojoClass.Userlist> List;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityRegBinding =DataBindingUtil.setContentView(this, R.layout.activity_reg_);
recyclerview=activityRegBinding.rvRecyclerView;
Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Customer List");
viewModel = ViewModelProviders.of(this).get(RegOtpViewModel.class);
linearLayoutManager=new LinearLayoutManager(this);
recyclerview.setLayoutManager(linearLayoutManager);
recyclerview.addOnScrollListener ( recyclerViewOnScrollListener );
recyclerview.setHasFixedSize(true);
adapter=new RegOtpAdapter();
recyclerview.setAdapter(adapter);
activityRegBinding.progressbarloader.setVisibility(View.VISIBLE);
getCustomerList();
}
private void getCustomerList() {
viewModel.getCustomer(""+totalcount).observe(this, new Observer<List<RegOtpPojoClass.Userlist>>() {
@Override
public void onChanged(@Nullable List<RegOtpPojoClass.Userlist> employees) {
activityRegBinding.progressbarloader.setVisibility(View.GONE);
List=employees;
adapter.setCustomeData(getApplicationContext(),(ArrayList<RegOtpPojoClass.Userlist>) employees);
}
});
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && List.size() >= thresholdItem) {
if (!onLoading) {
onLoading = true;
List.add(null);
adapter.notifyItemInserted(List.size() - 1);
totalcount = totalcount + 1;
viewModel.getCustomer (""+totalcount);
}
}
}
}
};
}
公共类注册活动扩展了AppCompatActivity{
回收视图回收视图;
RegOtpViewModel视图模型;
RegOtpAdapter适配器;
公共ProgressBar ProgressBar;
ActivityRegBinding ActivityRegBinding;
直线布局经理直线布局经理;
公共布尔onload=false;
公共布尔onLoading3=false;
私有int阈值项=50;
公共整数总数=1;
公共字符串userId=“”;
名单;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
activityRegBinding=DataBindingUtil.setContentView(this,R.layout.activity\u reg\u1);
recyclerview=activityRegBinding.rvRecyclerView;
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle(“客户列表”);
viewModel=ViewModelProviders.of(this.get)(RegOtpViewModel.class);
linearLayoutManager=新的linearLayoutManager(此);
recyclerview.setLayoutManager(linearLayoutManager);
RecycleView.addOnScrollListener(RecycleServiceOnScrollListener);
recyclerview.setHasFixedSize(true);
适配器=新的RegOtpAdapter();
recyclerview.setAdapter(适配器);
activityRegBinding.progressbarloader.setVisibility(View.VISIBLE);
getCustomerList();
}
私有void getCustomerList(){
viewModel.getCustomer(“+totalcount).observe(这个,新的观察者(){
@凌驾
更改后的公共void(@Nullable List employees){
activityRegBinding.progressbarloader.setVisibility(View.GONE);
列表=员工;
setCustomeData(getApplicationContext(),(ArrayList)employees);
}
});
}
@凌驾
公共布尔onSupportNavigateUp(){
onBackPressed();
返回true;
}
private RecyclerView.OnScrollListener RecyclerServiceOnScrollListener=new RecyclerView.OnScrollListener(){
@凌驾
CrollStateChanged上的公共无效(RecyclerView RecyclerView,int newState){
super.onScrollStateChanged(recyclerView、newState);
}
@凌驾
已填空的公共空间(RecyclerView RecyclerView、int dx、int dy){
super.onScrolled(recyclerView、dx、dy);
如果(dy>0){
int visibleItemCount=linearLayoutManager.getChildCount();
int totalItemCount=linearLayoutManager.getItemCount();
int firstVisibleItemPosition=linearLayoutManager.findFirstVisibleItemPosition();
如果((visibleItemCount+firstVisibleItemPosition)>=totalItemCount&&List.size()>=thresholdItem){
如果(!正在加载){
onload=true;
List.add(空);
adapter.notifyItemInserted(List.size()-1);
totalcount=totalcount+1;
viewModel.getCustomer(“+totalcount”);
}
}
}
}
};
}
这是activity_reg.xml页面
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressbarloader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_gravity="center|top"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/dimen_5dp"/>
</LinearLayout>
</LinearLayout>
</layout>
这是适配器Cals
public class RegOtpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
ArrayList<RegOtpPojoClass.Userlist> userlists;
private static final int VIEW_TYPE_ITEM = 0;
private static final int VIEW_TYPE_LOADING = 1;
Context context;
public void setCustomeData(Context context, ArrayList<RegOtpPojoClass.Userlist> userlist) {
this.context=context;
this.userlists = userlist;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_ITEM){
CustomerListItemBinding customerListItemBinding =
DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.customer_list_item, parent, false);
return new MyViewHolder(customerListItemBinding);
}else{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_loading_item, parent, false);
LoadingViewHolder cvh = new LoadingViewHolder(view);
return cvh;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder) {
final MyViewHolder myViewHolder = (MyViewHolder) holder;
RegOtpPojoClass.Userlist currentStudent = userlists.get(position);
myViewHolder.customerListItemBinding.setCustomerList(currentStudent);
}
}
@Override
public int getItemCount() {
if(userlists==null) return 0; else return userlists.size();
}
@Override
public int getItemViewType(int position) {
return userlists.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
class MyViewHolder extends RecyclerView.ViewHolder {
private CustomerListItemBinding customerListItemBinding;
public MyViewHolder(@NonNull CustomerListItemBinding CustomerListItemBinding) {
super(CustomerListItemBinding.getRoot());
this.customerListItemBinding = CustomerListItemBinding;
}
}
public void getCallFunction(){
String phone = ""+userlists.get(0).getMobile();
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
context.startActivity(intent);
}
}
公共类RegOtpAdapter扩展了RecyclerView.Adapter{
ArrayList用户列表;
私有静态最终整数视图\类型\项=0;
私有静态最终整型视图\类型\加载=1;
语境;
public void setCustomeData(上下文上下文,ArrayList用户列表){
this.context=context;
this.userlists=userlist;
notifyDataSetChanged();
}
@非空
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
if(视图类型==视图类型项目){
CustomerListItemBinding CustomerListItemBinding=
DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.customer\u list\u项目,父项,false);
返回新的MyViewHolder(customerListItemBinding);
}否则{
View=LayoutFlater.from(parent.getContext())。充气(R.layout.layout\u装入
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="CustomerList"
type="com.kann.basiynadmin.registration.RegOtpPojoClass.Userlist"/>
<variable
name="RegAdapter"
type="com.kann.basiynadmin.registration.RegOtpAdapter"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/card_delivery_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/dimen_20dp"
android:fitsSystemWindows="true"
android:visibility="visible"
app:cardCornerRadius="2dp"
app:cardElevation="2dp">
<LinearLayout
android:id="@+id/relaLayName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="@dimen/dimen_20dp"
android:layout_marginBottom="@dimen/dimen_20dp"
android:layout_marginLeft="@dimen/dimen_10dp"
android:weightSum="2"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1.4"
android:layout_margin="@dimen/dimen_5dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_marginRight="@dimen/dimen_5dp"
android:background="@drawable/ic_local_phone_black_24dp"/>
<TextView
android:id="@+id/txt_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="@{CustomerList.mobile}"
android:textSize="16sp"
android:onClick="@{()-> RegAdapter.getCallFunction()}"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight=".6"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dimen_5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="OTP : "
android:textSize="16sp"
android:textColor="@color/black_near" />
<TextView
android:id="@+id/otp_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="@{CustomerList.verification_code}"
android:textSize="16sp"
android:textColor="@color/dark_green" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/grey_10"/>
</LinearLayout>
</layout>
public class RegOtpPojoClass {
@SerializedName("userlist")
public List<Userlist> userlist;
@SerializedName("status")
private String status;
public List<Userlist> getUserlist() {
return userlist;
}
public void setUserlist(List<Userlist> userlist) {
this.userlist = userlist;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public class Userlist {
@SerializedName("userid")
private String userid;
@SerializedName("mobile")
private String mobile;
@SerializedName("is_mobile_verified")
private String is_mobile_verified;
@SerializedName("verification_code")
private String verification_code;
public String getVerification_code() {
return verification_code;
}
public void setVerification_code(String verification_code) {
this.verification_code = verification_code;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
String getIs_mobile_verified() {
return is_mobile_verified;
}
public void setIs_mobile_verified(String is_mobile_verified) {
this.is_mobile_verified = is_mobile_verified;
}
}
}