ViewPager 使用 CursorLoader 托管两个片段,仅加载一个加载程序
ViewPager Hosting Two Fragments with CursorLoader Loading Only One Loader
我正在尝试使用游标加载器将数据加载到托管在 viewpager 中的两个片段中。每次启动 activity 时,只有一个 fragment1 cursorloader 加载数据。 Fragment2 在方向发生变化时加载,此时 fragment1 不加载 data.The fragment1 的代码是:
public class TransactionFragment extends Fragment
implements LoaderManager.LoaderCallbacks<Cursor>{
private static final int TRANSACTION_LOADER = 0;
private int walletId;
private RecyclerView transactionRecycler;
TransactionCursorAdapter adapter;
public TransactionFragment() {
// Required empty public constructor
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
walletId=WalletViewDashboardActivity.walletId;
getActivity().getSupportLoaderManager().initLoader(TRANSACTION_LOADER, null, this);
}
@Override
public void onResume() {
super.onResume();
getActivity().getSupportLoaderManager().restartLoader(TRANSACTION_LOADER, null, this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_transaction, container, false);
transactionRecycler = (RecyclerView) rootView.findViewById(R.id.recycler_transactions);
transactionRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String sTransactionSelection =
CarepayContract.InvoiceEntry.TABLE_NAME +
"." + CarepayContract.InvoiceEntry.COLUMN_WALLET_ID + " = ? ";
String[] sArguments = {String.valueOf(walletId)};
return new CursorLoader(
getActivity(),
CarepayContract.InvoiceEntry.CONTENT_URI,
null,
sTransactionSelection,
sArguments,
null
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.e("Transactions", String.valueOf(data.getCount()));
adapter = new TransactionCursorAdapter(getActivity(), data);
transactionRecycler.setAdapter(adapter);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}}
第二个片段的代码是:
public class DependantFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private RecyclerView recyclerView;
private static final int DEPENDANT_LOADER = 0;
private int walletId;
private DependantCursorAdapter adapter;
public DependantFragment() {}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
walletId=WalletViewDashboardActivity.walletId;
recyclerView.setAdapter(adapter);
getActivity().getSupportLoaderManager().initLoader(DEPENDANT_LOADER, null, this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
recyclerView.setAdapter(adapter);
getActivity().getSupportLoaderManager().restartLoader(DEPENDANT_LOADER, null, this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dependant, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_dependants);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
@Override
public android.support.v4.content.Loader<Cursor> onCreateLoader(int id, Bundle args) {
String sDependantSelection =
CarepayContract.UserEntry.TABLE_NAME +
"." + CarepayContract.UserEntry.COLUMN_TYPE + " = ? AND " + CarepayContract.UserEntry.TABLE_NAME +
"." + CarepayContract.UserEntry.COLUMN_WALLET_ID + " = ?";
String[] sArguments = {"1", String.valueOf(walletId)};
return new CursorLoader(
getActivity(),
CarepayContract.UserEntry.DEPENDANT_URI,
null,
sDependantSelection,
sArguments,
null
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.e("Dependant", String.valueOf(data.getCount()));
adapter = new DependantCursorAdapter(getActivity(), data);
recyclerView.setAdapter(adapter);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
} }
主机代码 activity 是:
public class WalletViewDashboardActivity extends AppCompatActivity {
public static int walletId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wallet_view_dashboard);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
walletId = Integer.parseInt(getIntent().getStringExtra("wallet_id"));
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Transactions"));
tabLayout.addTab(tabLayout.newTab().setText("Dependants"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final WalletViewPagerAdapter adapter = new WalletViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@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) {
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public class WalletViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public WalletViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TransactionFragment();
case 1:
return new DependantFragment();
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}}
private static final int TRANSACTION_LOADER = 0;
private static final int DEPENDANT_LOADER = 0;
你用了getActivity(),所以你用了一个loadermanager,两个loader的id都设置为0,只要把其中一个常量改成1就可以了
我正在尝试使用游标加载器将数据加载到托管在 viewpager 中的两个片段中。每次启动 activity 时,只有一个 fragment1 cursorloader 加载数据。 Fragment2 在方向发生变化时加载,此时 fragment1 不加载 data.The fragment1 的代码是:
public class TransactionFragment extends Fragment
implements LoaderManager.LoaderCallbacks<Cursor>{
private static final int TRANSACTION_LOADER = 0;
private int walletId;
private RecyclerView transactionRecycler;
TransactionCursorAdapter adapter;
public TransactionFragment() {
// Required empty public constructor
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
walletId=WalletViewDashboardActivity.walletId;
getActivity().getSupportLoaderManager().initLoader(TRANSACTION_LOADER, null, this);
}
@Override
public void onResume() {
super.onResume();
getActivity().getSupportLoaderManager().restartLoader(TRANSACTION_LOADER, null, this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_transaction, container, false);
transactionRecycler = (RecyclerView) rootView.findViewById(R.id.recycler_transactions);
transactionRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String sTransactionSelection =
CarepayContract.InvoiceEntry.TABLE_NAME +
"." + CarepayContract.InvoiceEntry.COLUMN_WALLET_ID + " = ? ";
String[] sArguments = {String.valueOf(walletId)};
return new CursorLoader(
getActivity(),
CarepayContract.InvoiceEntry.CONTENT_URI,
null,
sTransactionSelection,
sArguments,
null
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.e("Transactions", String.valueOf(data.getCount()));
adapter = new TransactionCursorAdapter(getActivity(), data);
transactionRecycler.setAdapter(adapter);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}}
第二个片段的代码是:
public class DependantFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private RecyclerView recyclerView;
private static final int DEPENDANT_LOADER = 0;
private int walletId;
private DependantCursorAdapter adapter;
public DependantFragment() {}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
walletId=WalletViewDashboardActivity.walletId;
recyclerView.setAdapter(adapter);
getActivity().getSupportLoaderManager().initLoader(DEPENDANT_LOADER, null, this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
recyclerView.setAdapter(adapter);
getActivity().getSupportLoaderManager().restartLoader(DEPENDANT_LOADER, null, this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dependant, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_dependants);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
@Override
public android.support.v4.content.Loader<Cursor> onCreateLoader(int id, Bundle args) {
String sDependantSelection =
CarepayContract.UserEntry.TABLE_NAME +
"." + CarepayContract.UserEntry.COLUMN_TYPE + " = ? AND " + CarepayContract.UserEntry.TABLE_NAME +
"." + CarepayContract.UserEntry.COLUMN_WALLET_ID + " = ?";
String[] sArguments = {"1", String.valueOf(walletId)};
return new CursorLoader(
getActivity(),
CarepayContract.UserEntry.DEPENDANT_URI,
null,
sDependantSelection,
sArguments,
null
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.e("Dependant", String.valueOf(data.getCount()));
adapter = new DependantCursorAdapter(getActivity(), data);
recyclerView.setAdapter(adapter);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
} }
主机代码 activity 是:
public class WalletViewDashboardActivity extends AppCompatActivity {
public static int walletId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wallet_view_dashboard);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
walletId = Integer.parseInt(getIntent().getStringExtra("wallet_id"));
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Transactions"));
tabLayout.addTab(tabLayout.newTab().setText("Dependants"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final WalletViewPagerAdapter adapter = new WalletViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@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) {
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public class WalletViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public WalletViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TransactionFragment();
case 1:
return new DependantFragment();
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}}
private static final int TRANSACTION_LOADER = 0;
private static final int DEPENDANT_LOADER = 0;
你用了getActivity(),所以你用了一个loadermanager,两个loader的id都设置为0,只要把其中一个常量改成1就可以了