ViewPager using Views instead of Fragments

二次信任 提交于 2020-01-24 20:17:06

问题


I've a ViewPager which now uses Views instead of Fragments to display each tab. Every single tab, inflates the same layout file.


Overview

In this ViewPager, I'm supposed to add Mines as Tabs, so basically every tab would correspond to a specific mine (the ones with minerals, not the bomb).

Some mines are already unlocked, but some other need to be purchased first, so I added a button to do just this.

Problem

Fact is, using fragments, it all worked fine, but now I swapped them with Views, I can buy the mine, but then, if I buy another one, the one I previously purchased, gets locked back as I never did buy it.


CODE

I'm really confused now, I don't know where the problem is, but I can surely give you the most relevant parts of the 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 Layout

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

Extra

If you prefer working with GitHub, here's the project with the full code with no skipped code.

来源:https://stackoverflow.com/questions/39663147/viewpager-using-views-instead-of-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!