lilintong 2019-10-07 10:02 采纳率: 0%
浏览 6785

Recyclerview嵌套recyclerview的深坑,我坐在Recyclerview这个坑里很久了,仰望天空希望能把我拉上去。

Recyclerview嵌套recyclerview的深坑,对于一个新手来说怎么也上不去,特来邀请键盘中的高手高高手,拉拉我;

第一个Recyclerview布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/area_LinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/drawable_black_one"
        android:orientation="horizontal"
        android:layout_margin="3dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_gravity="center"
            android:text=""
            android:textColor="@drawable/drawable_white_pure"
            android:lines="1"
            android:layout_weight="1"/>


        <TextView
            android:layout_width="1dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:background="@color/whiteness"

             />

        <TextView
            android:id="@+id/peers_location_TextView"
            android:layout_width="70dp"
            android:layout_height="40dp"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="10dp"
            android:ellipsize="end"
            android:gravity="center"
            android:text=""
            android:textColor="@color/whiteness"
            android:textSize="14sp"
            android:lines="1"
            android:textStyle="bold" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        >
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >


            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="0dp"
                android:layout_marginEnd="10dp"
                android:scaleType="fitStart"
                android:src="@drawable/location" />
        </RelativeLayout>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/drawable_black_one"
        android:layout_gravity="center"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_margin="3dp">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:background="@color/text_white"
            android:hint="行业人员"
            android:textSize="13dp"
            android:gravity="center"
            android:lines="1"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="搜索"
            android:textSize="14dp"
            android:layout_marginLeft="14dp"
            android:layout_gravity="center"
            android:textColor="@color/whiteness"
            android:layout_margin="5dp"
            android:gravity="center"/>
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/user_info_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />
</LinearLayout>

在第一个Recycleview的item里面嵌套了第二个Recyclerview不知道这样对不对?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/portrait"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/user_portrait"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="5dp"
                android:src="@drawable/em_default_avatar" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="20dp"
                    android:layout_marginTop="5dp"
                    android:gravity="clip_horizontal">

                    <TextView
                        android:id="@+id/user_industry"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/dialog_circular_whiteness"
                        android:ellipsize="end"
                        android:gravity="center"
                        android:text="建筑"
                        android:textColor="@color/colorPrimary"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/user_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:layout_marginLeft="5dp"
                        android:background="@drawable/dialog_circular_whiteness"
                        android:ellipsize="end"
                        android:gravity="center"
                        android:text="努力改变自己"
                        android:textColor="#323232"
                        android:textSize="10sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/name_voip"
                        android:layout_width="20dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:ellipsize="end"
                        android:gravity="center"
                        android:text="voip"
                        android:textColor="@color/text_pink"
                        android:textSize="7sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="35dp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="努力改变自己"
                        android:textSize="12dp" />
                </LinearLayout>

            </LinearLayout>

            <RelativeLayout
                android:layout_width="60dp"
                android:layout_height="match_parent"
                android:gravity="center">

                <TextView
                    android:id="@+id/user_location"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:background="@drawable/dialog_circular_whiteness"
                    android:text=""
                    android:textSize="12dp"
                    android:textStyle="bold" />
            </RelativeLayout>

        </LinearLayout>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">

                **<!-- 导入RecyclerView-->**
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/user_peesr_info"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="10dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="访问:10000人"
                    android:textSize="8dp" />
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>

第一个Recyclerview的适配器:

public class Home_RecyclerView_Adapter extends RecyclerView.Adapter<Home_RecyclerView_Adapter.myViewHodler> {
    private Context context;
    private LinkedList<Home_list_get_set> LinkedList;
    public LinkedList<User_Recruitment_get_set> mLinkedList;
    public Home_list_get_set mHome_list_get_set;


    //创建构造函数
    public Home_RecyclerView_Adapter(Context context, LinkedList<Home_list_get_set> industrytitlegetsetList) {
        //将传递过来的数据,赋值给本地变量
        this.context = context;//上下文
        this.LinkedList = industrytitlegetsetList;//实体类数据ArrayList
    }

