如何从RecyclerView适配器打开片段?

时间:2017-12-28 08:09:19

标签: java android android-fragments

在我的应用程序中,我希望使用fragment打开adapter 我在RecyclerView adapter中写下按钮的以下代码 片段代码:

public class SelectAddressFragment extends BaseFragment implements SelectAddressInterface {

    @BindView(R.id.selectAddressFrag_progressBar)
    ProgressBar selectAddressFrag_progressBar;
    @BindView(R.id.selectAddressFrag_list)
    RecyclerView selectAddressFrag_list;
    private PrefsUtils prefsUtils;
    private String phoneNumber;
    private APIs apIs;
    private List<Re> model = new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;
    private RelativeLayout changeAddressContent, newAddressContent;
    private SelectAddressAdapter adapter;
    private String sendAddressID, sendName, sendState, sendCity, sendAddress, sendPostalCode, sendPhone, sendCellPhone;

    public SelectAddressFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_address, container, false);

        ButterKnife.bind(this, v);
        apIs = ApiClient.getClient().create(APIs.class);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        prefsUtils = new PrefsUtils(getActivity());
        if (prefsUtils.isExist(PrefsKeys.PHONE_NUMBER.name())) {
            phoneNumber = prefsUtils.getFromShared(PrefsKeys.PHONE_NUMBER.name());
        } else {
            phoneNumber = "";
        }
        changeAddressContent = getActivity().findViewById(R.id.changeAddressContent);
        newAddressContent = getActivity().findViewById(R.id.newAddressContent);
        adapter = new SelectAddressAdapter(getActivity(), model, this, getFragmentManager());

        getListAddressData();
        initRecyclerView(selectAddressFrag_list, adapter);

        return v;
    }

我写了上面的代码,但点击按钮时没有打开片段!

我该如何解决?

2 个答案:

答案 0 :(得分:1)

您可以将FragmentManager传递给适配器构造函数。像,

 public FragmentManager f_manager;
    //in your constructor add FragmentManager
    public YourAdapterName(Context context, FragmentManager f_manager)
    {
        this.context = context;
        this.f_manager = f_manager;
    }

点击:

     holder.itemListDialog_edtBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                     // We can get the fragment manager
                   f_manager.beginTransaction()
                        .replace(R.id.newAddressContent, new NewAddressFragment())
                        .commit();

            }
        })
    ;

答案 1 :(得分:0)

建议处理片段中的点击事件并使用回调方法! 如果您使用片段中的管理器来获取此信息: GetChildFragmentManager()

  

适配器:

static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    public interface OnItemCLickListener{
        void OnItemClick(int position);
    }

    private OnItemCLickListener mOnItemCLickListener;

    public void setOnItemCLickListener(OnItemCLickListener onItemCLickListener) {
        this.mOnItemCLickListener = onItemCLickListener;
    }

    @Override
    public ProgressViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //...
    }

    @Override
    public void onBindViewHolder(ProgressViewHolder holder, int position) {
        //...
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        ViewHolder(View itemView) {
            super(itemView);

            if(mOnItemCLickListener!=null){
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mOnItemCLickListener.OnItemClick(getAdapterPosition());
                    }
                });
            }
        }
    }
}
  

片段:

mAdapter.setOnItemCLickListener(new RecyclerViewAdapter.OnItemCLickListener() {
            @Override
            public void OnItemClick(int position) {
                // TODO: 2017/12/28 ...
            }
        });