繁体   English   中英

如何从其 Recycler View Adapter 调用 Activity 的方法? 或者更好,如何从适配器访问活动的 UI 元素?

[英]How do i call a method of an Activity from its Recycler View Adapter? Or better,How to access the UI elements of an activity from the adapter?

我在 ItemActivity 和它的 Adapter 中有一个 Recycler View 作为一个单独的类 ItemAdapter.Now 根据所选项目的计数,我需要更新放置在 ItemActivity.How 中的按钮上的文本?

注意:我已将 getApplicationContext() 作为上下文传递给适配器。它传递了我在清单中设置的 GlobalValue 上下文。

<application
    android:name=".GlobalValues"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:theme="@style/AppTheme">

以下是我的 ItemActivity.xaml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    tools:context=".ItemPickerActivity">

    <TextView
        android:id="@+id/itemstext"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose Items"
        android:fontFamily="@font/exo_semibold"
        android:textColor="@color/dark_grey"              
        />    
    <android.support.v7.widget.RecyclerView
        android:layout_marginHorizontal="3dp"
        android:id="@+id/pickerRecview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/itemstext"
        android:layout_marginTop="7dp" />

    <RelativeLayout
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:padding="10dp"
        android:id="@+id/checkout"
        android:clickable="true"
        android:background="@color/go_green"
        >

        <TextView
            android:id="@+id/Total"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rs XXX"
            android:layout_marginLeft="5dp"
            android:textSize="18sp"
            android:textColor="@color/white"
            android:layout_centerVertical="true"
            android:fontFamily="@font/exo_semibold"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Checkout"
            android:textColor="@color/white"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:textSize="22sp"
            android:fontFamily="@font/exo_semibold"
            />

        <TextView
            android:id="@+id/TotalItems"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:text="xx Items"
            android:fontFamily="@font/exo_semibold"
            android:textColor="@color/white"
            android:textSize="18sp" />   
    </RelativeLayout>        
</RelativeLayout>

这就是我实例化适配器对象的方式

ItemPickerAdapter adapter = new ItemPickerAdapter(getApplicationContext(), ItemsList);

            recyclerView.setAdapter(adapter);

我需要从适配器更改我的 ItemActitvity 中 ItemCount 的值!请告诉我这是怎么可能的。谢谢!

最简单的方法是使 Adapter 类成为包含在 Activity 类中的内部类。 然后制作你需要操作的全局变量的View。

将活动中的适配器实例化从:

ItemPickerAdapter adapter = new ItemPickerAdapter(getApplicationContext(), ItemsList);

对此:

ItemPickerAdapter adapter = new ItemPickerAdapter(this, ItemsList);

这意味着您传递活动的上下文而不是应用程序的上下文。
然后在您的适配器类中声明:

MainActivity mainActivity = (MainActivity) context;

MainActivity替换为您的活动的类名,并将context替换为 Context 的适配器类构造函数的参数名称。

现在,您可以通过以下方式访问活动中的每个公共方法、属性和视图:

mainActivity.something

Edit1 :在您的活动类中使用变量

Context mainContext = this;

然后实例化适配器:

ItemPickerAdapter adapter = new ItemPickerAdapter(mainContext, ItemsList);

Edit2 :如果您想从适配器访问您的活动的 UI 元素,例如TextView ,请在活动类中创建一个公共方法:

public void setTextViewText(String text) {
    myTextView.setText(text);
}

并从适配器调用它:

mainActivity.setTextViewText("some text");

您可以执行以下操作

创建一个界面

public interface ItemCountListner {
  void onItemCountUpdate(int count);
}

在您的活动中实现接口并将引用传递给您的适配器

class MainActivity implements ItemCountListner {

---------
ItemPickerAdapter adapter = new ItemPickerAdapter(MainActivity.this, ItemsList, this);
---------

//implemented method
void onItemCountUpdate(int count){
 // update your ui
}
}

在适配器构造函数中添加参数

公共 ItemPickerAdapter(上下文上下文,列表 itemList,ItemCountListner 侦听器)

通过调用更新您的用户界面

listener.onItemCountUpdate(count);

在 RecyclerView Adapter 中,您可以定义自己的 viewModel,在其中,您可以将 onClick 附加到您想要的任何组件上。 例如:

 public class MyViewHolder extends RecyclerView.ViewHolder
    {
        private TextView eachFeaturesName;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            eachFeaturesName = itemView.findViewById(R.id.eachFeaturesName);
            }
    }

在 onBindViewHolder 函数中,您可以执行以下操作:

public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int position) {

//myViewHolder.eachFeaturesName.setonViewClickListener(...)

}

这是在 RecyclerView 上使用 onClickListener 的简单方法

暂无
暂无

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

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