android – 如何滚动列表视图背景与项目
作者:互联网
我将图像设置为Listview背景,如果我想用项目滚动它,我该怎么办?
例如:
1是背景,如果我向下滚动Listview,
它会改变
1
-----1-----1--------
1 1
-1-------------1----
至
--------1----------
1 1
---1----------1----
1 1
也许我可以扩展listview并覆盖dispatchDraw,
但如果我使用listFragment,我该怎么办?
有人帮帮我吗?
解决方法:
在Activity的XML文件中定义listview,如:
(根据您的要求在此xml文件中定义属性)
<com.example.MyCustomListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
创建一个名为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);
}
}
希望这会解决你的问题:)
标签:android,android-listview,background,android-listfragment 来源: https://codeday.me/bug/20190529/1180749.html