簡體   English   中英

在 TabLayout 和 ViewPager2 中執行異步任務后更新具有相同布局的多個片段

[英]Update multiple fragments with same layout after doing Async Task in TabLayout and ViewPager2

我使用TabLayoutViewPager2以顯示最多 3 個具有相同布局的片段。 每個選項卡一個片段。

在異步任務完成后,所有 3 個片段都應該更新自己的 ListView。 所有片段共享相同的布局,但每個片段都有自己的 ListView 內容。

當用戶單擊特定按鈕時會觸發異步任務。

我認為整個代碼會一團糟,所以這里是主要代碼,我認為應該足夠了:

主Activity.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;
}

}

獲取Anuncios.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 的方法。 我怎樣才能做到這一點? 當我單擊選項卡時創建片段,而不是之前(除了最初可見的第一個)。 那么我應該什么時候附加列表視圖適配器以及如何附加?

用這部分代碼更新你的代碼希望它會有所幫助並刪除這部分代碼“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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM