gpt4 book ai didi

java - 回收器 View 未显示在 fragment 中

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:48 27 4
gpt4 key购买 nike

我有三个 fragment : fragment 用户 fragment 主页 fragment 进度

我正在尝试在 homefragment 中实现一个 recycler View ,它可以在 recycler View 中回收图像,但没有发生任何事情,logcat 中也没有显示错误,我不知道我做错了什么。

1.这是我的自定义适配器:

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder>{
@NonNull

private LayoutInflater inflator;
List<Information> data= Collections.emptyList();

public CustomAdapter(Context context, List<Information> data){
inflator= LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=inflator.inflate(R.layout.customlayout, parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}

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

Information current=data.get(position);
holder.icon.setImageResource(current.images);
holder.title.setText(current.title);

}

@Override
public int getItemCount() {
return 0;
}

class MyViewHolder extends RecyclerView.ViewHolder{

ImageView icon;
TextView title;
public MyViewHolder(View itemView) {
super(itemView);

icon = (ImageView) itemView.findViewById(R.id.listicon);
title = (TextView) itemView.findViewById(R.id.textView);
}
}
}
  • 这是我的 fragment

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    public class HomeFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private CustomAdapter adapter;
    int[] IMAGES = {R.drawable.abs,R.drawable.arms,R.drawable.back,R.drawable.chest,R.drawable.full,R.drawable.legs};
    private RecyclerView recyclerView;


    ArrayList<HashMap<String, String>> data;


    private String mParam1;
    private String mParam2;


    public HomeFragment() {

    }
    public static HomeFragment newInstance(String param1, String param2) {
    HomeFragment fragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
    mParam1 = getArguments().getString(ARG_PARAM1);
    mParam2 = getArguments().getString(ARG_PARAM2);
    }
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    View layout = inflater.inflate(R.layout.fragment_home, container, false);
    recyclerView =(RecyclerView) layout.findViewById(R.id.drawerList);
    adapter = new CustomAdapter(getActivity(), getData());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    return layout;
    }

    public static List<Information> getData(){
    List<Information> data = new ArrayList<>();
    int [] icons = {R.drawable.abs,R.drawable.arms,R.drawable.back,R.drawable.chest,R.drawable.full,R.drawable.legs};
    String[] titles={"Abs","Arms","Back","Chest","full","legs"};
    for (int i=0; i<icons.length && i<titles.length;i++){

    Information current = new Information();
    current.images=icons[i];
    current.title=titles[i];
    data.add(current);
    }
    return data;

    }





    }
  • 我看不出我做错了什么。

    1. fragmenthome.xml

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



      <android.support.v7.widget.RecyclerView
      android:id="@+id/drawerList"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_alignParentStart="true"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true">

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

      </FrameLayout>

    4.Custom_layout.xml

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


    <ImageView
    android:id="@+id/listicon"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    app:srcCompat="@mipmap/ic_launcher" />

    <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="62dp"
    android:text="TextView" />

    最佳答案

    您没有在适配器的构造函数中分配数据

    this.data = data;

    那么您的商品数量应该是

      data.size(); 

    不应为 0。

    关于java - 回收器 View 未显示在 fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52494408/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com