How to start activity from RecyclerView adapter in fragment [duplicate]

假如想象 提交于 2020-01-24 22:46:30

问题


I can't click RecyclerView to a new Activity from RecyclerViewAdapter.

I call ItemClick here.

DayAdapter.java:

holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {
            openProgramActivity(view, position);
        }
    });
}

This function opens a new Activity:

public void openProgramActivity(View view, int position) {
    //Intent openProgramActivity = new Intent(context, ProgramActivity.class);
    Intent openProgramActivity = new Intent(view.getContext(), ProgramActivity.class);
    openProgramActivity.putExtra("index",position);
    view.getContext().startActivity(openProgramActivity);
}

FragmentDay30.java:

public class FragmentDay30 extends Fragment {

private View view;

public static FragmentDay30 newInstance() {
    FragmentDay30 fragment = new FragmentDay30();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_30day, container,false);

    ViewPager slideViewPager = (ViewPager) view.findViewById(R.id.slideViewPager);
    SlideAdapter slideAdapter = new SlideAdapter(getActivity());
    slideViewPager.setAdapter(slideAdapter);

    RecyclerView fragment30datRecyclerView = (RecyclerView) view.findViewById(R.id.fragment30dayRecyclerView);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
    fragment30datRecyclerView.setLayoutManager(linearLayoutManager);
    DayAdapter dayAdapter = new DayAdapter(getActivity());
    fragment30datRecyclerView.setAdapter(dayAdapter);

    return view;
}

I try to use getActvity() and getContext() but not to new Activity.


回答1:


Pass contaxt to recyclerview adapter constructor like this

Context context;
MyAdapter(Context context, .....){
this.context=context;
}

Call Activity

context.startActivity(......);



回答2:


Though you can start Activity from Adapter class passing a Context but as Documented it's

  1. not a Good design pattern
  2. and also a Bad practice to follow.

I would rather suggest to have an interface defined in your Adapter class which would be implemented by the Fragment class. Fragment class initializes the Adapter passing it's reference which you would typeCast to interface like this

DayAdpater.class

public class DayAdapter extends RecyclerView.Adapter<DayAdapter.ViewHolder> {

    private OnActionListener mListener;

    DayAdapter(OnActionListener listener){
        this.mListener=listener;
    }

     holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {
                mListener.startActivity(position);
        }
    });

    interface OnActionListener{
        public void startActivity(int position);
    }

}

FragmentDay30.class

public class FragmentDay30 extends Fragment implements DayAdapter.OnActionListener{

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_30day, container,false);

        RecyclerView fragment30datRecyclerView = (RecyclerView) view.findViewById(R.id.fragment30dayRecyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        fragment30datRecyclerView.setLayoutManager(linearLayoutManager);
        DayAdapter dayAdapter = new DayAdapter(getActivity(), this);
        fragment30datRecyclerView.setAdapter(dayAdapter);

        return view;
    }

    /**
    * this is the place where you should start a new activity
    */
    public void startActivity(int position) {
        //Intent openProgramActivity = new Intent(context, ProgramActivity.class);
        Intent openProgramActivity = new Intent(getActivity(), ProgramActivity.class);
        openProgramActivity.putExtra("index",position);
        getActivity.startActivity(openProgramActivity);
    }

}

This is how the your adpater class interacts with the fragment class.

Hope this helps.




回答3:


Pass context in RecyclerView adapter constructor which you are using for setAdapter like this:

Context context;

MyCustomAdapter(Context context, .....){
this.context=context;
}

For call Activity used:

context.startActivity(......);



来源:https://stackoverflow.com/questions/51219863/how-to-start-activity-from-recyclerview-adapter-in-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!