我有以下片段类:
public class fragment1 extends Fragment {
private TextView bunz_count;
private TextView money_count;
private Bunz bunz;
private Handler handler;
int delay = 1000;
View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
bunz = Bunz.getInstance();
handler = new Handler();
view = inflater.inflate(R.layout.fragment1, container, false);
handler.postDelayed(new Runnable(){
public void run(){
update(view);
handler.postDelayed(this, delay);
}
}, delay);
return view;
}
public void update(View view){
bunz_count = (TextView) view.findViewById(R.id.final_bunz_count);
money_count = (TextView) view.findViewById(R.id.final_money_count);
//System.out.println(bunz.getBaker1());
BigDecimal number = ((BigDecimal.valueOf
(bunz.getBaker1()).multiply(BigDecimal.valueOf(.1))));
// ).add((BigDecimal.valueOf(bunz.getBaker2()).multiply(BigDecimal.valueOf(.2)))).
// add((BigDecimal.valueOf
// (bunz.getBaker3()).multiply(BigDecimal.valueOf(.4)))).
// add((BigDecimal.valueOf
// (bunz.getBaker4()).multiply(BigDecimal.valueOf(.8)))).
// add((BigDecimal.valueOf(bunz.getBaker5()).multiply(BigDecimal.valueOf(1)))).
// add((BigDecimal.valueOf(bunz.getBaker6()).multiply(BigDecimal.valueOf(2)))).
// add((BigDecimal.valueOf(bunz.getBaker7()).multiply(BigDecimal.valueOf(4)))).
// add((BigDecimal.valueOf(bunz.getBaker8()).multiply(BigDecimal.valueOf(5)))).
//add((BigDecimal.valueOf(bunz.getBaker9()).multiply(BigDecimal.valueOf(10))));
//System.out.println(number);
bunz.setBunz(bunz.getBunz().add((number)));
bunz_count.setText("Bunz: " + bunz.getBunz());
money_count.setText("Money: " + bunz.getMoney());
System.out.println("bunz" + bunz.getBunz());
}
}
如何将玩家货币的UI显示更新放在单独的线程上运行?由于后台有许多不同的进程在运行,这个线程会出现延迟和问题。为了避免这种情况,我该如何将其放在单独的线程上运行?
编辑:我尝试过类似以下的方式:
mHandlerThread = new HandlerThread("yeye");
mHandlerThread.start();
handler = new Handler(mHandlerThread.getLooper());
handler.postDelayed(new Runnable(){
public void run(){
update(view);
handler.postDelayed(this, delay);
}
}, delay);
但是它并没有起到帮助的作用!
谢谢!