我正在研究一個應用程序,其中一個自定義視圖由一些後端邏輯更新。 自定義視圖在 onResume() 方法中更新,但不能通過 view.invalidate(); 更新。 我想讓這個視圖更新按鈕的onclick 。
代碼Fragment如下:public class ChartDemo extends Activity {
private IDemoChart mChart;
Button displayGraph, reloadGraph;
LinearLayout linearLayout;
View view;
Random randomData, randomBattery;
int data, battery;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mChart = new SalesStackedBarChart();
reloadGraph = (Button) findViewById(R.id.reloadView);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
view = new View(ChartDemo.this);
loadGraphWithValues();
reloadGraph.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view.invalidate();
loadGraphWithValues();
//view = mChart.execute(ChartDemo.this);
}
});
linearLayout.addView(view);
}
public void loadGraphWithValues() {
data = generateDataValue();
battery = generateBatteryValue();
ListData list = new ListData();
list.setData(data, battery);
SalesStackedBarChart.values.add(list);
view = mChart.execute(ChartDemo.this);
}
public int generateBatteryValue() {
//TODO Auto-generated method stub
randomData = new Random();
return randomData.nextInt(100) + 1;
}
public int generateDataValue() {
//TODO Auto-generated method stub
randomBattery = new Random();
return randomBattery.nextInt(100) + 1;
}
public class ListData {
double data;
double battery;
//getter and setter methods..
}
}
請提前致謝。