How to use Recycleview on fragment ?

Asked

Viewed 370 times

0

I don’t know if I asked the question correctly, but let’s go there who can help me thank :)

this is my second Mainactivity, I am making an application with navigation Drawer and in it to make the other screens of the menu I am using the fragments, however I want to make a list in each menu, but I do not know how to do it inside a class that extends Fragment...

public class Main2activity extends Appcompatactivity {

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

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false);
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recyvlerview);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(new MyAdapter(this));

}

}

This is the Adapter:

public class Myadapter extends Recyclerview.Adapter {

Context context;
// array para receber as imagens
ArrayList<Bitmap> images = new ArrayList<Bitmap>();




public MyAdapter(Context context) {
    this.context = context;
    // imagens
    images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.tn_gpx15));
    images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.tn_gpx19));
    images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.tn_tb));
    images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.tn_gpx15));
    images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.tn_gpx19));
    images.add(BitmapFactory.decodeResource(context.getResources(),R.drawable.tn_tb));


}

public MyAdapter(Fragment_main fragment_main) {

}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(context).inflate(R.layout.row, parent, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
    // setando imagens, posições.
    holder.imageView.setImageBitmap(images.get(position));

}

@Override
public int getItemCount() {

    return images.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

    ImageView imageView;

    public ViewHolder(View itemView) {
        super(itemView);

        imageView = (ImageView) itemView.findViewById(R.id.my_imagesview);


    }
}

so far so good, works all right and returns me the list of image, inserir a descrição da imagem aqui

the more I wanted to do the same in fragment, there I tried to do the same thing,

public class Fragment_main extends Fragment {

RecyclerView recyclerView2;

public Fragment_main() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View Acess = inflater.inflate(R.layout.fragment_main, container, false);

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false);
    RecyclerView recyclerView = (RecyclerView) Acess.findViewById(R.id.my_recyvlerview2);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(new MyAdapter(this));

    return Acess;
}

}

but in the Linearlayoutmanager(this, Linearlayoutmanager.VERTICAL, false) section, it is pointing out that it is not compatible, in the context part.

inserir a descrição da imagem aqui

wanted to know how to solve this and display this list in the fragment layout...

1 answer

3


When you are working on Fragment, you should use getActivity(); in place of this

example:

RecyclerView.LayoutManager layoutManager = 
    new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);
  • 1

    valeu worked before I got to put getcontext kkk, thank you Eonardo days, helped me a lot, remembering that for those who are seeing this post and is in same problem, have to replace the two this by getActivity()...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.