选项卡中的RecyclerView未显示任何项目

时间:2018-05-23 18:02:53

标签: android android-fragments android-recyclerview recycler-adapter

在创建标题时,向我建议了类似的问题。 我看了一眼他们,但我找不到我想要的答案,所以我走了。

我有一个片段"规划"这将包含不同的片段。 它将有一个带有三个选项卡的TabLayout,使用名为" PlanningListItemFragment"的相同ListFragment。

它将包含一个recyclerview,它将使用" PlanningItemFragment"添加项目。和适配器" RecyclerViewAdapter"。

我的应用程序运行顺畅,完全没有错误。 不幸的是,我的任何标签中都看不到任何项目。 我不明白问题所在,我希望你能帮助我。 这是代码:

片段规划:

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




    }
}

log.d向我显示正在正确调用OnCreateView方法。

FragmentPlanningList.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/RCview">


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


</LinearLayout>

PlanningItemFragment:

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

Item_Planning 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>

RecyclerViewAdapter:

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

        }
    }



}

规划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有关。但这不是这里的情况。

我希望你能给我一些见解,因为我的logcat没有显示任何错误我丢失了... 毋庸置疑,我对这些东西很陌生,还有很多需要学习的东西。

谢谢!

3 个答案:

答案 0 :(得分:0)

@jonamar请尝试更新此行,

仅供参考我很长一段时间也遇到过类似的问题,到目前为止我记得我在代码下面有更新,一切正常。

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

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

答案 1 :(得分:0)

(我没有足够的评论来评论)

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

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

答案 2 :(得分:0)

正如Mike M所说,这就是答案:

“规划XML”布局中的内部LinearLayoutCompat没有指定方向,因此它是水平的。由于TabLayout的layout_width是match_parent,因此ViewPager在屏幕外被推出。你可以在LinearLayoutCompat上指定一个垂直方向,或者你可以删除它,因为根LinearLayout已经是垂直的,并且LinearLayoutCompat不是真正必要的

非常感谢你!