【问题标题】: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?如何从其 Recycler View Adapter 调用 Activity 的方法?或者更好的是,如何从适配器访问活动的 UI 元素?
【发布时间】:2018-11-03 10:22:25
【问题描述】:

我在 ItemActivity 中有一个 Recycler View 及其适配器作为单独的类 ItemAdapter。现在根据所选项目的计数,我需要更新放置在 ItemActivity 中的按钮上的文本。如何完成?

注意:我已将 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 的值!请告诉我如何可能。谢谢!

【问题讨论】:

标签: java android android-recyclerview


【解决方案1】:

最简单的方法是让 Adapter 类成为包含在 Activity 类中的内部类。然后制作你需要的视图来操作一个全局变量。

【讨论】:

  • 谢谢!这是可能的,但我也想将该适配器用于不同活动中的另一个 Recycler View。
  • 接口就是这样。
【解决方案2】:

您可以执行以下操作

创建一个界面

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

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

public ItemPickerAdapter(上下文上下文, List itemList, ItemCountListner 监听器)

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

listener.onItemCountUpdate(count);

【讨论】:

    【解决方案3】:

    在您的 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 的简单方法

    【讨论】:

      【解决方案4】:

      在您的活动中更改适配器实例化:

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

      到这里:

      ItemPickerAdapter adapter = new ItemPickerAdapter(this, ItemsList);
      

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

      MainActivity mainActivity = (MainActivity) context;
      

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

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

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

      【讨论】:

      • 我试过了,但它不起作用... --ItemPickerAdapter adapter = new ItemPickerAdapter(this, ItemsList);-- 这里 'this' 被认为是 Firebase valueEventListener...我不'不知道为什么
      • 这段代码在哪里?不是在你的活动类里面吗?
      • 在我的活动中的方法内
      • 问题实际上不在于访问方法!这是关于从另一个类访问 Ui 元素
      • 你的意思是像TextViews之类的?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多