如何在ViewPager中创建刷新页面

时间:2016-11-14 21:46:59

标签: android android-fragments android-viewpager

问题

我正在ViewPager使用fragment。 每个片段的布局都有一个button,点击后,运行一段代码,然后消失

如果我点击一个按钮,它会运行代码并按照编程消失。

但是如果我从2个标签的片段中移开,然后我回到它,看起来ViewPager杀死了该片段的缓存并使用其默认布局恢复它:所以按钮再次出现。

如何始终保存每个片段的状态?我不希望ViewPager只保存我正在查看的两个片段。

图片

适配器

这是我的适配器:

public class MineAdapter extends PagerAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;

public MineAdapter(Context context) {
    mContext = context;
    mLayoutInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {

    //Fill Data directly from Repository
    return CenterRepository.getSingletonInstance().getListOfLavels().size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((FrameLayout) object);
}

@Override
    public Object instantiateItem(ViewGroup container, final int position) {

    System.out.println("Code executed");

    final View itemView = mLayoutInflater.inflate(R.layout.carousal_page, container,
            false);

    switch (position) {
        case 0:
            itemView.setBackgroundResource(R.color.iron);
            break;
        case 1:
            itemView.setBackgroundResource(R.color.coal);
            break;
        case 2:
            itemView.setBackgroundResource(R.color.gold);
            break;
    }

    //Mine Name
    ((TextView) itemView.findViewById(R.id.mineName)).setText(
            CenterRepository.getSingletonInstance().getListOfLavels().get(position).getmName());

    //Mine Cost
    ((TextView) itemView.findViewById(R.id.mineCost)).setText("" +
            CenterRepository.getSingletonInstance().getListOfLavels().get(position).getUnlockCost());

    //Mine Cost
    ((TextView) itemView.findViewById(R.id.mineDropRate)).setText("" +
            CenterRepository.getSingletonInstance().getListOfLavels().get(position).getDropRate());

    //Mineral Name
    ((TextView) itemView.findViewById(R.id.mineMineral)).setText(
            CenterRepository.getSingletonInstance().getListOfLavels().get(position).getMineral().getName());

    //Mineral Drop Rate
    ((TextView) itemView.findViewById(R.id.mineDropRate)).setText("" +
            CenterRepository.getSingletonInstance().getListOfLavels().get(position).getMineral().getValue());

    // Unlock Button
    itemView.findViewById(R.id.unlockButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (CenterRepository.getSingletonInstance().getCurrentUser().getGold() >=
                    CenterRepository.getSingletonInstance().getListOfLavels().get(position).getUnlockCost()) {

                //If User has more gold than cost to unlock remove lock image and buy it

                CenterRepository.getSingletonInstance().getCurrentUser().setGold(
                        CenterRepository.getSingletonInstance().getCurrentUser().getGold()
                                - CenterRepository.getSingletonInstance().getListOfLavels().get(position).getUnlockCost()); // Update user's gold


                itemView.findViewById(R.id.unlockButton).setVisibility(View.GONE); // Remove lock button


                Toast.makeText(mContext,
                        "Reduced " + CenterRepository.getSingletonInstance().getListOfLavels().get(position).getUnlockCost() +
                                "\n Updated Gold " + CenterRepository.getSingletonInstance()
                                .getCurrentUser().getGold(), Toast.LENGTH_LONG).show();

            } else {

                // Not enough money
                Toast.makeText(mContext, "Not enough money to purchase You need " +
                        (CenterRepository.getSingletonInstance().getListOfLavels().get(position).getUnlockCost()
                                - CenterRepository.getSingletonInstance().getCurrentUser().getGold()) + "More", Toast.LENGTH_SHORT).show();
            }

        }
    });

    container.addView(itemView);

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((FrameLayout) object);
}

}

2 个答案:

答案 0 :(得分:2)

public void setOffscreenPageLimit(int limit)

是ViewPager视图所需要的。

Android developer link for ViewPager

答案 1 :(得分:0)

保存按钮在onSaveInstanceState中的可见性状态,并在onViewCreated中相应地处理它。

 @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    btnTest = (Button) view.findViewById(R.id.btnTest);
    btnTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            btnTest.setVisibility(View.GONE);
        }
    });
    if(savedInstanceState != null){
        btnTest.setVisibility(savedInstanceState.getInt("button_visibility",View.VISIBLE));
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt("button_visibility",btnTest.getVisibility());
    super.onSaveInstanceState(outState);
}