Android RecyclerView in选项卡不显示任何项目

Android RecyclerView in选项卡不显示任何项目,android,android-fragments,android-recyclerview,Android,Android Fragments,Android Recyclerview,在创建标题时,有人向我提出了类似的问题。 我看了他们所有人,但我没有找到我想要的答案,所以我走了 我有一个片段“计划”,它将容纳不同的片段。 它将使用名为“PlanningListItemFragment”的同一个ListFragment,具有三个选项卡的TableLayout 它将保存一个RecycleView,该视图将使用“PlanningItemFragment”和适配器“RecycleWebAdapter”添加项 我的应用程序运行平稳,没有任何错误。 不幸的是,在我的任何选项卡中都看不到

在创建标题时,有人向我提出了类似的问题。 我看了他们所有人,但我没有找到我想要的答案,所以我走了

我有一个片段“计划”,它将容纳不同的片段。 它将使用名为“PlanningListItemFragment”的同一个ListFragment,具有三个选项卡的TableLayout

它将保存一个RecycleView,该视图将使用“PlanningItemFragment”和适配器“RecycleWebAdapter”添加项

我的应用程序运行平稳,没有任何错误。 不幸的是,在我的任何选项卡中都看不到任何项目。 我不明白问题出在哪里,我希望你能帮助我。 代码如下:

碎片规划:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_planning,
                container, false);
        final Button chooseDate = (Button)view.findViewById(R.id.chooseDate);

        // Code for setting the date with calendar
        chooseDate.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {

                                        DialogFragment datePicker = new DatePickerFragment();
                                        datePicker.show(getChildFragmentManager(), "date picker");

                                      }
                                      });

        //code for the tablayout

        tabLayout = (TabLayout)view.findViewById(R.id.planning_tabs);
        viewPager = (ViewPager)view.findViewById(R.id.viewpager);
        FragmentManager cfManager = getChildFragmentManager();
        adapter = new ViewPagerAdapter(cfManager);

        // add Fragment Here

        adapter.addFragment(new PlanningListItemFragment(),"Breakfast");
        adapter.addFragment(new PlanningListItemFragment(),"Lunch");
        adapter.addFragment(new PlanningListItemFragment(),"Dinner");
        // set Icons to tabs

        // tablayout.getTabAt(0).setIcon(R.drawable.X1):
        // tablayout.getTabAt(1).setIcon(R.drawable.X2):
        // tablayout.getTabAt(2).setIcon(R.drawable.X3):

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        // remove shadow from the action bar
        // ActionBar actionBar = getSupportActionbar();
        // actionBar.setElevation(0);


        return view;
    }
如您所见,它有3个选项卡

PlanningListItemFragment:

public class PlanningListItemFragment extends Fragment {

    View v;
    private RecyclerView mrecyclerview;
    private List<PlanningItemFragment> lstPlanningItem;

    public PlanningListItemFragment() {

    }

    @Nullable
    //@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_planning_list, container, false);
        mrecyclerview = v.findViewById(R.id.RCview);
        RecyclerViewAdapter recyclerAdapter = new RecyclerViewAdapter(getContext(),lstPlanningItem);
        mrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mrecyclerview.setAdapter(recyclerAdapter);
        Log.d("debugMode", "The onCreateView method has been launched");
        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // TODO: 22/05/2018 Implement the call to get the meal picture from the database

        lstPlanningItem = new ArrayList<>();
        lstPlanningItem.add(new PlanningItemFragment("Menu A", "keywords", R.drawable.ic_nav_ic_meal));




    }
}
public class PlanningItemFragment {

    private String Name;
    private String Keywords;
    private int Photo;


    public PlanningItemFragment(String name, String keywords, int photo) {
        Name = name;
        Keywords = keywords;
        Photo = photo;
    }

    // getter

    public String getName(){
        return Name;
    }

    public String getKeywords(){
        return Keywords;
    }

