Java libGDX:Scrollpane是Scrollpane的子级,在滚动中在它们之间切换
在许多android应用程序中,我都会遇到“列表的粘性标题”,我们很喜欢它,现在,我尝试在libGDX框架中使用Java libGDX:Scrollpane是Scrollpane的子级,在滚动中在它们之间切换,java,android,libgdx,scrollview,scrollpane,Java,Android,Libgdx,Scrollview,Scrollpane,在许多android应用程序中,我都会遇到“列表的粘性标题”,我们很喜欢它,现在,我尝试在libGDX框架中使用Scrollpane和另一个scene2duiactors来实现这一点 我实现了60%或更多,因为我认为这很简单,但我缺少一些东西来完成它,所以,我需要一些帮助 完整示例当scrollpane1(家长)的数量达到特定高度时,请停止为您的孩子滚动: package com.mygdx.game; import com.badlogic.gdx.ApplicationAdapter;
Scrollpane
和另一个scene2dui
actors来实现这一点
我实现了60%或更多,因为我认为这很简单,但我缺少一些东西来完成它,所以,我需要一些帮助
完整示例当scrollpane1
(家长)的数量达到特定高度时,请停止为您的孩子滚动:
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;
}
}
package com.mygdx.game;
导入com.badlogic.gdx.ApplicationAdapter;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.graphics.Color;
导入com.badlogic.gdx.graphics.GL20;
导入com.badlogic.gdx.graphics.Pixmap;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.g2d.BitmapFont;
导入com.badlogic.gdx.scenes.scene2d.Stage;
导入com.badlogic.gdx.scenes.scene2d.ui.Image;
导入com.badlogic.gdx.scenes.scene2d.ui.Label;
导入com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
导入com.badlogic.gdx.scenes.scene2d.ui.Table;
导入com.badlogic.gdx.scenes.scene2d.utils.TextureRegionRavable;
导入com.badlogic.gdx.utils.Align;
公共类MyGdxGame扩展应用程序适配器{
私人舞台;
私有滚动窗格滚动窗格1;
私有滚动窗格滚动窗格2;
@凌驾
公共void create(){
阶段=新阶段();
Table baseContainer=新表();
表容器=新表();
scrollPane1=新的滚动窗格(容器);
表列表=新表();
scrollPane2=新的滚动窗格(列表);
Table mainHeader=新表();
背景(新的TextureRegionravable(this.getPixel()).tint(Color.GRAY));
Table stickyHeader=新表格();
container.add(mainsteader).height(100.growX();
container.row();
container.add(stickyHeader).height(200.growX();
container.row();
container.add(scrollPane2.grow();
baseContainer.setFillParent(true);
添加(滚动窗格1.grow();
stage.addActor(baseContainer);
Gdx.input.setInputProcessor(阶段);
this.fillDummyDataInHeader(stickyHeader);
this.fillDummyDataInList(列表);
}
@凌驾
公共空心调整大小(整型宽度、整型高度){
stage.getViewport().update(宽度、高度);
}
@凌驾
公共无效呈现(){
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
舞台表演;
stage.draw();
//修复粘性标题
如果(scrollPane1.getScrollY()>=100){
//在滚动窗格1中停止滚动
scrollPane1.setScrollingDisabled(true,true);
滚动窗格1.cancel();
//运行滚动窗格2
scrollPane2.setScrollingDisabled(真、假);
}
}
@凌驾
公共空间处置(){
stage.dispose();
}
私有void fillDummyDataInHeader(表stickyHeader){
stickyHeader.background(新的TextureRegionRavable(this.getPixel()).tint(新的颜色(0.1960F、0.3921F、0.7843F、0.8F));
图像ic=新图像(新纹理(“badlogic.jpg”);
Label Label=新标签(“嗨,我是粘性标题!”,new Label.LabelStyle(new BitmapFont(),Color.WHITE));
标签.设置对齐(对齐.中心);
粘贴头.添加(ic).pad(10).center().尺寸(50);
stickyHeader.row();
stickyHeader.add(label).pad(10.center().growX();
}
私有void fillDummyDataInList(表列表){
对于(int i=0;i<10;i++){
Table row=new Table().background(新的TextureRegionravable(this.getPixel()).tint(新的颜色(1F,0.63921F,0.6F));
图像ic=新图像(新纹理(“badlogic.jpg”);
Label Label=新标签(“行项目[”+(i+1)+“]”,新标签.LabelStyle(新位图字体(),颜色.深灰色));
行。添加(ic)。衬垫(20)。尺寸(40);
row.add(label).height(100).growX();
list.add(row.pad(20.padTop(0.growX));
list.row();
}
}
私有纹理getPixel(){
Pixmap Pixmap=新的Pixmap(1,1,Pixmap.Format.rgba888);
像素贴图设置颜色(1F、1F、1F、1F);
pixmap.fill();
纹理=新纹理(pixmap);
dispose();
返回纹理;
}
}
使用堆栈
是第二种解决方案,如下代码所示:
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;
}
}
package com.mygdx.game;
导入com.badlogic.gdx.ApplicationAdapter;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.graphics.Color;
导入com.badlogic.gdx.graphics.GL20;
导入com.badlogic.gdx.graphics.Pixmap;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.g2d.BitmapFont;
导入com.badlogic.gdx.scenes.scene2d.Stage;
导入com.badlogic.gdx.scenes.scene2d.ui.Cell;
导入com.badlogic.gdx.scenes.scene2d.ui.Container;
导入com.badlogic.gdx.scenes.scene2d.ui.Image;
导入com.badlogic.gdx.scenes.scene2d.ui.Label;
导入com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
导入com.badlogic.gdx.scenes.scene2d.ui.Stack;
导入com.badlogic.gdx.scenes.scene2d.ui.Table;
导入com.badlogic.gdx.scenes.scene2d.utils.TextureRegionRavable;
导入com.badlogic.gdx.utils.Align;
公共类MyGdxGame扩展应用程序适配器{
私人舞台;
私有滚动窗格滚动窗格1;
私有堆栈;
私有表粘贴头;
私人集装箱;
私人小区;
@凌驾
公共void create(){
阶段=新阶段();
Table baseContainer=新表();
表容器=新表();
scrollPane1=新的滚动窗格(容器){
@凌驾
受保护的空心滚动(浮动像素){
超级卷轴(像素);
如果(像素大于等于100F){
stack.add(tableCont