gpt4 book ai didi

java - libGDX : Scrollpane is child of Scrollpane and switch between them in scrolling

转载 作者:行者123 更新时间:2023-11-29 18:30:05 36 4
gpt4 key购买 nike

在许多 android 应用程序中,我遇到了“列表的粘性标题”标题,我们喜欢它。 https://github.com/search?q=header+sticky+android ,现在,我正在尝试使用 Scrollpane 和另一个 scene2dui Actor 在 libGDX 框架中完成它。

enter image description here

我达到了大约 60% 或更多,因为我认为这很容易做到,但我缺少一些东西来完成它,所以,我需要一些帮助!

完整示例scrollpane1(父级)的 amountY 到达特定高度时,请停止为您的 child 滚动:

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;

public class MyGdxGame extends ApplicationAdapter {
private Stage stage;

private ScrollPane scrollPane1;
private ScrollPane scrollPane2;

@Override
public void create() {
stage = new Stage();

Table baseContainer = new Table();

Table container = new Table();
scrollPane1 = new ScrollPane(container);

Table list = new Table();
scrollPane2 = new ScrollPane(list);

Table mainHeader = new Table();
mainHeader.background(new TextureRegionDrawable(this.getPixel()).tint(Color.GRAY));

Table stickyHeader = new Table();
container.add(mainHeader).height(100).growX();
container.row();
container.add(stickyHeader).height(200).growX();
container.row();
container.add(scrollPane2).grow();

baseContainer.setFillParent(true);
baseContainer.add(scrollPane1).grow();

stage.addActor(baseContainer);

Gdx.input.setInputProcessor(stage);

this.fillDummyDataInHeader(stickyHeader);
this.fillDummyDataInList(list);
}

@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height);
}

@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

stage.act();
stage.draw();

// to fix sticky header
if (scrollPane1.getScrollY() >= 100) {
// stop scrolling in scrollpane 1
scrollPane1.setScrollingDisabled(true, true);
scrollPane1.cancel();

// run scrollpane 2
scrollPane2.setScrollingDisabled(true, false);

}
}

@Override
public void dispose() {
stage.dispose();
}

private void fillDummyDataInHeader(Table stickyHeader) {
stickyHeader.background(new TextureRegionDrawable(this.getPixel()).tint(new Color(0.1960F, 0.3921F, 0.7843F, 0.8F)));
Image ic = new Image(new Texture("badlogic.jpg"));
Label label = new Label("Hi, I am sticky header!", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
label.setAlignment(Align.center);
stickyHeader.add(ic).pad(10).center().size(50);
stickyHeader.row();
stickyHeader.add(label).pad(10).center().growX();
}

private void fillDummyDataInList(Table list) {
for (int i = 0; i < 10; i++) {
Table row = new Table().background(new TextureRegionDrawable(this.getPixel()).tint(new Color(1F, 0.63921F, 0, 0.6F)));
Image ic = new Image(new Texture("badlogic.jpg"));
Label label = new Label("Row Item [" + (i + 1) + "]", new Label.LabelStyle(new BitmapFont(), Color.DARK_GRAY));
row.add(ic).pad(20).size(40);
row.add(label).height(100).growX();

list.add(row).pad(20).padTop(0).growX();
list.row();
}
}

private Texture getPixel() {
Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(1F, 1F, 1F, 1F);
pixmap.fill();

Texture texture = new Texture(pixmap);
pixmap.dispose();
return texture;
}

}

最佳答案

使用 Stack 是@second 提及的解决方案,如下代码:

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Cell;
import com.badlogic.gdx.scenes.scene2d.ui.Container;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Stack;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;

public class MyGdxGame extends ApplicationAdapter {
private Stage stage;

private ScrollPane scrollPane1;
private Stack stack;
private Table stickyHeader;
private Container<Table> tableContainer;
private Cell<Container<Table>> oldCell;

@Override
public void create() {
stage = new Stage();

Table baseContainer = new Table();

Table container = new Table();
scrollPane1 = new ScrollPane(container) {
@Override
protected void scrollY(float pixelsY) {
super.scrollY(pixelsY);

if (pixelsY >= 100F) {
stack.add(tableContainer);
} else {
oldCell.setActor(tableContainer);
}
}
};

Table list = new Table();

Table mainHeader = new Table();
mainHeader.background(new TextureRegionDrawable(this.getPixel()).tint(Color.GRAY));

stickyHeader = new Table();
tableContainer = new Container<Table>(stickyHeader);
container.add(mainHeader).height(100).growX();
container.row();
oldCell = container.add(tableContainer.fill().top().height(100)).height(100).growX();
container.row();
container.add(list).grow();

baseContainer.setFillParent(true);
stack = new Stack();
stack.setFillParent(true);
stack.add(scrollPane1);

stage.addActor(stack);

Gdx.input.setInputProcessor(stage);

this.fillDummyDataInHeader(stickyHeader);
this.fillDummyDataInList(list);
}

@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height);
}

@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

stage.act();
stage.draw();
}

@Override
public void dispose() {
stage.dispose();
}

private void fillDummyDataInHeader(Table stickyHeader) {
stickyHeader.background(new TextureRegionDrawable(this.getPixel()).tint(new Color(0.1960F, 0.3921F, 0.7843F, 0.8F)));
Image ic = new Image(new Texture("badlogic.jpg"));
Label label = new Label("Hi, I am sticky header!", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
label.setAlignment(Align.left);
stickyHeader.add(ic).pad(10).left().size(50);
stickyHeader.add(label).pad(10).left().growX();
}

private void fillDummyDataInList(Table list) {
for (int i = 0; i < 10; i++) {
Table row = new Table().background(new TextureRegionDrawable(this.getPixel()).tint(new Color(1F, 0.63921F, 0, 0.6F)));
Image ic = new Image(new Texture("badlogic.jpg"));
Label label = new Label("Row Item [" + (i + 1) + "]", new Label.LabelStyle(new BitmapFont(), Color.DARK_GRAY));
row.add(ic).pad(20).size(40);
row.add(label).height(100).growX();

list.add(row).pad(20).padTop(0).growX();
list.row();
}
}

private Texture getPixel() {
Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(1F, 1F, 1F, 1F);
pixmap.fill();

Texture texture = new Texture(pixmap);
pixmap.dispose();
return texture;
}

}

sticky header in libGDX list

关于java - libGDX : Scrollpane is child of Scrollpane and switch between them in scrolling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56763374/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com