How to use Recycleview in the snippet?

0

I do not know if I asked the question correctly, but come on, who can help me thank you:)

This is my second MainActivity, I am doing an application with navigation drawer and in it to do 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 which 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));

}

}

Here 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, everything works fine and returns me to the image list,

But I wanted to do the same thing in the 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;
}

}

however in the LinearLayoutManager (this, LinearLayoutManager.VERTICAL, false) excerpt; is accusing that it is not compatible in the context part.

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

    
asked by anonymous 19.10.2016 / 15:44

1 answer

3

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

example:

RecyclerView.LayoutManager layoutManager = 
    new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);
    
19.10.2016 / 15:49