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 {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();

    Fragment fragment = fm.findFragmentById(R.id.main_fragment_container);

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

        fm.beginTransaction()
                .add(R.id.main_fragment_container, fragment)
                .commit();
    }

}
}  


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
}


@Override
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.show(getFragmentManager(), "MyDialog");
}

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

    recyclerView = getView().findViewById(R.id.recyclerview);
    fab = getView().findViewById(R.id.floatingActionButton);

    titles = new ArrayList<>();

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

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

    recyclerView.setAdapter(recyclerViewAdapter);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog();
        }
    });


}
} 


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;
}

@NonNull
@Override
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);
}

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

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

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView tvTitle;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        View v = itemView;
        tvTitle = itemView.findViewById(R.id.row_title);

    } 


What I have tried:

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

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