简体   繁体   中英

Customize Android widget Gallery

In android Gallery I am facing a problem. In the Gallery(Horizontal) I show full screen item. When I horizontally scroll the item slowly when it scrolled to the half of the screen it automatically jump to the next item which looks very odd. I don't want the automatic jump. I think the example make sense to understand the problem. Please help if anyone have idea to solve the problem.

If I understand you correctly, you don't want the widget to automatically move to the next item if it is moved too much. Try a HorizontalScrollView instead.

This happens because you're probably using the onItemSelected() callback to update a view.

Even if you set setCallbackDuringFling(false) , the callback will be called when scrolling slowly.

What I've noticed is that updating the text in a Button in the callback causes the jump you describe. The same happens with TextViews, but ImageViews don't seem to have this problem.

My solution/hack was to extend the problematic view (the Button) and draw the text myself, bypassing whatever is happening in the SetText method that causes the problem. Here is the full code:

public class ValueButton extends Button {
    private String text = "";
    private Paint textPaint;
    private float textX, textY;

    public ValueButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Align.CENTER);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        textPaint.setTextSize(h / 2);
        textX = w / 2;
        textY = getBaseline();
    };

    public void setTextNoJump(String text) {
        this.text = text;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawText(text, textX, textY, textPaint);
    }
}

The problem with this code is that it ignores the text formatting set in the XML files. If anyone has an easy fix for that, I'd be happy to hear it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM