我正在尝试将MPAndroidChart添加到我的项目中,并且在主活动中添加图表的声明和初始化时,一切工作正常。当我开始将UI逻辑提取到另一个类中时,就会出现问题:仅当对视图类的引用是局部变量时,才可以将数据设置到图表中,但是当它成为属性时,则不会设置数据。>
我发现的最奇怪的事情之一是,当View类是一个属性时,如果我在onCreate中创建一个局部变量并将其分配给View类字段,那么数据将被正确设置。
活动的有效实施:
class MarketPriceActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mMarketPriceViewMvc = MarketPriceViewMvcImpl(layoutInflater, null)
setContentView(mMarketPriceViewMvc.rootView)
mMarketPriceViewMvc.setData(getValues())
}
fun getValues() : List<Entry> {
val values = mutableListOf<Entry>()
values.add(Entry(1.0.toFloat(), 11397.80166666666.toFloat()))
values.add(Entry(2.0.toFloat(), 11144.38916666667.toFloat()))
values.add(Entry(3.0.toFloat(), 10450.813333333334.toFloat()))
values.add(Entry(4.0.toFloat(), 9988.947499999998.toFloat()))
values.add(Entry(5.0.toFloat(), 10230.733333333332.toFloat()))
return values
}
}
无法执行活动:
class MarketPriceActivity : AppCompatActivity() {
private val marketPriceViewMvcImpl: MarketPriceViewMvcImpl
get() {
return MarketPriceViewMvcImpl(layoutInflater, null)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(marketPriceViewMvcImpl.rootView)
marketPriceViewMvcImpl.setData(getValues())
}
fun getValues() : List<Entry> {
val values = mutableListOf<Entry>()
values.add(Entry(1.0.toFloat(), 11397.80166666666.toFloat()))
values.add(Entry(2.0.toFloat(), 11144.38916666667.toFloat()))
values.add(Entry(3.0.toFloat(), 10450.813333333334.toFloat()))
values.add(Entry(4.0.toFloat(), 9988.947499999998.toFloat()))
values.add(Entry(5.0.toFloat(), 10230.733333333332.toFloat()))
return values
}
}
MarketPriceViewMvcImpl的实现:
class MarketPriceViewMvcImpl(inflater: LayoutInflater, parent: ViewGroup?) : BaseViewMvc() {
private val mTxtCurrentPrice: TextView
private val mTxtCurrentDate: TextView
private val mChart: LineChart
init {
rootView = inflater.inflate(R.layout.activity_market_price, parent, false)
mTxtCurrentPrice = findViewById(R.id.txt_current_price)
mTxtCurrentDate = findViewById(R.id.txt_current_date)
mChart = findViewById(R.id.chart)
styleChart()
}
fun setData(values: List<Entry>) {
var dataSet: LineDataSet
if(mChart.data != null && mChart.data.dataSetCount > 0) {
dataSet = mChart.data.getDataSetByIndex(0) as LineDataSet
dataSet.values = values
mChart.data.notifyDataChanged()
mChart.notifyDataSetChanged()
}
else {
dataSet = LineDataSet(values, "Bitcoin")
styleDataSet(dataSet)
mChart.data = LineData(dataSet)
}
}
/**
* This method is in charge of styling the general aspects of the chart
*/
private fun styleChart() {
// General Chart Style
mChart.setTouchEnabled(true)
mChart.setPinchZoom(true)
mChart.setBackgroundColor(rootView!!.resources.getColor(R.color.backgroundColor))
mChart.legend.isEnabled = false
mChart.description.isEnabled = false
// Axis
mChart.axisRight.isEnabled = false
val xAxis = mChart.xAxis
xAxis.textColor = Color.WHITE
xAxis.position = XAxis.XAxisPosition.BOTTOM
val yAxis = mChart.axisLeft
yAxis.textColor = Color.WHITE
}
/**
* This method is in charge of styling the general aspects of a given DataSet
*/
private fun styleDataSet(dataSet: LineDataSet) {
dataSet.setDrawIcons(false)
// Lines Style
dataSet.color = rootView!!.resources.getColor(R.color.greenPrimary)
dataSet.setCircleColor(rootView!!.resources.getColor(R.color.greenDark))
dataSet.circleRadius = 5.0.toFloat()
dataSet.setDrawCircleHole(false)
dataSet.setDrawValues(false) // Do not draw values
}
}
BaseViewMvc的实现
abstract class BaseViewMvc {
var rootView: View? = null
protected set
protected val context: Context
get() = rootView!!.context
protected fun <T : View> findViewById(id: Int): T {
return rootView!!.findViewById(id)
}
}
答案 0 :(得分:0)
已经解决!问题是我定义ViewMvc的方式。我更改了它,现在可以正常使用了:
发件人:
status
到
private val marketPriceViewMvcImpl: MarketPriceViewMvcImpl
get() {
return MarketPriceViewMvcImpl(layoutInflater, null)
}