让片段知道什么时候某个列表视图项目在屏幕上

时间:2018-05-19 05:57:58

标签: java android listview android-fragments

我有一个片段,其中包含一个带有自定义ArrayAdapter的ListView。当ArrayList中的某些元素滚动到并通过getView()显示时,我想在片段中运行一个特定的方法。

有没有办法让我的自定义ArrayAdapter直接在创建它的片段对象上调用方法?或者是否有一种方法让片段监听正在显示的某个数组元素?

这是我实例化ArrayAdapter的片段代码:

private void initAdapter() {
    infoViewAdapter infoAdapter = new infoViewAdapter(getActivity(), myInfo);

    infoListView = rootView.findViewById(R.id.list);

    infoListView.setAdapter(infoAdapter);
}

这是我的自定义适配器中的getView()

public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    View listItem = convertView;
    if(listItem == null) {
        listItem = LayoutInflater.from(myContext).inflate(R.layout.piece_of_info, parent, false);
    }

    myInfo currentInfo = getItem(position);

    TextView infoWordsText = listItem.findViewById(R.id.infoWordsView);
    TextView infoGotoText = listItem.findViewById(R.id.infoGoto);

    ImageView infoImage = listItem.findViewById(R.id.infoImageView);
    if (currentInfo.pic != -1) {
        // info block
        infoWordsText.setVisibility(View.VISIBLE);
        infoImage.setVisibility(View.VISIBLE);
        infoGotoText.setVisibility(View.GONE);

        infoWordsText.setText(currentInfo.words);
        infoImage.setImageResource(currentInfo.pic);
    } else {
        // category header
        infoWordsText.setVisibility(View.GONE);
        infoImage.setVisibility(View.GONE);
        infoGotoText.setVisibility(View.VISIBLE);

        infoGotoText.setText(currentInfo.words);

        // HERE I WOULD LIKE TO CALL A METHOD ON THE FRAGMENT THAT CREATED ME
        // OR HAVE THE FRAGMENT REALIZE A CERTAIN ARRAY ELEMENT HAS BEEN DISPLAYED
    }

    return listItem;
}

2 个答案:

答案 0 :(得分:0)

您可以使用回调。

public interface OnGetView {
   void execute(int position);
}

创建一个扩展ArrayAdapter的自定义适配器:

public class CustomAdapter extends ArrayAdapter {
    private OnGetView callback;

    public CustomAdapter(Context context, int resource, List<T> objects, OnGetView callback) {
       super(context, resource, objects);
       this.callback = callback;
    }

    @Override
    public View getView(..) {
       process...
       callback.execute(position); // Should use thread here to not block ui thread if your callback is long-running task
    }
}

初始化适配器时,通过该构造函数传递回调,然后进行设置。

// In your fragment
OnGetView myCallback = new OnGetView() {
    @Override
    void execute(int position) {
       ... your logic
    }
};

OnGetView myCallback = () -> { your logic};

然后将其传递给您的自定义数组适配器:

new CustomArrayAdapter(...., myCallback);

答案 1 :(得分:0)

我无法得到Manh让我工作的回调......目前它只是我的Java知识之外的一点点,但我能够得到Karan Modi在评论中建议的方法工作。除了我的代码已经传递到我的自定义ArrayAdapter构造函数的活动上下文之外,我还包含了对片段实例本身的引用:

infoViewAdapter infoAdapter = new infoViewAdapter(getActivity(), myInfo, this);

和我的构造函数:

infoViewAdapter(Context context, ArrayList<MordorInfo> list, FeaturedFragment cb) {
    super (context, 0, list);
    myContext = context;
    infoList = list;
    featuredCallback = cb;
}

然后调用片段的方法:

if (featuredCallback != null) {
    featuredCallback.setButtonCallback(currentInfo.gotoRef);
}

像魅力一样工作。这种方法有什么缺点吗?