How to scroll a list as a list using an element

I set the image as the background of the list, if I want to scroll it with an element, what can I do?

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

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

to

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

maybe I could extend listview and override dispatchDraw, but if I use listFragment, what can I do? Does anyone help me?

+4
source share
2 answers

In your Activity XML file, define a listview as follows:

(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"/> 

Create one class called 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 solves your problem :)

+6
source

The code from AndroidLearner works well, except for one error, see my comment on AndroidLearner answers. I wrote a version of Kotlin's code that fixes the error, and also works with any background that was defined in xml as follows:

 <ListViewWithScrollingBackground android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/some_background"/> 

Here is the code:

 import android.content.Context import android.graphics.Canvas import android.util.AttributeSet import android.widget.ListView class ListViewWithScrollingBackground(context: Context, attrs: AttributeSet) : ListView(context, attrs) { private val background by lazy { getBackground().toBitmap() } override fun dispatchDraw(canvas: Canvas) { var y = if (childCount > 0) getChildAt(0).top.toFloat() - paddingTop else 0f while (y < height) { var x = 0f while (x < width) { canvas.drawBitmap(background, x, y, null) x += background.width } y += background.height } super.dispatchDraw(canvas) } private fun Drawable.toBitmap(): Bitmap = if (this is BitmapDrawable && bitmap != null) bitmap else { val hasIntrinsicSize = intrinsicWidth <= 0 || intrinsicHeight <= 0 val bitmap = Bitmap.createBitmap(if (hasIntrinsicSize) intrinsicWidth else 1, if (hasIntrinsicSize) intrinsicHeight else 1, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) setBounds(0, 0, canvas.width, canvas.height) draw(canvas) bitmap } } 

To convert Drawable to Bitmap I used this post.

0
source

Source: https://habr.com/ru/post/1447007/


All Articles