How to implement RecyclerView on a FrameLayout?

帅比萌擦擦* 提交于 2019-12-11 16:27:16

问题


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.


回答1:


Welcome to SO!

Create your Adapter like this.

MyAdapter.java

public class Myadapter extends RecyclerView.Adapter<Myviewholder>{

    private ArrayList<String> Citynames;
    private Context c;
    private LayoutInflater layoutInflater;
    private Myviewholder viewHolder;

//constr
public Myadapter(List<String> Citynames, Context c) {
        this.c = c;
        this.Citynames = Citynames;
        this.layoutInflater = LayoutInflater.from(c);

    }

@NonNull
    @Override
    public Myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.cardview, parent, false);
        viewHolder = new Myviewholder(view);

        return viewHolder;
    }

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

                   myviewholder.nametext.setText(Citynames.get(i));

}

 @Override
    public int getItemCount() {
        return Citynames.size();
}

//your view holder
public class Myviewholder extends RecyclerView.ViewHolder {

        TextView nametext;

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

and your main class like this

...

@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);
        myadapter.notifyDataSetChanged(); //add this too

        return view; //simply 
    }

Hope this help!!




回答2:


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



来源:https://stackoverflow.com/questions/58597292/how-to-implement-recyclerview-on-a-framelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!