Search code examples
android-recyclerviewfragmentandroid-framelayoutbottomnavigationview

How to implement RecyclerView on a FrameLayout?


I am learning RecyclerView and stuck in activity. I have a bottomNavigationView and a frame layout above the bottomNavigationView I want to show a RecyclerView on that FrameLayout. How can I do that? There is no error in my program and i don't know why its not showing the RecyclerView.

This is the xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Bottom_nav">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomnavid"
       />
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomnavid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav"
        />
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#FFEB3B"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/live_matchrecyclerid"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

    <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp"
    android:padding="5dp"
    app:cardCornerRadius="3dp"
    app:cardElevation="5dp"
    >
    <TextView
        android:id="@+id/cityid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textSize="50sp"
        android:padding="5dp"/>

</android.support.v7.widget.CardView>

This is the myadapter class

 public class Myadapter extends RecyclerView.Adapter<Myviewholder> {
            ArrayList<String> Citynames;
            Context c;
             public Myadapter(ArrayList<String> citynames, Context c) {
            Citynames = citynames;
            this.c = c;
        }
        @NonNull
        @Override
        public Myviewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(c).inflate(R.layout.cardview,viewGroup,false);
            Myviewholder VH = new Myviewholder(v);
            return VH;
        }
        @Override
        public void onBindViewHolder(@NonNull Myviewholder myviewholder, int i) {
            myviewholder.nametext.setText(Citynames.get(i));
        }
        @Override
        public int getItemCount() {
            return Citynames.size();
        }
    }
    this is my viewholder class 
        public class Myviewholder extends RecyclerView.ViewHolder {

        TextView nametext;

        public Myviewholder(@NonNull View itemView) {
            super(itemView);
            nametext = itemView.findViewById(R.id.cityid);
        }
    }

This is my main class

public class Feature extends Fragment {
       ArrayList<String> Citynames = new ArrayList<>   (Arrays.asList("dhaka","rongpur","bagura",
            "sylhet","vhola","lalmonirhut","khulna","cumillah","rajshahi"));
    public  Feature()
    {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view  = inflater.inflate(R.layout.feature,container,false);

         RecyclerView recyclerView = view.findViewById(R.id.featurerecyclerid);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        Myadapter myadapter = new Myadapter(Citynames,getActivity());
        recyclerView.setAdapter(myadapter);
        return inflater.inflate(R.layout.feature,container,false);
    }
}

No error is showing in my project but not showing the RecyclerView.


Solution

  • ok you just set your your recycler view before the fragment view created,I mostly metion you in your previuous question
    set your recyler view in 'onViewCreated' method,see my answer there.And not to post same question again and again,you also can edit your question