我有一个包含framelayout的活动。在其中,我有一个包含ViewPager的片段,里面有两个不同的片段。当我按下给定片段中的按钮时,我用新片段替换framelayout容器。现在,当我按下ViewPager视图时,它没有恢复,它就消失了。
这是第一个片段:
public class CurrencyFragment extends BaseFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
// onclick method for the refresh button on the action bar
// the method itself calls the anoteher method getCurrency()
// it also sets visibility to progress bar and loading text and
// sets invisible to the listview
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Log.i("TAG", "refresh pressed =>");
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
// oncreateView() where the basic view is created
@Override
public View onCreateView(final LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_currency_layout,
container, false);
mBankName = (TextView) view.findViewById(R.id.Currency_name);
mBuySell = (LinearLayout) view.findViewById(R.id.Buy_Sell);
mButtonBuy = (TextView) view.findViewById(R.id.Button_buy);
mButtonSell = (TextView) view.findViewById(R.id.Button_sell);
mViewpager = (ViewPager) view.findViewById(R.id.Detailpager);
Bundle bundle = getArguments();
if(bundle != null){
Bank object = (Bank) bundle.getSerializable("message");
mBankName.setText(object.getName());
code = object.getCode();
}
testadapter = new ViewPagerAdapter(getFragmentManager(),code);
mViewpager.setAdapter(testadapter);
if(code.equals("NBG")){
mBuySell.setVisibility(View.GONE);
mViewpager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
}else{
mBuySell.setVisibility(View.VISIBLE);
}
mViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if (position == 0){
mButtonBuy.setBackgroundColor(Color.parseColor("#2FCEB8"));
mButtonSell.setBackgroundColor(Color.parseColor("#333333"));
}
else{
mButtonSell.setBackgroundColor(Color.parseColor("#2FCEB8"));
mButtonBuy.setBackgroundColor(Color.parseColor("#333333"));
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
mButtonBuy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mButtonBuy.setBackgroundColor(Color.parseColor("#2FCEB8"));
mButtonSell.setBackgroundColor(Color.parseColor("#333333"));
mViewpager.setCurrentItem(0);
}
});
mButtonSell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mButtonSell.setBackgroundColor(Color.parseColor("#2FCEB8"));
mButtonBuy.setBackgroundColor(Color.parseColor("#333333"));
mViewpager.setCurrentItem(1);
}
});
return view;
}
}
用其他
替换后public class DetailedCalculator extends BaseFragment {
private View detailedView;
private TextView mButtonBuy;
private TextView mButtonSell;
private TextView mDetailedBank;
private ViewPager mDetailpager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
DetailPagerAdapter myAdapter = new DetailPagerAdapter(getFragmentManager());
detailedView = inflater.inflate(R.layout.detail_buy_sell, container, false);
mButtonBuy = (TextView) detailedView.findViewById(R.id.Detail_buying);
mButtonSell = (TextView) detailedView.findViewById(R.id.Detail_selling);
mDetailedBank = (TextView) detailedView.findViewById(R.id.Detail_BankNAme);
mDetailpager = (ViewPager) detailedView.findViewById(R.id.Detailpager);
mDetailpager.setAdapter(myAdapter);
mDetailpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position == 0){
mButtonBuy.setBackgroundColor(Color.parseColor("#2FCEB8"));
mButtonSell.setBackgroundColor(Color.parseColor("#333333"));
}
else{
mButtonSell.setBackgroundColor(Color.parseColor("#2FCEB8"));
mButtonBuy.setBackgroundColor(Color.parseColor("#333333"));
}
}
@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
}
});
mButtonBuy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mButtonBuy.setBackgroundColor(Color.parseColor("#2FCEB8"));
mButtonSell.setBackgroundColor(Color.parseColor("#333333"));
mDetailpager.setCurrentItem(0);
}
});
mButtonSell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mButtonSell.setBackgroundColor(Color.parseColor("#2FCEB8"));
mButtonBuy.setBackgroundColor(Color.parseColor("#333333"));
mDetailpager.setCurrentItem(1);
}
});
return detailedView;
}
}
答案 0 :(得分:1)
尝试在包含getFragmentManager()
的片段中使用getChildFragmentManager()
代替ViewPager
:
testadapter = new ViewPagerAdapter(getFragmentManager(),code);
到
testadapter = new ViewPagerAdapter(getChildFragmentManager(),code);