Skip to main content

Androi studio Cargar Android Studio cargar ReciclerView en Fragment

/**

  • A simple {@link Fragment} subclass. */ public class Fragment_horizontal extends Fragment {

    private static final String TAG = "HomeActivity";

    //vars private ArrayList mNames = new ArrayList<>(); private ArrayList mImageUrls = new ArrayList<>();

    public Fragment_horizontal() { // 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_h, container, false);

    }

    private void getImages(){ Log.d(TAG, "initImageBitmaps: preparing bitmaps.");

     mImageUrls.add("https://c1.staticflickr.com/5/4636/25316407448_de5fbf183d_o.jpg");
     mNames.add("Havasu Falls");
    
     mImageUrls.add("https://.jpg");
     mNames.add("Trondheim");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Portugal");
    
     mImageUrls.add("https://i.redd.it/j6myfqglup501.jpg");
     mNames.add("Rocky Mountain National Park");
    
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Mahahual");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Frozen Lake");
    
    
     mImageUrls.add("https://i..jpg");
     mNames.add("White Sands Desert");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Austrailia");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Washington");
    
     initRecyclerView();
    

    }

    private void initRecyclerView(){ Log.d(TAG, "initRecyclerView: init recyclerview");

     LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
     RecyclerView recyclerView = getView().findViewById(R.id.recyclerView);
     getActivity().findViewById(R.id.recyclerView);
     recyclerView.setLayoutManager(layoutManager);
     RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), mNames, mImageUrls);
     recyclerView.setAdapter(adapter);
    

    } }

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment_horizontal extends Fragment {

    private static final String TAG = "HomeActivity";

    //vars
    private ArrayList<String> mNames = new ArrayList<>();
    private ArrayList<String> mImageUrls = new ArrayList<>();



    public Fragment_horizontal() {
        // 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_h, container, false);

    }

    private void getImages(){
        Log.d(TAG, "initImageBitmaps: preparing bitmaps.");

        mImageUrls.add("https://c1.staticflickr.com/5/4636/25316407448_de5fbf183d_o.jpg");
        mNames.add("Havasu Falls");

        mImageUrls.add("https://.jpg");
        mNames.add("Trondheim");

        mImageUrls.add("https://i..jpg");
        mNames.add("Portugal");

        mImageUrls.add("https://i.redd.it/j6myfqglup501.jpg");
        mNames.add("Rocky Mountain National Park");


        mImageUrls.add("https://i..jpg");
        mNames.add("Mahahual");

        mImageUrls.add("https://i..jpg");
        mNames.add("Frozen Lake");


        mImageUrls.add("https://i..jpg");
        mNames.add("White Sands Desert");

        mImageUrls.add("https://i..jpg");
        mNames.add("Austrailia");

        mImageUrls.add("https://i..jpg");
        mNames.add("Washington");

        initRecyclerView();

    }

    private void initRecyclerView(){
        Log.d(TAG, "initRecyclerView: init recyclerview");

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        RecyclerView recyclerView = getView().findViewById(R.id.recyclerView);
        getActivity().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(layoutManager);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), mNames, mImageUrls);
        recyclerView.setAdapter(adapter);
    }
}