    public int getPhoto() {
        return Photo;
    }
项目规划xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/img_meal"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="Item Name"
            android:id="@+id/item_name"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Key words"
            android:id="@+id/keywords"/>
    </LinearLayout>



</LinearLayout>

回收服务适配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{

Context mContext ;
List<PlanningItemFragment> mData;

    public RecyclerViewAdapter(Context mContext, List<PlanningItemFragment> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v ;
        v = LayoutInflater.from(mContext).inflate(R.layout.item_planning,parent,false);
        ViewHolder vHolder = new ViewHolder(v);
        return vHolder;
    }

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

        holder.tv_name.setText(mData.get(position).getName());
        holder.tv_keywords.setText(mData.get(position).getKeywords());
        holder.img_photo.setImageResource(mData.get(position).getPhoto());
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_name;
        private TextView tv_keywords;
        private ImageView img_photo;

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

            tv_name = itemView.findViewById(R.id.item_name);
            tv_keywords =  itemView.findViewById(R.id.keywords);
            img_photo = itemView.findViewById(R.id.img_meal);

        }
    }



}
公共类RecycleServiceAdapter扩展了RecyclerView.Adapter{
语境;
列出mData;
公共RecycleServiceAdapter(上下文mContext,列表mData){
this.mContext=mContext;
this.mData=mData;
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
观点五;
v=布局从(mContext)变平。充气(R.布局。项目计划,父项,假);
视窗夹持器vHolder=新视窗夹持器(v);
返回vHolder;
}
@凌驾
公共无效onBindViewHolder(ViewHolder,int位置){
holder.tv_name.setText(mData.get(position.getName());
holder.tv_keywords.setText(mData.get(position.getKeywords());
holder.img_photo.setImageResource(mData.get(position.getPhoto());
}
@凌驾
public int getItemCount(){
返回mData.size();
}
公共静态类ViewHolder扩展了RecyclerView.ViewHolder{
私有文本视图tv_名称;
私有文本视图tv_关键字;
私人图像查看img_照片;
公共视图持有者(视图项视图){
超级(项目视图);
tv_name=itemView.findviewbyd(R.id.item_name);
tv_keywords=itemView.findviewbyd(R.id.keywords);
img_photo=itemView.findviewbyd(R.id.img_餐);
}
}
}
规划XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".Fragments.planning">


    <Button
        android:id="@+id/chooseDate"
        style="@style/Widget.AppCompat.Button.Borderless.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="5dp"
        android:elevation="0dp"
        android:hint="Tap to select a date!"
        android:textColorHint="@color/blue"

        android:textSize="20sp" />

    <TextView
        android:id="@+id/dateGiven"
        android:textAlignment="center"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:textColor="@color/blue" />

    <android.support.v7.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="match_parent">



    <android.support.design.widget.TabLayout
        android:id="@+id/planning_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabTextColor="@color/blue"
        app:tabSelectedTextColor="@color/blue"
        />

    <android.support.v4.view.ViewPager
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/viewpager">


    </android.support.v4.view.ViewPager>
    </android.support.v7.widget.LinearLayoutCompat>

</LinearLayout>

我在此网站上发现的大多数问题都与getItemCount()返回0有关。但事实并非如此

我希望你能给我一些见解,因为我的日志没有显示任何错误,我迷路了。。。 不用说,我对这方面还很陌生,还有很多东西要学


谢谢

@jonamar请尝试更新此行

仅供参考,我很久以前也遇到过类似的问题,据我记忆所及,我已经更新了下面的代码,一切正常

mrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));

(我没有足够的学分发表评论)

在设置适配器之前,是否可以记录列表lstPlanningItem的大小? 看看是不是零。 尝试取消注释@Override for onCreateView,并将列表初始化和更新部分移动到onCreateView()

@Nullable
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
v=充气机。充气(R.布局图.碎片计划表,容器,假);
lstPlanningItem=newarraylist();
mrecyclerview=v.findViewById(R.id.RCview);
RecycleServiceAdapter RecycleAdapter=新的RecycleServiceAdapter(getActivity(),lstPlanningItem);
setLayoutManager(新的LinearLayoutManager(getActivity());
mrecyclerview.setAdapter(回收适配器);
添加(新的PlanningItemFragment(“菜单A”、“关键字”、R.drawable.ic_导航_ic_膳食”);
Log.wtf(“我的列表的大小”,Integer.toString(lstPlanningItem.size());
recyclerAdapter.notifydatasetchanged();
Log.d(“debugMode”,“onCreateView方法已经启动”);
返回v;
}

正如Mike M所说,答案如下:

“Planning XML”布局中的内部LinearLayoutCompat没有指定方向,因此它是水平的。由于TabLayout的布局宽度为match\u parent,因此ViewPager被推出屏幕外的一侧。您可以在该LinearLayoutCompat上指定垂直方向,也可以将其删除,因为根LinearLayout已经是垂直的,而LinearLayoutCompat并不是真正需要的


非常感谢你

我们可以看到您的片段吗?我添加了xml,谢谢“规划xml”布局中的内部
LinearLayoutCompat
没有指定
方向,因此它是
水平的。由于
TabLayout
layout\u width
match\u parent
,因此
ViewPager
被推出屏幕外侧。您可以在
LinearLayoutCompat
上指定
vertical
方向,也可以将其删除,因为根
LinearLayout
已经是
vertical
,并且
LinearLayoutCompat
不是必需的。感谢您的回答,很遗憾,它没有帮助。回收服务中仍然没有错误和项目感谢您的回复!当我这样做时,我确实会尝试在com.example.android.meat_timealpha10.Fragments.RecycleServiceAdapter.getItemCount上的空对象引用上调用接口方法“int java.util.List.size()”,所以它看起来是“0”,但为什么呢?我认为我做的一切都是对的…我已经编辑了我的代码。试试看。我已将getContext()更改为getActivity()。我认为那无关紧要。此外,我还尝试在列表初始化和更新之后进行日志记录(这意味着列表不能为空)。请查看它记录的列表大小。再次感谢。我已经实施了更改,并确认列表不再为空:E/size_of_my_list:1,但仍然没有显示任何项目:(您现在是否有任何错误?只是没有显示任何项目?没有任何错误。只是没有显示任何项目。如果有帮助,我可以上传一张图片。)
mrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
LinearLayoutManager llm= new LinearLayoutManager(getActivity());
mrecyclerview.setLayoutManager(llm);
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_planning_list, container, false);
    lstPlanningItem = new ArrayList<>();

    mrecyclerview = v.findViewById(R.id.RCview);
    RecyclerViewAdapter recyclerAdapter = new RecyclerViewAdapter(getActivity(),lstPlanningItem);
    mrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
    mrecyclerview.setAdapter(recyclerAdapter);

    lstPlanningItem.add(new PlanningItemFragment("Menu A", "keywords", R.drawable.ic_nav_ic_meal));
    Log.wtf("size_of_my_list", Integer.toString(lstPlanningItem.size()));
    recyclerAdapter.notifydatasetchanged();
    Log.d("debugMode", "The onCreateView method has been launched");
    return v;
}