ViewPager使用视图而不是片段

时间:2016-09-23 14:22:02

标签: android android-fragments android-viewpager

ViewPager现在使用Views代替Fragments来显示每个标签。 每个选项卡都会膨胀相同的布局文件。

概述

在这个ViewPager中,我应该将地雷添加为标签,所以基本上每个标签都对应一个特定的地雷(有矿物的那些,而不是炸弹)。

有些地雷已经解锁,但其他一些地雷需要先购买,所以我添加了button来做这件事。

问题

事实是,使用碎片,一切都运行良好,但现在我用Views换了它们,我可以买矿,但是如果我买另一个,我之前购买的那个,被锁定,因为我从来没有买它。

CODE

我现在真的很困惑,我不知道问题出在哪里,但我肯定会给你最相关的代码部分:

MinerAdapter

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 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;
    }

    [Some setup skipped]

    // 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;
    }
}

XML布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/mineName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="COAL MINE"
        android:textColor="@android:color/white"
        android:textSize="25sp" />


    <TextView
        android:id="@+id/mineCost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:gravity="center"
        android:text="1000"
        android:textColor="@android:color/white"
        android:textSize="50sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:paddingEnd="100dp"
        android:paddingStart="100dp">

        <TextView
            android:id="@+id/mineMineral"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignTop="@+id/mineDropRate"
            android:text="COAL"
            android:textAlignment="center"
            android:textColor="@android:color/white"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/mineDropRate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:text="1"
            android:textAlignment="center"
            android:textColor="@android:color/white"
            android:textSize="25sp" />

    </RelativeLayout>
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/unlockButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Unlock" />

</RelativeLayout>

MainActivity

public class MainActivity extends AppCompatActivity {

ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //  Add Test User from Activity
    CenterRepository.getSingletonInstance().setCurrentUser(new User("FET", 2000, 20, 10));

    //Add Test Mines
    CenterRepository.getSingletonInstance().getListOfLavels().clear();
    CenterRepository.getSingletonInstance().addMine(new Mine("Iron", new Mineral("Iron Mineral", 1), 100, 2));
    CenterRepository.getSingletonInstance().addMine(new Mine("Coal", new Mineral("Coal Mineral", 3), 200, 2));
    CenterRepository.getSingletonInstance().addMine(new Mine("Gold", new Mineral("Gold Mineral", 2), 300, 2));

    viewPager = (ViewPager) findViewById(R.id.vpPager);
    viewPager.setAdapter(new MineAdapter(this));
    }
}

附加

如果您更喜欢使用GitHub,here's the project使用完整代码而不跳过代码。

0 个答案:

没有答案