4

リストビューの背景に画像を設定しましたが、アイテムと一緒にスクロールしたい場合はどうすればよいですか?

例: 1 は背景です。Listview を下にスクロールすると、

        1          
-----1-----1--------
   1         1
-1-------------1----

--------1----------
      1    1
---1----------1----
 1              1

おそらくリストビューを拡張してdispatchDrawをオーバーライドできますが、listFragmentを使用すると何ができますか? 誰か助けて?

4

2 に答える 2

6

アクティビティの XML ファイルで、リストビューを次のように定義します::

(要件に応じて、この xml ファイルでプロパティを定義します)

<com.example.MyCustomListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

MyCustomListView という名前のクラスを 1 つ作成します ::

    public class MyCustomListView extends ListView
    {

       private Bitmap background;

    public MyCustomListView(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        background = BitmapFactory.decodeResource(getResources(), R.drawable.yourImageName);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) 
    {
        int count = getChildCount();
        int top = count > 0 ? getChildAt(0).getTop() : 0;
        int backgroundWidth = background.getWidth();
        int backgroundHeight = background.getHeight();
        int width = getWidth();
        int height = getHeight();

        for (int y = top; y < height; y += backgroundHeight)
        {
            for (int x = 0; x < width; x += backgroundWidth)
            {
                canvas.drawBitmap(background, x, y, null);
            }
        }
        super.dispatchDraw(canvas);
    }
 }

これで問題が解決することを願っています:)

于 2012-11-20T12:46:33.150 に答える