我正在android中开发一个ExpenseManager应用程序,我正在实现fragmentActivity
。此fragment
会显示帐户摘要页面,其中包含帐户余额,帐户上次交易金额和其他详细信息。
AccountSummary Activity
是使用ViewPager
,ActionBar
,FragmentPageAdapter
和FragmentActivity
实施的多标签活动。
摘要页面的布局对于所有帐户都是通用的,只有数据会根据用户选择的帐户进行更改。
现在,我想实现此活动,我可以在所有帐户或(fragment
标签)中重复使用/复制相同的ActionBar
布局(而不是实例)。当用户从actionBar
中选择任何标签时,它应该加载/显示/显示片段以及该帐户数据。
(我知道我需要使用不同的Fragment
创建动态TagName
,并且需要在FragmentTransaction
的帮助下进行替换;出于某种原因,此解决方案不起作用)。
这与playstore上的应用程序类似,应用程序布局为Url
有人可以使用示例代码发布解决方案吗?我一直在努力寻找解决方案,但似乎没有任何效果。
修改
AccountSummaryTabActivity.java
public class AccountSummaryTabActivity extends FragmentActivity implements ActionBar.TabListener {
ViewPager viewPager = null;
ActionBar actionBar = null;
AccountSummaryTabAdapter accSummaryAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account_summary_tab);
DBHelper dbHelper = new DBHelper(getApplicationContext());
RuntimeExceptionDao<Account, Integer> simpleAccountDao = dbHelper.getSimpleAccountDataDao();
List<Account> accountList = simpleAccountDao.queryForAll();
viewPager = (ViewPager)findViewById(R.id.accountSummaryPager);
actionBar = getActionBar();
accSummaryAdapter = new AccountSummaryTabAdapter(getSupportFragmentManager());
accSummaryAdapter.setCount(accountList.size());
viewPager.setAdapter(accSummaryAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
String[] accountNameArr = new String[accountList.size()];
Integer i=1;
for (Account account : accountList) {
if(account.getIsPrimaryAcc().equalsIgnoreCase("Y"))
accountNameArr[0] = account.getName();
else
accountNameArr[i++] = account.getName();
}
// Adding Tabs
for (String accountName : accountNameArr) {
actionBar.addTab(actionBar.newTab().setText(accountName).setTabListener(this));
}
Intent intent = getIntent();
if(intent.getStringExtra("selectedAccNameTab")==null)
intent.putExtra("selectedAccNameTab", accountNameArr[0]);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
actionBar.setSelectedNavigationItem(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
System.out.println("onTabSelected "+tab.getText());
DBHelper dbHelper = new DBHelper(getApplicationContext());
RuntimeExceptionDao<Account, Integer> simpleAccountDao = dbHelper.getSimpleAccountDataDao();
List<Account> accountList = simpleAccountDao.queryForAll();
Intent intent = getIntent();
intent.putExtra("selectedAccNameTab", tab.getText());
accSummaryAdapter = new AccountSummaryTabAdapter(getSupportFragmentManager());
accSummaryAdapter.setCount(accountList.size());
viewPager.setAdapter(accSummaryAdapter);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
System.out.println("onTabUnselected "+tab.getText());
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
System.out.println("onTabReselected "+tab.getText());
}
}
AccountSummaryTabAdapter.java
public class AccountSummaryTabAdapter extends FragmentPagerAdapter {
FragmentManager fm=null;
Integer count = 0;
public AccountSummaryTabAdapter(FragmentManager fm) {
super(fm);
this.fm=fm;
}
public void setCount(Integer count){
this.count=count;
}
@Override
public Fragment getItem(int arg0) {
switch (arg0){
default :{
AccountSummaryFragment fragment = new AccountSummaryFragment();
return fragment;
}
}
}
@Override
public int getCount() {
return count;
}
}
AccountSummaryFragment.java
public class AccountSummaryFragment extends Fragment {
public AccountSummaryFragment() {
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_account_summary, container, false);
String accountName = getActivity().getIntent().getStringExtra("selectedAccNameTab");
DBHelper dbHelper = new DBHelper(getActivity());
RuntimeExceptionDao<Account, Integer> simpleAccountDao = dbHelper.getSimpleAccountDataDao();
List<Account> accountList = simpleAccountDao.queryForEq("name", accountName);
Account defaultAcc = accountList.get(0);
String[] accountNameArr = new String[]{"Monthly","Quaterly","Half Yearly","Yearly"};
TextView balanceTxVw = (TextView) rootView.findViewById(R.id.accountBalanceTxVw);
Double balance = defaultAcc.getBalance();
DecimalFormat df = new DecimalFormat("0.00");
balanceTxVw.setText(df.format(balance));
Spinner dropdown = (Spinner)rootView.findViewById(R.id.graphSpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, accountNameArr);
dropdown.setAdapter(adapter);
return rootView;
}
}
activity_account_summary_tab.xml布局文件
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/accountSummaryPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
提前致谢
答案 0 :(得分:0)
解决此问题的最可能方法是执行以下操作:
我希望有所帮助。让我知道它是怎么回事。