您需要使用单例模式将您的数据存储在一个单一的地方。
我从您的代码中得到的是,您的游戏中有多个levels,每个level 都需要最少数量的golds 才能解锁它们。
这是 200 金币的测试数据的样子:-
模型类
让我们从 Level 类开始 -lavel 有名称和最低金阈值
public class LevelModel {
private String levelName;
private int unlockCost;
public String getLevelName() {
return levelName;
}
public int getUnlockCost() {
return unlockCost;
}
public LevelModel(String levelName, int unlockCost) {
this.levelName = levelName;
this.unlockCost = unlockCost;
}
}
接下来是用户类,所有字段都是不言自明的
public class User {
private String userName;
private int gold;
private int experience;
private int experienceLevel = 1;
/**
* @param userName
* @param gold
* @param experience
* @param experienceLevel
*/
public User(String userName, int gold, int experience, int experienceLevel) {
this.userName = userName;
this.gold = gold;
this.experience = experience;
this.experienceLevel = experienceLevel;
}
//Setters
public void setGold(int gold) {
this.gold = gold;
}
public void setExperience(int experience) {
this.experience = experience;
}
public void setExperienceLevel(int experienceLevel) {
this.experienceLevel = experienceLevel;
}
//Getters
public String getUserName() {
return userName;
}
public int getGold() {
return gold;
}
public int getExperience() {
return experience;
}
public int getExperienceLevel() {
return experienceLevel;
}
}
现在您需要一个地方来存储、更新和访问您的游戏数据,这个类将充当 Singleton 类来保存您的所有游戏数据。
public class CenterRepository {
public void setCurrentUser(User currentUser) {
this.currentUser = currentUser;
}
private User currentUser;
ArrayList<LevelModel> listOfLavels = new ArrayList<>();
private static CenterRepository singletonInstance;
private CenterRepository() {
}
public static CenterRepository getSingletonInstance() {
if (null == singletonInstance) {
singletonInstance = new CenterRepository();
}
return singletonInstance;
}
public User getCurrentUser() {
return currentUser;
}
public ArrayList<LevelModel> getListOfLavels() {
return listOfLavels;
}
}
现在第二部分如何访问和更新来自 ViewPager 的数据。我已经增强了您的视图寻呼机以使用视图而不是片段
MineAdapter 已更新**
package com.fet.minebeta.ui;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.fet.minebeta.R;
import com.fet.minebeta.data.CenterRepository;
/**
* Created by FET on 08/09/2016.
* All rights reserved.
* Please contact @fettucciari.leonardo@gmail.com
*/
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) {
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 hide lock image and buy it
CenterRepository.getSingletonInstance().getCurrentUser().setGold(
CenterRepository.getSingletonInstance().getCurrentUser().getGold()
- CenterRepository.getSingletonInstance().getListOfLavels().get(position).getUnlockCost()); // Update user's gold
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);
}
}
这里需要注意的一点是 carousal 页面有 FrameLayout 作为 rootlayout 。如果您打算使用任何其他更新 addview 并相应地删除视图功能
carousal_page.xml **更新您不需要为每种矿物单独布局
<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 {
PagerAdapter adapterViewPager;
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", 200, 20, 10));
//Add Test Mines
CenterRepository.getSingletonInstance().getListOfLavels().add(new Mine("Iron", new Mineral("Iron Mineral", 1), 100, 2));
CenterRepository.getSingletonInstance().getListOfLavels().add(new Mine("Coal", new Mineral("Coal Mineral", 3), 200, 2));
CenterRepository.getSingletonInstance().getListOfLavels().add(new Mine("Gold", new Mineral("Gold Mineral", 2), 300, 2));
viewPager = (ViewPager) findViewById(R.id.vpPager);
viewPager.setAdapter(new MineAdapter(this));
Toast.makeText(getApplicationContext(), "Current Credits " + CenterRepository.getSingletonInstance()
.getCurrentUser().getGold(), Toast.LENGTH_LONG).show();
}
}