如何从我的Adapter onClick打开一个片段

时间:2017-02-22 10:09:47

标签: android android-fragments

我想从我的RecyclerView Adapter onlick打开一个名为optionsFragment的新片段。我尝试了很多教程,但我对此非常愚蠢。

新片段应在左上角有一个后退箭头按钮,以返回RecyclerView。

请帮帮我。

这是我在适配器中持有者的onClick函数:



FragmentManager benutzerlisteManager = ((NavigationDrawerActivity)customContext).getSupportFragmentManager();

               BenutzerOverviewFragment benutzerOverviewFragment = new BenutzerOverviewFragment();

               benutzerlisteManager.beginTransaction().replace(R.id.content_navigation_drawer,
                       benutzerOverviewFragment,
                       benutzerOverviewFragment.getTag())
                       .addToBackStack(null)
                       .commit();




我已尝试过顶部的代码,但应用程序崩溃时出现此错误:

  

NavigationDrawerActivity @ b42040b必须实现OnFragmentInteractionListener

5 个答案:

答案 0 :(得分:3)

以下是从适配器加载新片段的方法:

  holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 OptionsFragment optionsFrag = new OptionsFragment ();
((ActivityName)context).getSupportFragmentManager().beginTransaction().replace(R.id.container, optionsFrag,"OptionsFragment").addToBackStack(null).commit();
    }
        });

对于背面按下图标,请按照以下步骤操作: Manage toolbar's navigation and back button from fragment in android

答案 1 :(得分:2)

  1. 创建界面:
    class MyActivity extends Activity implements MyItemClickListener{ }

  2. 将此界面实施到您的活动/片段:

  3. MyAdapter adapter = new MyAdapter(this); //this is instance of MyItemClickListner

    1. 将接口的对象传递给适配器:
    2. // OnClickListener for holder

      1. 从适配器调用接口的方法:
      2. holder.itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View v) {
        mMyClickListner.onItemClick(getAdapterPosition());
        }
        });
        mMyClickListner.onItemClick(getAdapterPosition());

        这会在你的activity / fragment中调用onItemClick。从此方法声明中,您可以打开片段:

        sFilterConfig.BankNumber = 0; sFilterConfig.FilterNumber = 0; sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; sFilterConfig.FilterIdHigh = 0x10<<5; sFilterConfig.FilterIdLow = 0; sFilterConfig.FilterMaskIdHigh = 0xff<<5; sFilterConfig.FilterMaskIdLow = 0; sFilterConfig.FilterFIFOAssignment = 0; sFilterConfig.FilterActivation = ENABLE; HAL_CAN_ConfigFilter(&hcan, &sFilterConfig);

答案 2 :(得分:2)

AppCompatActivity activity = (AppCompatActivity) view.getContext();

Fragment fragment = new DetailFragment();

activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer,fragment).addToBackStack(null).commit();

您可以编写onClick

答案 3 :(得分:0)

我假设你有活动对象。 您可以使用活动这样的对象

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {

            activity.getFragmentManager().beginTransaction().replace(R.id.container,new YourFragment()).addToBackStack(null).commit();


            }
        });

答案 4 :(得分:0)

尝试

private Context mContext;
ArrayList<Inquiry> mInquiryYearsLists;
InquiryFragment mInquiryFragment;

public InquiryAdapter(Context context, ArrayList<Inquiry> arrayListingg) {
    this.mInquiryYearsLists = arrayListingg;
    this.mContext = context;
}

@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {

            mInquiryFragment = new InquiryDashboardFragment();

            FragmentTransaction mFragmentTransaction = ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction();
            mFragmentTransaction.replace(R.id.fragment_container, mInquiryFragment). mFragmentTransaction.addToBackStack(null).commit();

        }
    });


}