Click here to Skip to main content
14,644,142 members
Rate this:
Please Sign up or sign in to vote.
See more:
can i use ListFragment to achieve what I said above or i need a custom recycler view inside a fragment, I tried to put a recycler view inside fragment but i didn't know how to make the onClick for recycler view work with fragment so it passes data to another fragment or another activity in case of multi-pane UI, so can i use list fragment or my only choice is recycler view, if it's then how can I do it in recycler view ?

my MainActivity which has a fragment that contains the recycler view

public class MainActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {

    FragmentManager fm = getSupportFragmentManager();

    Fragment fragment = fm.findFragmentById(;

    if (fragment == null) {
        fragment = new FragmentRecyclerList();

                .add(, fragment)


my fragment which contains the recycler view

public class FragmentRecyclerList extends Fragment implements MyDialog.OnInputSelected {

private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;
private List<string> titles;
private FloatingActionButton fab;

public FragmentRecyclerList() {
    // Required empty public constructor

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_fragment_recycler_list, container, false);

void showDialog() {
    MyDialog myDialog = new MyDialog();
    myDialog.setTargetFragment(FragmentRecyclerList.this, 1);, "MyDialog");

public void onActivityCreated(@Nullable Bundle savedInstanceState) {

    recyclerView = getView().findViewById(;
    fab = getView().findViewById(;

    titles = new ArrayList<>();

    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    recyclerViewAdapter = new RecyclerViewAdapter(getActivity(), titles);


    fab.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


my recycler view adapter

public class RecyclerViewAdapter extends RecyclerView.Adapter {

private Context context;
private List<string> titles;

public RecyclerViewAdapter(Context context, List<string> titles) {
    this.context = context;
    this.titles = titles;

public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_wallpaper
            , parent, false);
    return new ViewHolder(view);

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    String title = titles.get(position);

public int getItemCount() {
    return titles.size();

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView tvTitle;

    public ViewHolder(@NonNull View itemView) {
        View v = itemView;
        tvTitle = itemView.findViewById(;


What I have tried:

i tried many ways but nothing works.
Updated 11-Mar-20 10:57am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Why are you using `findFragmentById` and not Jetpack Navigation library?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100