为啥直到在选项卡之间滚动才会显示 recyclerview 项目?

Posted

技术标签:

【中文标题】为啥直到在选项卡之间滚动才会显示 recyclerview 项目?【英文标题】:Why recyclerview items doesn't show until scroll between tabs?为什么直到在选项卡之间滚动才会显示 recyclerview 项目? 【发布时间】:2020-11-05 20:05:19 【问题描述】:

我有一个应用程序可以从活动中接收输入,并且需要在标签中实现的 recyclerview 中显示它。 在活动中按下按钮后,值不会显示在我需要的目标选项卡(第二个)中,并且只有在我滚动最后一个选项卡(4t)并返回到第二个选项卡后才会显示在那里。

代码如下:

tab2

public class Tab2Fragment extends Fragment 


    private ArrayList<exampleItem> mExampleList = new ArrayList<>();
    RecyclerView mRecyclerView;
    private exampleAdapter mAdapter = new exampleAdapter(mExampleList);
    private RecyclerView.LayoutManager mLayoutManager;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        View v = inflater.inflate(R.layout.fragment_two, container, false);
        mRecyclerView = v.findViewById(R.id.recyclerview2);

        buildRecyclerView(v);
        loadData();

        return v;
    
    public void removeItem(int position) 
        mExampleList.remove(position);
        //mExampleList.clear();
        //mRecyclerView.removeViewAt(position);
        mAdapter.notifyItemRemoved(position);
        mAdapter.notifyDataSetChanged();
    
    public void changeItem(int position, String text) 
        mExampleList.get(position).changeText1(text);
        mAdapter.notifyItemChanged(position);
    
        private void loadData() 
            SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("shared preferences12", Context.MODE_PRIVATE);
            Gson gson = new Gson();
            String json = sharedPreferences.getString("task list12", null);
            Type type = new TypeToken<ArrayList<exampleItem>>() 
            .getType();
            mExampleList = gson.fromJson(json, type);
            if (mExampleList == null) 
                mExampleList = new ArrayList<>();
            
        
        private void buildRecyclerView(View view) 
            mRecyclerView = view.findViewById(R.id.recyclerview2);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(getContext());
            mAdapter = new exampleAdapter(mExampleList);
            //mRecyclerView.swapAdapter(mAdapter,false);
            mRecyclerView.setLayoutManager(mLayoutManager);
            mAdapter.notifyDataSetChanged();
            mRecyclerView.setAdapter(mAdapter);

                        mAdapter.setOnItemClickListener(new exampleAdapter.OnItemClickListener() 
                @Override
                public void onItemClick(int position) 
                    changeItem(position, "Clicked");
                

                @Override
                public void onDeleteClickListener(int position) 
                    removeItem(position);
                
            );
        


主要活动

public class MainActivity extends AppCompatActivity 
    private TabAdapter adapter;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        adapter = new TabAdapter(getSupportFragmentManager());
        adapter.addFragment(new Tab1Fragment(), "main");
        adapter.addFragment(new Tab2Fragment(), "daily");
        adapter.addFragment(new Tab3Fragment(), "weekly");
        adapter.addFragment(new Tab4Fragment(), "monthly");
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    

【问题讨论】:

【参考方案1】:

您在 changeItem 和 removeitem 和 buildRecyclerView 函数中使用 notifyDataSetChanged()。在 loadData 和 changeItem 调用之前调用 buildRecyclerView 函数在 onItemClick 中调用我不知道与您提到您按下的按钮相同。但在 loadData 函数中,您不会调用 notifyDataSetChanged() 或其他通知函数。可能这是您的问题的原因。请检查recyclerview的数据列表每次更改后是否调用notifyDataSetChanged()或其他通知函数

【讨论】:

以上是关于为啥直到在选项卡之间滚动才会显示 recyclerview 项目?的主要内容,如果未能解决你的问题,请参考以下文章

直到最小的滚动或点击,单元格才会显示在表格视图中(注意:后台线程上没有任何反应)

使用 ajax 加载内容时在 Bootstrap 选项卡之间显示微调器

使用 UIImageView+AFNetworking,图像直到滚动才会出现

以编程方式将非活动选项卡滚动到底部

当我从一个选项卡单击到另一个选项卡时,如何使TabLayout中的选项卡不可滚动

RichEdit 控件在切换选项卡时不显示水平滚动条