/**

  • A simple {@link Fragment} subclass. */ public class Fragment_horizontal extends Fragment {

    private static final String TAG = "HomeActivity";

    //vars private ArrayList mNames = new ArrayList<>(); private ArrayList mImageUrls = new ArrayList<>();

    public Fragment_horizontal() { // 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_h, container, false);

    }

    private void getImages(){ Log.d(TAG, "initImageBitmaps: preparing bitmaps.");

     mImageUrls.add("https://c1.staticflickr.com/5/4636/25316407448_de5fbf183d_o.jpg");
     mNames.add("Havasu Falls");
    
     mImageUrls.add("https://.jpg");
     mNames.add("Trondheim");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Portugal");
    
     mImageUrls.add("https://i.redd.it/j6myfqglup501.jpg");
     mNames.add("Rocky Mountain National Park");
    
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Mahahual");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Frozen Lake");
    
    
     mImageUrls.add("https://i..jpg");
     mNames.add("White Sands Desert");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Austrailia");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Washington");
    
     initRecyclerView();
    

    }

    private void initRecyclerView(){ Log.d(TAG, "initRecyclerView: init recyclerview");

     LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
     RecyclerView recyclerView = getView().findViewById(R.id.recyclerView);
     getActivity().findViewById(R.id.recyclerView);
     recyclerView.setLayoutManager(layoutManager);
     RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), mNames, mImageUrls);
     recyclerView.setAdapter(adapter);
    

    } }

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment_horizontal extends Fragment {

    private static final String TAG = "HomeActivity";

    //vars
    private ArrayList<String> mNames = new ArrayList<>();
    private ArrayList<String> mImageUrls = new ArrayList<>();



    public Fragment_horizontal() {
        // 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_h, container, false);

    }

    private void getImages(){
        Log.d(TAG, "initImageBitmaps: preparing bitmaps.");

        mImageUrls.add("https://c1.staticflickr.com/5/4636/25316407448_de5fbf183d_o.jpg");
        mNames.add("Havasu Falls");

        mImageUrls.add("https://.jpg");
        mNames.add("Trondheim");

        mImageUrls.add("https://i..jpg");
        mNames.add("Portugal");

        mImageUrls.add("https://i.redd.it/j6myfqglup501.jpg");
        mNames.add("Rocky Mountain National Park");


        mImageUrls.add("https://i..jpg");
        mNames.add("Mahahual");

        mImageUrls.add("https://i..jpg");
        mNames.add("Frozen Lake");


        mImageUrls.add("https://i..jpg");
        mNames.add("White Sands Desert");

        mImageUrls.add("https://i..jpg");
        mNames.add("Austrailia");

        mImageUrls.add("https://i..jpg");
        mNames.add("Washington");

        initRecyclerView();

    }

    private void initRecyclerView(){
        Log.d(TAG, "initRecyclerView: init recyclerview");

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        RecyclerView recyclerView = getView().findViewById(R.id.recyclerView);
        getActivity().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(layoutManager);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), mNames, mImageUrls);
        recyclerView.setAdapter(adapter);
    }
}
Origen Enlace

Androi studio Cargar ReciclerView en Fragment

Quiero cargar un ReciclerView en un Fragment pero solo se muestra la platilla de xml y no me carga lo demás:

/**

  • A simple {@link Fragment} subclass. */ public class Fragment_horizontal extends Fragment {

    private static final String TAG = "HomeActivity";

    //vars private ArrayList mNames = new ArrayList<>(); private ArrayList mImageUrls = new ArrayList<>();

    public Fragment_horizontal() { // 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_h, container, false);

    }

    private void getImages(){ Log.d(TAG, "initImageBitmaps: preparing bitmaps.");

     mImageUrls.add("https://c1.staticflickr.com/5/4636/25316407448_de5fbf183d_o.jpg");
     mNames.add("Havasu Falls");
    
     mImageUrls.add("https://.jpg");
     mNames.add("Trondheim");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Portugal");
    
     mImageUrls.add("https://i.redd.it/j6myfqglup501.jpg");
     mNames.add("Rocky Mountain National Park");
    
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Mahahual");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Frozen Lake");
    
    
     mImageUrls.add("https://i..jpg");
     mNames.add("White Sands Desert");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Austrailia");
    
     mImageUrls.add("https://i..jpg");
     mNames.add("Washington");
    
     initRecyclerView();
    

    }

    private void initRecyclerView(){ Log.d(TAG, "initRecyclerView: init recyclerview");

     LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
     RecyclerView recyclerView = getView().findViewById(R.id.recyclerView);
     getActivity().findViewById(R.id.recyclerView);
     recyclerView.setLayoutManager(layoutManager);
     RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), mNames, mImageUrls);
     recyclerView.setAdapter(adapter);
    

    } }