在动态 viewpager 片段中创建 recyclerviews

Posted

技术标签:

【中文标题】在动态 viewpager 片段中创建 recyclerviews【英文标题】:Creating recyclerviews within dynamic viewpager fragments 【发布时间】:2020-12-27 09:02:08 【问题描述】:

我的布局中有一个 tablayout+viewpager。

片段及其选项卡是从我的数据库中的类别动态创建的,因此当用户创建或删除新类别时它们会更新。

然后我希望能够在每个选项卡中将相应类别的项目显示为 recyclerview 项目。我已经在创建/编辑它们的常规 recyclerviews 的其他地方显示了这些项目,因此只需让它们在此 viewpager 中以相同的方式显示,按选项卡排序。项目也将使用日历视图按日期过滤。

到目前为止,我有一个 DynamicFragmentAdapter:

package com.example.navgraphtest;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import org.jetbrains.annotations.NotNull;


public class DynamicFragmentAdapter extends FragmentStatePagerAdapter 
    private int mNumOfTabs;

    DynamicFragmentAdapter(FragmentManager fm, int NumOfTabs) 
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    

    @NonNull
    @Override
    public Fragment getItem(int position) 
        Bundle b = new Bundle();
        b.putInt("position", position);
        Fragment frag = DynamicFragment.newInstance();
        frag.setArguments(b);
        return frag;
    

    @Override
    public int getCount() 
        return mNumOfTabs;
    

DynamicFragment 本身,目前只显示与其类别相关的索引


package com.example.navgraphtest;

import android.content.Context;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;


public class DynamicFragment extends Fragment 
    ArrayList<CategoryModel> arrayList;


    public static DynamicFragment newInstance() 
        return new DynamicFragment();
    

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.dynamic_fragment_layout, container, false);
        initViews(view);
        return view;
    

    private void initViews(View view) 

        DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
        arrayList = new ArrayList<CategoryModel>(databaseHelper.getCategories());

        TextView textView=view.findViewById(R.id.commonTextView);
         
            textView.setText(String.valueOf("Category :  "+getArguments().getInt("position")));
        
    

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

    @Override
    public void onAttach(@NotNull Context context) 
        super.onAttach(context);
    

    @Override
    public void onPause() 
        super.onPause();
    

    @Override
    public void onResume()  super.onResume(); 

    @Override
    public void onStop() 
        super.onStop();
    

    @Override
    public void onDestroyView() 
        super.onDestroyView();
    

    @Override
    public void onDestroy() 
        super.onDestroy();
    

在主片段内

package com.example.navgraphtest;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import org.threeten.bp.LocalDate;

import java.util.ArrayList;

import static android.content.ContentValues.TAG;

public class CalendarViewFragment extends Fragment 

    private MaterialCalendarView mCalendarView;
    private CalendarDay currentDate;
    private TextView mTextViewCurrentDate;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    ArrayList<CategoryModel> arrayList;

    @Override
    public void onResume() 
        Log.d(TAG, "onResume: ");
        super.onResume();
        // Update ActionBar titles in MainActivity to reflect current fragment
        // Actionbar is in main layout not the fragments.
        ((MainActivity) getActivity()).setActionBarTitle("Calendar View");
        ((MainActivity) getActivity()).getSupportActionBar().setSubtitle("View your consumption history");

        // Open fragment with today's date selected on calendarview.
        mCalendarView.setSelectedDate(CalendarDay.today());

        // initViews for category tabs
        initViews();


    


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        Log.d(TAG, "onCreateView: ");


        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_calendar_view, container, false);



    

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) 
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG, "onViewCreated: ");

        mTextViewCurrentDate = getActivity().findViewById(R.id.currentDateTextView);
        mCalendarView = getActivity().findViewById(R.id.calendarView);
        mCalendarView.setOnDateChangedListener(new OnDateSelectedListener()
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) 
                mTextViewCurrentDate.setText(String.valueOf(date));
            

        );

        final NavController navController = Navigation.findNavController(view);
    

    private void initViews()
        Log.d(TAG, "initViews: ");

        mViewPager = getActivity().findViewById(R.id.viewPagerCalendarView);
        mTabLayout =  getActivity().findViewById(R.id.tabLayoutCalendarView);
        mViewPager.setOffscreenPageLimit(5);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() 
            @Override
            public void onTabSelected(TabLayout.Tab tab) 
                mViewPager.setCurrentItem(tab.getPosition());
            

            @Override
            public void onTabUnselected(TabLayout.Tab tab) 

            

            @Override
            public void onTabReselected(TabLayout.Tab tab) 

            
        );

        setDynamicFragmentToTabLayout();

    

    private void setDynamicFragmentToTabLayout() 
        DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
        arrayList = new ArrayList<CategoryModel>(databaseHelper.getCategories());

        Log.d(TAG, "setDynamicFragmentToTabLayout: ");

        for (int i = 0; i < arrayList.size() ; i++) 

            mTabLayout.addTab(mTabLayout.newTab().setText(arrayList.get(i).getCategoryTitle()));
        



        DynamicFragmentAdapter mDynamicFragmentAdapter = new DynamicFragmentAdapter(getActivity().getSupportFragmentManager(), mTabLayout.getTabCount());
        mViewPager.setAdapter(mDynamicFragmentAdapter);
        mViewPager.setCurrentItem(0);
    

