startactivity之后的接口回调

时间:2018-09-20 09:52:01

标签: android android-recyclerview interface recycler-adapter android-viewholder

我有一个片段,其中包含recyclerview及其产品。单击“行”时,将打开一个新活动(SuggestionActivity)。此活动还包含建议产品内的recyclerview。当我单击此产品时,选择或取消选择。在recyclerview下,我有一个按钮,当单击此按钮时,它应该是第一个收听recyclerview适配器并通知数据的。

例如: ProductsFragment-> RecyclerAdapter->单击行->打开活动->单击一个或多个行,然后选择->单击按钮->关闭活动并通知片段适配器


我在片段中的第一个recyclerview

public class MyView extends RecyclerView.ViewHolder {

        public MyView(View view) {
            super(view);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    SuggestionActivity x = new SuggestionActivity();
                    x.setTab2AdapterListener(new SuggestionActivity.Tab2AdapterListener() {
                        @Override
                        public void interfaceClicked() {
                            Log.d("interfaceClicked", "interfaceClicked"); // This line doesn't work!
                        }
                    });


                    Intent i = new Intent(mContext, SuggestionActivity.class);
                    i.putExtra("first", String.valueOf(tag));
                    i.putExtra("second", String.valueOf(getAdapterPosition()));
                    mContext.startActivity(i);
                }
            });
        }
    }

我在“建议活动”上定义的界面

private Tab2AdapterListener tab2AdapterListener;
public interface Tab2AdapterListener {
    void interfaceClicked();
}
public void setTab2AdapterListener(Tab2AdapterListener tab2AdapterListener) {
    this.tab2AdapterListener = tab2AdapterListener;
}

InspectationActivity中的按钮

btnSendSuggestions.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String suggested = "";
                for (int i = 0; i < suggestedProductsList.size(); i++) {
                    if (suggestedProductsList.get(i).getIsSelected())
                        suggested += suggestedProductsList.get(i).getProductId() + ",";
                }
                if ( suggested.length() > 0 ) {
                    suggested = suggested.substring(0, suggested.length() - 1);
                }

                if (tab2AdapterListener != null)
                    tab2AdapterListener.interfaceClicked();
                 // tab2AdapterListener comes null
            }
        });

0 个答案:

没有答案