0
我有三個選項卡的主要活動,並且三個選項卡正在填充回收器視圖,同時使用帶有排空的JSONHttp請求獲取數據。但是,問題在於數據第一次被提取時,它會被填充。但是,當我從一個選項卡切換到另一個選項卡時,我無法找到再次獲取數據(如刷新它)的方法。如何在用戶在標籤之間切換時動態更新頁面片段?
我在做什麼?答片斷如下:
public class MainActivity extends AppCompatActivity {
// Class name TAG to be used for logging
private static final String TAG = MainActivity.class.getSimpleName();
// Variables
private User user;
private static ArrayList<> items;
private Fragment Fragment1, Fragment2;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
user = getIntent().getParcelableExtra("USER");
fetchItems();
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int position) {
Log.d("Main", "Switching tab");
// Update the view
super.onPageSelected(position);
}
});
}
public void fetchJobs(){
items = new ArrayList<>();
JsonArrayRequest getItems = new JsonArrayRequest(Request.Method.GET, url + "items.json?", null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray o) {
// Get JSon from Http request, this code works fine..
// items array is populated with the desired response
mSectionsPagerAdapter.notifyDataSetChanged();
....
}
如果我叫fetchItems()當我切換到一個選項卡以刷新它不會與新的條目重新填充列表。