我真的不知道如何实现这一点,没有取消用户创建类别的选项并且只有一个固定的数字,但是我不知道如何获得这些项目在 tablayout/viewpager 中显示。

【问题讨论】:

【参考方案1】:

那么你的 dynamicFragmentAdapter 应该是这样的

    public class DynamicFragmentAdapter extends FragmentStatePagerAdapter 
    private int mNumOfTabs;
    Fragment fragment = null;
    ViewPager viewPager;
    ArrayList<Menu> fragDataList=new ArrayList<>();
    private List<String> tabNameList;
    Context context;

    public DynamicFragmentAdapter (Context  context,FragmentManager fm, int mNumOfTabs, List<String> tabNameList, ArrayList<Menu> fragDataList,ViewPager viewPager) 

        super(fm);
        this.mNumOfTabs = mNumOfTabs;
        this.tabNameList = tabNameList;
        this.fragDataList=fragDataList;
        this.context=context;
        this.viewPager=viewPager;


    

    @Override
    public Fragment getItem(final int position) 
    
        for (int i = 0; i < mNumOfTabs ; i++) 
            
               fragment = GenericFragment.newInstance( fragDataList,position);
               Log.i("info",String.valueOf(position));

                break;


        
        return fragment;
    
    @Override
    public int getCount() 
        return mNumOfTabs;
    

    @Override
    public CharSequence getPageTitle(int position) 
       return tabNameList.get(position);
      //  return super.getPageTitle(position);
    

你的主要片段通过你可以从数据库中获取数据,viewpager和tablayout会是这样的

      //this method is used to set number of tabs dynamically
    private void sendData(int /*sizeof tabNames list*/ size, final List<String> tabName, final ArrayList<Menu> fragArrayList, List<Menu> positioncheck) 
        Log.d("My Msg", "Tab countert check = " + size);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() 
            int pos = 0;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);

            

            @Override
            public void onPageSelected(int position) 
                pos = position;


            

            @Override
            public void onPageScrollStateChanged(int state) 

            
        );

        final DynamicFragmentAdapter dynamicFragmentAdapter = new DynamicFragmentAdapter (getApplicationContext(), getSupportFragmentManager(), size, tabName, fragArrayList, viewPager);
        viewPager.setAdapter(dynamicFragmentAdapter );
        //viewPager.setOffscreenPageLimit(5);
        // viewPager.setCurrentItem(tabLayout.getSelectedTabPosition());
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() 
            @Override
            public void onTabSelected(TabLayout.Tab tab) 
                viewPager.setCurrentItem(tab.getPosition());
            

            @Override
            public void onTabUnselected(TabLayout.Tab tab) 

            

            @Override
            public void onTabReselected(TabLayout.Tab tab) 

            
        );
        tabLayout.setupWithViewPager(viewPager);

    

你的 dynamicFragment 会得到数据

`public class DynamicFragment extends Fragment 
    static List<fragDAta> fragDataList;
    static int loopsize = 0;
    int mnum = 0;


   public static DynamicFragment newInstance(ArrayList<fragData> fragDataList, int position) 
        DynamicFragment.fragDataList = fragDataList;
        DynamicFragment f = new DynamicFragment ();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("position", position);
        f.setArguments(args);
        DynamicFragment.loopsize = loopsize;
        return f;


    

     public DynamicFragment () 
        // Required empty public constructor
    

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        mnum = getArguments() != null ? getArguments().getInt("position") : 1;
    
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        // Inflate the layout for this fragment


        return inflater.inflate(R.layout.fragment_dynamic, container, false);
    

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) 
        super.setUserVisibleHint(isVisibleToUser);

    

 @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) 
        super.onViewCreated(view, savedInstanceState);

// here is your data
        Log.d("MYMSG", "DATA CHECK = " + fragDataList.size());


    

    @Override
    public void onResume()  
Log.d("MYMSG", "DATA CHECK = " + fragDataList.size());
super.onResume(); 

  
`

【讨论】:

您好,非常感谢您的回复。我已经尝试实施您的建议,但它不起作用。没有创建片段,也没有显示任何数据,只是带有标题的选项卡布局。 能否把代码分享给我,你是如何实现的,我可以看到并更准确地帮助你 抱歉回复晚了@Ifoste17 你得到结果了吗?如果没有,我会帮助你

以上是关于在动态 viewpager 片段中创建 recyclerviews的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法使用相同的布局动态创建片段并向它们显示数据?

如何从片段更改 Tablayout 的背景颜色?

ViewPager2 无法动态添加删除片段

在 ViewPager 中使用视图创建动态片段

使用 FragmentStatePagerAdapter 在 ViewPager 的两侧动态添加片段

Android ViewPager + 带有动态 ListViews 的片段