Display different types of files in a carousel tabs

时间:2018-12-03 13:14:57

标签: codenameone

I need to implement a carousel to show images/video/pdf/ppt files. I used Tab component to achieve it. But it's not working as expected. The first tab displays image correctly but at the same time pdf file gets opened which is there on the third tab. Second tab video does not play. I tried calling these events on tab selection but still not working. My code as below.

    Form hi = new Form("Swipe Tabs", new LayeredLayout());

    Tabs t = new Tabs();
    t.hideTabs();

    container1 = BoxLayout.encloseY();
    container2 = BoxLayout.encloseY();
    container3 = BoxLayout.encloseY();

    InputStream is = null; 
    ImageViewer iv = null;

    try{
        is = Display.getInstance().getResourceAsStream(getClass(), "/Img1.png");
        iv = new ImageViewer(Image.createImage(is));
    }catch(Exception exc){
        exc.printStackTrace();
    }

    container1 = BoxLayout.encloseY(iv);


    FileSystemStorage fs = FileSystemStorage.getInstance();
    fs.mkdir(fs.getAppHomePath());

    String fileName = fs.getAppHomePath() + "test.mp4";

    if(!fs.exists(fileName)) {
        Util.downloadUrlToFile("http://localhost/app/test.mp4", fileName, true);
    }
    try{
        Media video = MediaManager.createMedia(fileName, true);
        video.setNativePlayerMode(true);
        container2 = BoxLayout.encloseY(new MediaPlayer(video));       
        video.play();
    }catch(Exception exc){
        exc.printStackTrace();
    }


    fs = FileSystemStorage.getInstance();
    fs.mkdir(fs.getAppHomePath());
    final String fileName1 = fs.getAppHomePath() + "file1.pdf";
    if(!fs.exists(fileName1)) {
        Util.downloadUrlToFile("http://localhost/app/file1.pdf", fileName1, true);
    }
    container3 = BoxLayout.encloseY();
    Display.getInstance().execute(fileName1);


    t.addTab("Tab1", container1);
    t.addTab("Tab2", container2);
    t.addTab("Tab3", container3);

    new ButtonGroup(firstTab, secondTab, thirdTab);
    firstTab.setSelected(true);
    Container tabsFlow = FlowLayout.encloseCenter(firstTab, secondTab, thirdTab);

    hi.add(t);
    hi.add(BorderLayout.south(tabsFlow));
    hi.show();

1 个答案:

答案 0 :(得分:1)

Display.execute启动外部查看器以显示内容。它不会创建可以嵌入的组件。我们不支持嵌入PDF,因为它在Android上不可用,而在iOS上却有点不稳定。如果您只关心iOS,则可以使用BrowserComponent来显示PDF。您可以使用按钮并在按下按钮以显示PDF时调用execute

更改此:

    container2 = BoxLayout.encloseY(new MediaPlayer(video));       
    video.play();

收件人:

    MediaPlayer mp = new MediaPlayer(video);
    container2 = mp;       
    mp.setAutoplay(true);
    mp.setLoop(true);

最后,我建议使用Log.e()而不是printStackTrace()