如何使用VerticalViewPager

时间:2019-02-21 11:46:32

标签: android xml android-viewpager

我想在我的应用程序上使用垂直幻灯片,所以我实现了这个库: https://github.com/castorflex/VerticalViewPager 通过添加implementation com.github.castorflex.verticalviewpager:library:19.0.1 在build.gradle的依赖项中。

现在,我想将此viewPager添加到我的xml文件中(如果它是本机水平View分页器,我将在布局中进行类似<android.support.v4.ViewPager>的操作) 但是我尝试的所有操作都失败了,它的语法是什么? (以前从未与图书馆合作过) 我也尝试过<github.castorflex.verticalviewpager> 谢谢

1 个答案:

答案 0 :(得分:0)

您必须输入xml

package hr.tvz.programiranje.java.windows;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Font;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.awt.Color;
import javax.swing.border.MatteBorder;
import javax.swing.table.DefaultTableModel;

public class LeaderboardTable extends JFrame {
    private JFrame frmLeaderboards;
    private JTable leaderboard_table;

    public void show_table_screen() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frmLeaderboards.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public LeaderboardTable() {
        initialize();
    }

    @SuppressWarnings("serial")
    private void initialize() {
        frmLeaderboards = new JFrame();
        frmLeaderboards.setTitle("Leaderboards");
        frmLeaderboards.setBounds(100, 100, 485, 218);
        frmLeaderboards.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        HashMap<String, Float> leaderboard_map = MainWindow.get_leaderboard_map();
        int map_size = leaderboard_map.size();
        Object[][] data = new Object[map_size][3];

        // Sort the map
        LinkedHashMap<String, Float> sorted = new LinkedHashMap<>();
        leaderboard_map.entrySet()
        .stream()
        .sorted(Map.Entry.comparingByValue())
        .forEachOrdered(x -> sorted.put(x.getKey(), x.getValue()));

        // Write the map data into a matrix
        int i = 0;
        for(String key:sorted.keySet()) {
            data[i][0] = i + 1;
            data[i][1] = key;
            data[i++][2] = sorted.get(key);
        }

        String[] column_names = {
            "Rank",
            "Name",
            "Time"
        };

        leaderboard_table = new JTable(new DefaultTableModel(data, column_names) {
            boolean[] columnEditables = new boolean[] { false, false, false };
            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });

        // ScrollPane
        JScrollPane scrollPane = new JScrollPane(leaderboard_table);
        scrollPane.setBounds(36, 37, 407, 79);
        getContentPane().add(scrollPane);

        leaderboard_table.setFont(new Font("Consolas", Font.PLAIN, 12));
        GroupLayout groupLayout = new GroupLayout(frmLeaderboards.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addComponent(leaderboard_table, GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addComponent(leaderboard_table, GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
        );
        frmLeaderboards.getContentPane().setLayout(groupLayout);
    }
}