从附加的RecyclerView.Adapter

时间:2018-10-09 11:38:50

标签: android android-fragments android-recyclerview

我最近开始使用Android Studio 3.1.2和SDK 19编码我的第一个Android项目。

我的一个片段包含一个RecyclerView,并附加了一个自定义的RecyclerView.Adapter。在CardView上,适配器通过其ViewHolder获取,可以有一个按钮。目标是,如果按下按钮,则应调用我的fragment的方法,尽管它是Fragment的自定义子类的实例:

来自 RequestingFragment

public abstract class RequestingFragment extends Fragment implements RequestCallbacks {

    public final static void startRequest(final RequestOperation, String param) {
        //this is the guy i want to call
    }

    //these are the RequestCallbacks, they're all getting called in startRequest()
    public void onSuccess(JSONObject json, String parsingkey) { }

    public void onError() { }

    public void onFinished() { }

现在我的 RequestingFragment 之一包含一个RecyclerView,在其上附加了自定义的 ErrorCompactAdapter 。在Adapters ViewHolder内部,我在其中加载单个CardView的布局,这里有一个按钮,该按钮应从我的 RequestingFragment

中调用startRequest() onClick

来自 ErrorCompactAdapter

public class ErrorCompactAdapter extends RecyclerView.Adapter<ErrorCompactAdapter.ErrorCompactViewHolder> {

    private Context context;
    private ArrayList<Error> errors;

    public ErrorCompactAdapter(Context context, ArrayList<Error> errors) {
        this.context = context;
        this.errors = errors;
    }

    public void onBindViewHolder(ErrorCompactViewHolder, int position) {
        //...
        holder.errorTakeOverButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //here's were i'm stuck
            }
        });
        //...
    }
}

我的第一种方法是将 ErrorCompactAdapter context属性更改为 RequestingFragment ,以便可以对此调用startRequest()

private Context context; // private RequestingFragment attacher;

public void onClick(View v) {
    attacher.startRequest(/*params*/);
}

但是我非常不确定,如果包含RecyclerView的片段将是接收请求响应的片段,或者以某种方式“伪匿名”的片段将接收响应并且只是不执行任何操作,那么。如果这是正确的道路,有人可以启发我吗?谢谢。

2 个答案:

答案 0 :(得分:3)

在您的ErrorCompactAdapter类的构造函数中传递Fragment。这对我来说是我想要的方式。我有同样的问题。

package main

import (
    "io"
    "log"
)

func main() {
    cname := models.Category{
        CategoryName: c.Name, // this to be picked from loop
    }
    bb, err := models.CategoryByID(db, 4)
    if err != nil {
        cname.Save(db)
        return cname
    }

    for {
        var c CatalogCSV

        //CheckorCreate when the loop starts and use the value in the rest of the loop

        if err := dec.Decode(&c); err == io.EOF {
            break
        } else if err != nil {
            log.Fatal(err)
        }

        a := models.Catalog{
            CatalogName:         c.Catalogname,
            CatalogCategoryName: c.Categoryname, // This is part of the initial CSV file
            CataloCategoryID:    b.ID,           // this is to be picked from CategoryByID
        }

        // save author to database
        err = a.Save(db)
        if err != nil {
            log.Fatal(err)
        }
    }
}

答案 1 :(得分:0)

从适配器调用片段函数的两种方法。 首先,原始/标准方法是通过接口实现。

  1. 在适配器类中创建接口和方法(希望在fragment中调用)
  2. 实现在片段类和回调中将在其中实现
  3. 调用适配器时,将侦听器的实例发送到适配器,并通过该实例调用接口方法

代码是i kotlin(在适配器类中)

interface ApiEventListener{
            fun setData(userToFollowModel: UserToFollowModel?)  {

        }
}

和片段类

 ProfileApiCalls.ApiEventListener listener;
 profileApiCalls.getUserToFollow(getContext(),listener)

将片段实例发送到适配器并通过该实例调用frament方法的第二种方法。喜欢

 fragment.callMethodname()