如何使用项目滚动列表视图背景 [英] how to scroll listview background with item

查看:25
本文介绍了如何使用项目滚动列表视图背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将图片设置为 Listview 背景,如果我想随项目一起滚动它,我该怎么办?

I set a image as Listview background, if I want to scroll it with the item, what can I do?

例如:1 是背景,如果我向下滚动 Listview,它将从

for example: 1 is the background, if I scroll Listview down, it will change from

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

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

也许我可以扩展listview并覆盖dispatchDraw,但是如果我使用 listFragment,我该怎么办?有人帮我吗?

maybe I could extends listview and override dispatchDraw, but if I use listFragment, what can I do ? anybody help me?

推荐答案

在您的 Activity 的 XML 文件中,像这样定义列表视图 ::

In your Activity's XML file define the listview like this ::

(根据您的要求在此 xml 文件中定义属性)

(Define the property in this xml file as per your requirement)

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

创建一个名为 MyCustomListView 的类 ::

Create one Class named MyCustomListView ::

    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);
    }
 }

希望这能解决您的问题:)

Hope this will solve your problem :)

这篇关于如何使用项目滚动列表视图背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