在TabLayout和ViewPager2中执行异步任务后,以相同的布局更新多个片段

问题描述

我正在使用 TabLayout ViewPager2 显示最多3个具有相同布局的片段。每个标签都有一个片段。

在异步任务完成后,所有3个片段都应更新其自己的ListView。所有片段共享相同的布局,但是每个片段都有自己的ListView内容

用户单击特定按钮时,将触发异步任务。

我认为整个代码会一团糟,所以这里是主要代码,我认为应该足够了:

MainActivity.class

// here we fire the async task if "buttonrefesh" is clicked

public boolean onoptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.buttonrefresh) //refresh
        new FetchAnuncios().execute();
    return super.onoptionsItemSelected(item);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Attaching the layout to the toolbar object
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    // Setting toolbar as the ActionBar with setSupportActionBar() call
    setSupportActionBar(toolbar);
    viewPager = findViewById(R.id.viewPager);
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    pagerAdapter = new MyPagerAdapter(MainActivity.this);
    viewPager.setAdapter(pagerAdapter);
    new TabLayoutMediator(tabLayout,viewPager,(tab,position) -> tab.setIcon(icons[position])).attach();
}

MyPagerAdapter.class

private class MyPagerAdapter extends FragmentStateAdapter {
    
    public MyPagerAdapter(FragmentActivity fa) {
            super(fa);
    }

    @Override
    public Fragment createFragment(int pos) {
        switch (pos) {
            case 0: {
                return Fragment_ma.newInstance("fragment 1");
            }
            case 1: {
                return Fragment_ma.newInstance("fragment 2");
            }
            case 2: {
                return Fragment_ma.newInstance("fragment 3");
            }
            default:
                return Fragment_ma.newInstance("fragment 1,Default");
        }
    }

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

}

Fragment_ma.class

public class Fragment_ma extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_ma,container,false);
    return v;
}

public static Fragment_ma newInstance(String text) {

    Fragment_ma f = new Fragment_ma();
    return f;
}

}

FetchAnuncios.class

    private class FetchAnuncios extends AsyncTask<Void,Integer,ArrayList<AnuncioData>> {        
    
    @Override
    protected ArrayList<AnuncioData> doInBackground(Void... voids) {

        ArrayList<AnuncioData> arrayList = new ArrayList<AnuncioData>();

        // here we do some I/O work to get arrayList with all the data that should be attached to each fragment ListView
        return arrayList;

    }

    
    @Override
    protected void onPostExecute(ArrayList<AnuncioData> arrayList) {
        super.onPostExecute(arrayList);
        
        final ListView list = findViewById(R.id.list); // I kNow here is the problem,but can't imagine how to solve it
        
        list.setAdapter(null);
        if (arrayList.size()>0) {
            CustomAdapter customAdapter = new CustomAdapter(MainActivity.this,arrayList);
            list.setAdapter(customAdapter);
        }


    }
}

如您所见,方法“ onPostExecute”是一种应使用存储在arrayList(每个ListView的不同内容)上的数据更新每个片段的3个ListView的方法。我该如何实现?片段是在我单击选项卡时创建的,而不是之前创建的(最初可见的第一个片段除外)。那么我什么时候应该连接listview适配器?

解决方法

使用这部分代码更新您的代码,希望它会有所帮助并删除这部分代码 “ list.setAdapter(null);”

if (arrayList.size()>0) {
    if(int i=0; i<arrayList.size()-1; i++){
        CustomAdapter customAdapter = new CustomAdapter(MainActivity.this,arrayList.get(i));
        list.setAdapter(customAdapter);
      }
    }
,

我终于开始工作了。 主要变化:

  • 在doBackground中创建MyPagerAdapter
  • 在将适配器设置为viewpager之前,将arrayList传递给MyPagerAdapter实例
  • 在片段列表类(onCreate)上设置列表适配器