Fragment 的 Recycler 视图中没有数据

No Data in Recycler View in Fragment

提问人:Hasnain Ghias 提问时间:2/11/2020 最后编辑:EpicPandaForceHasnain Ghias 更新时间:1/22/2021 访问量:355

问:

我正在构建一个具有选项卡布局的 Android 应用程序,其中有 Fragment,并且在 Fragment 中我显示了一些值。 我刚刚只添加了硬编码值来测试布局,但即使这些值在片段中也不可见。

下面是 ViewPagerAdapter 的代码:

public class FavouriteViewPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> favouriteFragmentList = new ArrayList<>(); // this line can cause crashes
    private final List<String> favouriteFragmentListTitles = new ArrayList<>();


    public FavouriteViewPagerAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, behavior);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return favouriteFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return favouriteFragmentListTitles.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return favouriteFragmentListTitles.get(position);
    }

    public void AddFragment(Fragment fragment, String title) {
        favouriteFragmentList.add(fragment); // this line can cause crashes
        favouriteFragmentListTitles.add(title);
    }
}

我已经尝试了可用的答案,但没有一个能解决我的问题。 我调试了应用程序,我的 RecyclerViewAdapter 甚至没有被调用,非常奇怪。我已经做过很多次了,但这从未发生过。 这是我的 RecyclerViewAdapter 代码:

public class ProductsRecyclerAdapter extends RecyclerView.Adapter<ProductsRecyclerAdapter.viewHolder> {

    Context context;
    private List<FavouriteProducts> favouriteProductsData;

    public ProductsRecyclerAdapter(Context context, List<FavouriteProducts> favouriteProductsData) {
        this.context = context;
        this.favouriteProductsData = favouriteProductsData;
    }

    @NonNull
    @Override
    public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.single_favourite_product_item, parent, false);
        viewHolder vHolder = new viewHolder(v);
        return vHolder;
    }

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

        holder.product_name.setText(favouriteProductsData.get(position).getProduct_name());
        holder.product_price.setText(favouriteProductsData.get(position).getProduct_price());
        holder.product_image.setImageResource(favouriteProductsData.get(position).getProduct_image());
        Toast.makeText(context,favouriteProductsData.get(position).getProduct_price() , Toast.LENGTH_SHORT).show();
        /*holder.heart_button.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                showSuccessToast(context,"Liked");
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                showSuccessToast(context,"Unliked");
            }
        });*/

    }

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

    static class viewHolder extends RecyclerView.ViewHolder {

        MaterialTextView product_name, product_price;
        ImageView product_image;
        LikeButton heart_button;

        public viewHolder(@NonNull View itemView) {
            super(itemView);
            product_name = itemView.findViewById(R.id.product_name);
            product_price = itemView.findViewById(R.id.product_price);
            product_image = itemView.findViewById(R.id.product_image);
            heart_button = itemView.findViewById(R.id.heart_button);

        }
    }
}

我没有收到任何错误,但在我检查后甚至没有调用适配器。一些帮助会很有帮助。

片段代码:

public class FavouriteProductsFragment extends Fragment {

    View v;
    private List<FavouriteProducts> productList;

    public FavouriteProductsFragment() {
    }


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

        productList = new ArrayList<>();
        productList.add(new FavouriteProducts("All Weather", "200/Ltr", R.drawable.all_weather));
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.products_favourite_fragment, container, false);
        RecyclerView productRecyclerView = v.findViewById(R.id.favourite_products_rv);
        ProductsRecyclerAdapter recyclerAdapter = new ProductsRecyclerAdapter(getContext(), productList);
        productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        productRecyclerView.hasFixedSize();
        productRecyclerView.setAdapter(recyclerAdapter);
        return v;
    }

}
Android 片段 android-recyclerview android-tablayout

评论


答:

1赞 Maulik Dadhaniya 2/11/2020 #1

您需要添加

RecyclerView productRecyclerView = v.findViewById(R.id.favourite_products_rv);
ProductsRecyclerAdapter recyclerAdapter = new ProductsRecyclerAdapter(getContext(), productList);
productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
productRecyclerView.hasFixedSize();
productRecyclerView.setAdapter(recyclerAdapter);
productList = new ArrayList<>();
productList.add(new FavouriteProducts("All Weather", "200/Ltr",R.drawable.all_weather));recyclerAdapter.notifyDataSetChanged()

