ViewPager ใช้ Views แทน Fragments

ฉันมี ViewPager ซึ่งตอนนี้ใช้ Views แทน Fragments เพื่อแสดงแต่ละแท็บ ทุกแท็บจะขยายไฟล์เลย์เอาต์เดียวกัน


ภาพรวม

ใน ViewPager นี้ ฉันควรจะเพิ่ม Mines เป็น Tabs ดังนั้นโดยพื้นฐานแล้วทุกแท็บจะสอดคล้องกับเหมืองเฉพาะ (อันที่มีแร่ธาตุ ไม่ใช่ระเบิด)

ทุ่นระเบิดบางอันถูกปลดล็อคแล้ว แต่บางอันจำเป็นต้องซื้อก่อน ดังนั้นฉันจึงเพิ่ม button เพื่อทำสิ่งนี้

ปัญหา

ความจริงก็คือการใช้ชิ้นส่วน ทุกอย่างทำงานได้ดี แต่ตอนนี้ฉันสลับมันกับ Views ฉันสามารถซื้อของฉันได้ แต่แล้ว ถ้าฉันซื้ออันอื่นอันที่ฉันซื้อก่อนหน้านี้ จะถูกล็อคกลับเพราะฉันไม่เคยซื้อมัน


รหัส

ตอนนี้ฉันสับสนมาก ฉันไม่รู้ว่าปัญหาอยู่ที่ไหน แต่ฉันสามารถให้ส่วนที่เกี่ยวข้องมากที่สุดของโค้ดแก่คุณได้อย่างแน่นอน:

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>


กิจกรรมหลัก

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

พิเศษ

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


person FET    schedule 23.09.2016    source แหล่งที่มา