Search code examples
androidandroid-fragmentactivityandroid-recyclerview

android recycleview using with fragment


Currently, I am working on a project that has a navigation drawer, and I am using fragments for the stuff. I was about to user a recycle view for it, but I get a problem with the code, specifically when I try to add the LayoutInflater, and stick it into my fragment that should be using the layoutinflater.from(fragment.getBaseContext); which isn't working

That is the code that works on the usual Activity, but it doesn't seem to work with a .fragment activity

Can anybody help me with this? or even point me in the right direction?

package sample.101.sample.fragment;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import java.util.ArrayList;

public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewHolder> {

    Bad_house_list bad_house_list;
    ArrayList<User> userArrayList;

    public MyRecycleViewAdapter(Bad_house_list bad_house_list, ArrayList<User> userArrayList) {
        this.bad_house_list = bad_house_list;
        this.userArrayList = userArrayList;
    }

    @NonNull
    @Override
    public MyRecycleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(Bad_house_list.getBaseContext());
        layoutInflater.inflate(R.layout.single_row, parent,false);


        return null;
    }

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

    }

    @Override
    public int getItemCount() {
        return 0;
    }
}

Solution

  • try this !

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_answer, parent, false);
    view.setOnClickListener(this);
    view.setOnLongClickListener(this);
    MyCustomHolderClass myCustomHolder = new MyCustomHolderClass(view);
    return myCustomView;