并删除 oncreate() 方法

评论

0赞 Hasnain Ghias 2/11/2020
您的意思是删除 oncreate() 并将其添加到 onCreateView() 中?
0赞 Hasnain Ghias 2/11/2020
这给了我getItemCount()中的NullPointerException
0赞 Maulik Dadhaniya 2/11/2020
初始化私有 List<FavouriteProducts> productList = new ArrayList<>() 全局@HasnainGhias
0赞 Hasnain Ghias 2/11/2020
还是没有运气兄弟
2赞 khushbu movaliya 2/11/2020 #2
productList = new ArrayList<>();
productList.add(new FavouriteProducts("All Weather", "200/Ltr",R.drawable.all_weather));
recyclerAdapter.notifyDataSetChanged()
RecyclerView productRecyclerView = v.findViewById(R.id.favourite_products_rv);
ProductsRecyclerAdapter recyclerAdapter = new ProductsRecyclerAdapter(getContext(), productList);
productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
productRecyclerView.hasFixedSize();
productRecyclerView.setAdapter(recyclerAdapter);

评论

0赞 khushbu movaliya 2/11/2020
您需要先在 RecyclerView 中设置的 List.After 中添加数据,这样您就不会出现空指针异常。
0赞 Hasnain Ghias 2/11/2020
仍然不会调用相同的 RecyclerViewDapter 方法,而不会出现错误
1赞 khushbu movaliya 2/11/2020 #3

尝试 productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));用于在 RecyclerView 中设置适配器。

评论

0赞 Hasnain Ghias 2/11/2020
谢谢你的努力,但仍然没有运气
0赞 Nimesh Patel 2/12/2020 #4

步骤1:首先为您的ViewPager创建一个或多个Fragment,然后在此片段中根据您的要求添加Recyclerview代码。

如下代码所示

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

    // Inflate the layout for this fragment
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_whatsapp_image, container, false);
    context = getActivity();
    ((HomeActivity) getActivity()).toolbar.setVisibility(View.VISIBLE);
    setupView(root);
    return root;
}

private void setupView(ViewGroup root) {
    recyclerView = (RecyclerView) root.findViewById(R.id.recyclerViewImage);
    progressBar=root.findViewById(R.id.simpleProgressBar);
    txtinfo=root.findViewById(R.id.txtinfo);
        setUPList();

}

Step2: Then Create ViewPager Adapter for Your TabLayout

public class ViewPagerAdapter extends FragmentPagerAdapter {
    public ViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        if(position == 0) return new AudioFragment();
        if(position == 1) return new VideoFragment();
        if(position == 2) return new ImageFragment();

        throw new IllegalStateException("Unexpected position " + position);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if(position == 0) return "Audio";
        if(position == 1) return "Video";
        if(position == 2) return "Image";
      
        throw new IllegalStateException("Unexpected position " + position);
    }
}

Step3 : 然后在活动的 Oncreate 方法中调用 SetupViewpager 方法

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar=findViewById(R.id.toolbar);
    viewPager=findViewById(R.id.viewpager);
    tabLayout=findViewById(R.id.tabs);

    //add toolbar
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setupViewPager(viewPager);

        tabLayout.setupWithViewPager(viewPager);
        setTabIcons();
}

注意:您可以从以下链接下载 ViewPager 代码。点击这里查看和下载

评论

0赞 Hasnain Ghias 2/12/2020
我已经完成了所有这些,但仍然没有显示数据的运气。即使我的相同代码适用于其他应用程序
0赞 Nimesh Patel 2/12/2020
将把你的活动代码,因为我想检查天气,你加载你的FavoriteViewPagerAdapter在活动中。
0赞 Hasnain Ghias 2/12/2020
viewPagerAdapter = new FavouriteViewPagerAdapter(getSupportFragmentManager());viewPagerAdapter.AddFragment(new FavouriteProductsFragment(), “产品”);viewPagerAdapter.AddFragment(new FavouriteVendorFragment(), “供应商”);viewPagerAdapter.AddFragment(new FavouriteProfessionalFragment(), “专业”);favourite_viewpager.setAdapter(viewPagerAdapter);favourite_tabs_layout.setupWithViewPager(favourite_viewpager);
0赞 Hasnain Ghias 2/12/2020
您可以在此处查看整个代码,bitbucket.org/fiazsial/welist-android-app/src/master