    /**
     * 创建viewhodler,相当于listview中getview中的创建view和viewhodler
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public myViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView user_peesr_info;//自定义recyclerveiw的适配器

        //创建自定义布局
//        View itemView = View.inflate(context, R.layout.peers_list_item, null);//用这个布局item宽高无效
//        myViewHodler itemView = new myViewHodler(LayoutInflater.from(context).inflate(R.layout.peers_list_item, parent, false));
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.peers_list_item, parent, false);
        user_peesr_info = view.findViewById (R.id.user_peesr_info);
        //给嵌套的RecyclerView设置适配器
        User_Recruitment_Adapter user_recruitment_adapter = new User_Recruitment_Adapter (context,mLinkedList);
        user_peesr_info.setAdapter (user_recruitment_adapter);
        user_peesr_info.setLayoutManager (new LinearLayoutManager (context, LinearLayoutManager.HORIZONTAL, false));
        //给嵌套的RecyclerView设置item的分割线
        user_peesr_info.addItemDecoration (new DividerItemDecoration (context, DividerItemDecoration.HORIZONTAL));
        return new myViewHodler(view);
//        return itemView;
    }

    /**
     * 绑定数据,数据与view绑定
     *
     * @param holder
     * @param position
     */
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(myViewHodler holder, int position) {
        //根据点击位置绑定数据
        mHome_list_get_set = LinkedList.get(position);
//        holder.mItemGoodsImg;
        holder.user_name.setText(mHome_list_get_set.getUser_name());
        holder.user_industry.setText(mHome_list_get_set.getUser_work());
//        holder.user_portrait.setImageDrawable (data.getHead_portait ());//改为下面Glide获取图上
                Glide.with(context)
                .load(mHome_list_get_set.getHead_portait ())//图片信息
                .apply(RequestOptions.bitmapTransform(new CircleCrop ()))//设置圆形
                .into(holder.user_portrait);//设置到那个部位
        holder.name_voip.setText(mHome_list_get_set.getUser_Vip());
        holder.user_location.setText(mHome_list_get_set.getUser_location());

    }

    /**
     * 得到总条数
     *
     * @return
     */
    @Override
    public int getItemCount() {
//        return LinkedList.size();
        return LinkedList == null ? 0 : LinkedList.size();
    }

    //自定义viewhodler
    class myViewHodler extends RecyclerView.ViewHolder {
        private ImageView user_portrait;
        private TextView user_name;
        private TextView user_industry;
        private TextView name_voip;
        private TextView user_location;
        public RecyclerView user_peesr_info;//自定义recyclerveiw的适配器

        public myViewHodler(View itemView) {
            super(itemView);

            user_peesr_info = itemView.findViewById (R.id.user_peesr_info);
            user_portrait = itemView.findViewById(R.id.user_portrait);
            user_name = itemView.findViewById(R.id.user_name);
            user_industry = itemView.findViewById(R.id.user_industry);
            name_voip = itemView.findViewById(R.id.name_voip);
            user_location = itemView.findViewById(R.id.user_location);

            user_portrait.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //可以选择直接在本位置直接写业务处理
                    //Toast.makeText(context,"点击了xxx",Toast.LENGTH_SHORT).show();
                    //此处回传点击监听事件
                    if (onItemClickListener != null) {
                        onItemClickListener.OnItemClick(v, LinkedList.get(getLayoutPosition()));

                    }
                }
            });


        }
        public View getItemView() {
            return itemView;

        }
    }

    /**
     * 设置item的监听事件的接口
     */
    public interface OnItemClickListener {
        /**
         * 接口中的点击每一项的实现方法
         *
         * @param view 点击的item的视图
         * @param data 点击的item的数据
         */
        public void OnItemClick(View view, Home_list_get_set data);

    }

    //需要外部访问,所以需要设置set方法,方便调用
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


第二个Recyclerview的适配器:

public class User_Recruitment_Adapter extends RecyclerView.Adapter<User_Recruitment_Adapter.myViewHodler> {
    public LinkedList<User_Recruitment_get_set> m_LinkedList;
    private Context context;
    public User_Recruitment_get_set mUser_Recruitment_get_set;
    //创建构造函数
    public User_Recruitment_Adapter(Context context, LinkedList<User_Recruitment_get_set> mLinkedList) {
        //将传递过来的数据,赋值给本地变量
        this.context = context;//上下文
        this.m_LinkedList = mLinkedList;

    }
    @NonNull
    @Override
    public myViewHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.user_peers_recruitment_item, viewGroup, false);

        return new myViewHodler(view);
    }

    @Override
    public void onBindViewHolder(@NonNull myViewHodler myViewHodler, int i) {
        mUser_Recruitment_get_set = m_LinkedList.get(i);
        myViewHodler.recruitment.setText (mUser_Recruitment_get_set.getRecruitment ());



    }

    @Override
    public int getItemCount() {
        return m_LinkedList == null ? 0 : m_LinkedList.size();
    }

    public class myViewHodler extends RecyclerView.ViewHolder {
            private TextView recruitment;
        public myViewHodler(@NonNull View itemView) {
            super (itemView);
            recruitment = itemView.findViewById(R.id.recruitment);
        }
    }
}

现问题是嵌套里面的Recyclerview无显示内容?我该如何写?

  • 写回答

4条回答 默认 最新

  • 一副要死的样子, 2020-04-01 08:51
    关注

    setNestedScrollingEnabled(false);内层嵌套Recyclerview设置这个方法。如果最外层有scrollView,外层recyclerview也setNestedScrollingEnabled(false);同时scrollview用NestedScrollView代替

    评论
    1人已打赏

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站