1

偽の水平ListViewを作成しようとしています。そのために、アダプター内の各セルの内容をローテーションしたいと思います。それはほとんど機能します:

グラフィックを回す

class ImageAdapter(ctx: Context, viewResourceId: Int, imageResourceId: Int, strings: Array[String], icons: TypedArray, colorSelected: Int, colorNotSelected: Int, colorBorder: Int) extends ArrayAdapter[String](ctx, viewResourceId, strings) {
    // Useful variables
    private val mInflater: LayoutInflater = ctx.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE).asInstanceOf[LayoutInflater]
    private val mStrings: Array[String] = strings
    private val mIcons: TypedArray = icons
    private val mViewResourceId: Int = viewResourceId

    private var mSelected : Int = 0

    def setSelected(s: Int) = mSelected = s

    // Inherited from ArrayAdapter
    override def getCount():Int = mStrings.length
    override def getItem(position: Int): String = mStrings(position)
    override def getItemId(position: Int): Long = position

    // The rotation machinery
    var rotation = 90
    private var matrix = new Matrix()
    val rect1 = new RectF(0, 0, 0, 0)
    val rect2 = new RectF(0, 0, 0, 0)
    val cst = Matrix.ScaleToFit.CENTER

    // Getting the view.
    override def getView(position: Int, convertView: View, parent: ViewGroup): View = {
        var result = if(convertView == null) mInflater.inflate(mViewResourceId, null) else convertView

        var iv: ImageView = result.findViewById(imageResourceId).asInstanceOf[ImageView]
        val drawable = mIcons.getDrawable(position)
        iv.setScaleType(ScaleType.MATRIX)
        matrix.reset()
        val height = drawable.getIntrinsicHeight()
        val width = drawable.getIntrinsicWidth()
        rotation match {
          case 0 =>
            rect1.set(0, 0, width, height)
            rect2.set(0, 0, iv.getWidth(), iv.getHeight())
            matrix.setRectToRect(rect1, rect2, cst)
          case 90 =>
            rect1.set(0, 0, width, height)
            rect2.set(-iv.getHeight()/2, -iv.getWidth()/2, iv.getHeight()/2, iv.getWidth()/2)
            matrix.setRectToRect(rect1, rect2, cst)
            matrix.postRotate(rotation, 0, 0)
            matrix.postTranslate(iv.getWidth()/2, iv.getHeight()/2)
          ...
          case _ =>
        }
        iv.setImageMatrix(matrix)
        iv.setImageDrawable(drawable)
        iv.postInvalidate()

        if(position == mSelected) {
          result.setBackgroundColor(colorSelected)
        } else {
          result.setBackgroundColor(colorNotSelected)
        }
        result.setTag(mStrings(position))
        result
    }
}

唯一の問題は、最初の画像が表示されないことです。代わりに、次のように表示されます。 リストビューが空

最初のリストを表示するには、各リストの1つの要素をクリックする必要があります。onclickメソッドは次のとおりです。

listGraphismesView.setOnItemClickListener(new OnItemClickListener {
    override def onItemClick(parent: AdapterView[_], view: View, position: Int, notUsedId: Long) = {
      var graphismeName = view.getTag().asInstanceOf[String]
      mAdapter.setSelected(position)
      mAdapter.notifyDataSetChanged()
      showGChallenge(position, graphismeName)
    }
  })

次に、フラグメントのonResume関数にmAdapter.notifyDataSetChanged()を入れようとしましたが、リストが最初から更新されません。すべての画像を表示するには、まだ何かをクリックする必要があります。理由は何ですか?

4

1 に答える 1

0

getview が onActivityCreated 関数によって呼び出されるため、iv.getWidth() と iv.getHeight() の両方がゼロを返すことがわかりました。onResume 関数に notifyDataSetChanged() を入れるのも早すぎました。したがって、ドローアブルはサイズ 0 で描画され、ボタンをクリックするとリロードされます。

これを変更するために、リスナーで resize 関数を呼び出す次のコードを書きました。

var iv: ImageView = result.findViewById(imageResourceId).asInstanceOf[ImageView]
val drawable = mIcons.getDrawable(position)
iv.setImageDrawable(drawable)
iv.setScaleType(ScaleType.MATRIX)
val vto = iv.getViewTreeObserver()
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  private var matrix = new Matrix()
  val rect1 = new RectF(0, 0, 0, 0)
  val rect2 = new RectF(0, 0, 0, 0)
  private var height = drawable.getIntrinsicHeight()
  private var width = drawable.getIntrinsicWidth()
  override def onGlobalLayout() {
    val iv_width = iv.getWidth
    val iv_height = iv.getHeight
    matrix.reset()
    rotation match {
      case 0 =>
        rect1.set(0, 0, width, height)
        rect2.set(0, 0, iv_width, iv_height)
        matrix.setRectToRect(rect1, rect2, cst)
      case 90 =>
        rect1.set(0, 0, width, height)
        rect2.set(-iv_height/2, -iv_width/2, iv_height/2, iv_width/2)
        matrix.setRectToRect(rect1, rect2, cst)
        matrix.postRotate(rotation, 0, 0)
        matrix.postTranslate(iv_width/2, iv_height/2)
      ...
      case _ =>
    }
    iv.setImageMatrix(matrix)
  }
})
于 2013-03-25T21:51:48.683 に答える