如何在FrameLayout上实现RecyclerView? [英] How to implement RecyclerView on a FrameLayout?

查看:103
本文介绍了如何在FrameLayout上实现RecyclerView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习RecyclerView并陷入活动中.我有一个bottomNavigationView和bottomNavigationView上方的框架布局,我想在该FrameLayout上显示一个RecyclerView.我怎样才能做到这一点? 我的程序没有错误,我也不知道为什么它没有显示RecyclerView.

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.

这是xml

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>

这是myadapter类

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);
    }
}

我的项目中没有显示错误,但没有显示RecyclerView.

推荐答案

好吧,您只需在创建碎片视图之前设置您的回收站视图,我主要是在您的上一个问题中提及您
在'onViewCreated'方法中设置您的Recyler视图,在那里查看我的答案.不要一次又一次地发布相同的问题,您还可以编辑问题

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

这篇关于如何在FrameLayout上实现RecyclerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