Home > OS >  How to call Fragment method from RecyclerView layoutManager?
How to call Fragment method from RecyclerView layoutManager?

Time:11-03

How to call Fragment method from RecyclerView layoutmanager? Here are my Adapter:

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
    private ArrayList<ExampleItem> mExampleList  ;
    public static class ExampleViewHolder extends RecyclerView.ViewHolder{
        public ImageView mImageView;
        public TextView mTextView1;
        public TextView mTextView2;
        public TextView mTextView3;
        public TextView mTextView4;

        public ExampleViewHolder(@NonNull View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.Icon_homework);
            mTextView1 = itemView.findViewById(R.id.Line_1);
            mTextView2 = itemView.findViewById(R.id.Line_2);
            mTextView3 = itemView.findViewById(R.id.line_3);
            mTextView4 = itemView.findViewById(R.id.line_4);

        }
    }

    public ExampleAdapter(ArrayList<ExampleItem> exampleList){
        mExampleList = exampleList;
    }

    @NonNull
    @Override
    public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_stundenplan, parent, false);
        ExampleViewHolder evh = new ExampleViewHolder(v);
        return evh;
    }

    @Override
    public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
        ExampleItem currentitem = mExampleList.get(position);

        holder.mImageView.setImageResource(currentitem.getImageRessource());
        holder.mTextView1.setText(currentitem.getText1());
        holder.mTextView2.setText(currentitem.getText2());
        holder.mTextView3.setText(currentitem.getText3());
        holder.mTextView4.setText(currentitem.getText4());

    }

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

Here are my Items:

public class ExampleItem   {
    private int mImageRessource;
    private String mText1;
    private String mText2;
    private String mText3;
    private String mText4;

    public ExampleItem(int imageRessource, String text1, String text2, String text3, String text4){
        mImageRessource = imageRessource;
        mText1 = text1;
        mText2 = text2;
        mText3 = text3;
        mText4 = text4;
    }

    public int getImageRessource(){
        return mImageRessource;
    }

    public String getText1(){
        return mText1;
    }

    public String getText2(){
        return mText2;
    }

    public String getText3(){
        return mText3;
    }

    public String getText4(){
        return mText4;
    }

}

And here are my Fragment code, i tried to find the problem, because when i open the app and click on the Fragmnet, it automatically close the app, and I think it is up to the layout manager and/or the Adapter.

public class StundenplanFragment extends Fragment {

    private RecyclerView mrecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment StundenplanFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static StundenplanFragment newInstance(String param1, String param2) {
        StundenplanFragment fragment = new StundenplanFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<ExampleItem> exampleList = new ArrayList<>();
        exampleList.add(new ExampleItem(R.drawable.backpack, "Fach", "7:50-9:30", "2.0", "Raum:15"));



        mAdapter = new ExampleAdapter(exampleList);
        mLayoutManager = new LinearLayoutManager(getContext());

        mrecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mrecyclerView.setAdapter(mAdapter);
        //mAdapter.notifyDataSetChanged();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

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

        mrecyclerView= view.findViewById(R.id.recyclerview);
        return view;
        //recyclerView.setHasFixedSize(true);
        //return inflater.inflate(R.layout.fragment_stundenplan, container, false);
    }


}

CodePudding user response:

pass fragment to the recyclerview constructor

   public ExampleAdapter(ArrayList<ExampleItem> exampleList,Fragment fragment){
    mExampleList = exampleList;
    this.fragment=fragment;
}

and when you need to call the method

if(fragment instanceOf StundenplanFragment ){
((StundenplanFragment)fragment).methodName());
}

CodePudding user response:

the method onCreate being called before onCreateView so basically in the fragment your trying to set the recyclerView adapter before the recyclerView created

so your fragment should look like this:

public class StundenplanFragment extends Fragment {

    // declaring the list in order to use it again in 'onCreateView'
    private ArrayList<ExampleItem> exampleList = new ArrayList<>();
    private RecyclerView mrecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment StundenplanFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static StundenplanFragment newInstance(String param1, String param2) {
        StundenplanFragment fragment = new StundenplanFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        exampleList.add(new ExampleItem(R.drawable.backpack, "Fach", "7:50-9:30", "2.0", "Raum:15"));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

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

        mrecyclerView= view.findViewById(R.id.recyclerview);

        mAdapter = new ExampleAdapter(exampleList);
        mLayoutManager = new LinearLayoutManager(getContext());

        mrecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mrecyclerView.setAdapter(mAdapter);
        //mAdapter.notifyDataSetChanged();

        return view;
        //recyclerView.setHasFixedSize(true);
        //return inflater.inflate(R.layout.fragment_stundenplan, container, false);
    }


}
  • Related