2

我有使用RecyclerView和GridLayoutManager的虛擬鍵盤,每行的默認spanSize爲11列。如何使用GridLayoutManager設置每行不同數量的列

有兩個英語和俄語鍵盤不同的模式,但是當我點擊更改爲英語鍵盤視圖我需要有鍵盤設置爲每行9列,而不是11,因爲它是俄語鍵盤。

如何以編程方式設置不同的列號?

Russian Keyboard with 11 columns per row

English Keyboard that needs to have 9 columns per row instead of 11

KEYBOARD適配器類

class KeyboardViewAdapter extends RecyclerView.Adapter<KeyboardViewAdapter.ViewHolder> { 

// UNDEFINED_ICONS for keyboard buttons without icon image 
private static final int UNDEFINED_ICONS = -1; 

// List of keyboard buttons 
private ArrayList<VirtualKeyboardButton> buttons; 

// Keyboard Button Listener 
private KeyboardButtonListener keyboardButtonListener; 

// Constructor 
public KeyboardViewAdapter(ArrayList<VirtualKeyboardButton> buttons, KeyboardButtonListener listener) { 
    this.buttons = buttons; 
    this.keyboardButtonListener = listener; 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    final View view = null; 

    switch (viewType) { 
     case 1: 
      // Case 1 is for keyboard button span size of 1 
      View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_view_keyboard_row_span_1, parent, false); 
      return new ViewHolder(v); 
     case 2: 
      // Case 2 is for keyboard button span size of 2 
      v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_view_keyboard_row_span_2, parent, false); 
      return new ViewHolder(v); 
     case 3: 
      // Case 3 is for keyboard button span size of 3 
      v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_view_keyboard_row_span_3, parent, false); 
      return new ViewHolder(v); 
     case 4: 
      // Case 4 is for keyboard button span size of 4 
      v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_view_keyboard_row_span_4, parent, false); 
      return new ViewHolder(v); 
    } 

    return new ViewHolder(view); 
} 

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
    // Virtual Keyboard Button virtualKeyboardButton 
    final VirtualKeyboardButton virtualKeyboardButton = buttons.get(position); 

    // Replace the button characters 
    holder.btnKeyboardButton.setTransformationMethod(null); 

    if (virtualKeyboardButton.getIcon() != UNDEFINED) { 
     // IF BUTTON HAS IMAGE ICON 
     holder.btnKeyboardImageView.setImageResource(virtualKeyboardButton.getIcon()); 
     holder.btnKeyboardImageView.setBackgroundResource(R.drawable.keyboard_button_shape); 
     holder.btnKeyboardImageView.setZ(100); 
    } else { 
     holder.btnKeyboardButton.setBackgroundResource(R.drawable.keyboard_button_shape); 
     holder.btnKeyboardButton.setTextColor(Color.parseColor("#ffffff")); 
     holder.btnKeyboardButton.setText(virtualKeyboardButton.getCharacter()); 
     holder.btnKeyboardImageView.setZ(0); 
    } 

    // Keyboard Button Click listener 
    holder.btnKeyboardButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      keyboardButtonListener.onKeyboardButtonClick(virtualKeyboardButton); 
     } 
    }); 

    // Keyboard Button Focus Listener 
    holder.btnKeyboardButton.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (hasFocus) { 
       holder.btnKeyboardButton.setBackgroundResource(R.drawable.keyboard_button_focus_shape); 
       holder.btnKeyboardButton.setTextColor(ContextCompat.getColor(BeelineApplication.get(), R.color.keyboard_button_text_color_black)); 
       holder.btnKeyboardImageView.setImageResource(virtualKeyboardButton.getSelectedIcon()); 

      } else { 
       holder.btnKeyboardButton.setBackgroundResource(R.drawable.keyboard_button_shape); 
       holder.btnKeyboardButton.setTextColor(ContextCompat.getColor(BeelineApplication.get(), R.color.keyboard_button_text_color_white)); 

       holder.btnKeyboardImageView.setImageResource(virtualKeyboardButton.getIcon()); 

      } 
     } 
    }); 

    // Handle button focus 
    if (virtualKeyboardButton.isFocused()) { 
     holder.btnKeyboardButton.requestFocus(); 
    } 
} 

@Override 
public int getItemCount() { 
    return buttons.size(); 
} 

public class ViewHolder extends RecyclerView.ViewHolder { 

    // Button btnKeyboardButton is a keyboard button with character 
    // ImageButton btnKeyboardImageButton is a keyboard button with icon 
    private Button btnKeyboardButton; 
    private ImageView btnKeyboardImageView; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     btnKeyboardButton = (Button) itemView.findViewById(R.id.btn_keyboard); 
     btnKeyboardImageView = (ImageView) itemView.findViewById(R.id.btn_keyboard_img); 
    } 
} 

@Override 
public int getItemViewType(int position) { 
    //Virtual Keyboard Button virtualKeyboardButton 
    VirtualKeyboardButton virtualKeyboardButton = buttons.get(position); 

    return virtualKeyboardButton.getCellSize(); 
} 

// Refresh keyboard buttons in adapter 
public void refreshButtons(ArrayList<VirtualKeyboardButton> buttons) { 
    this.buttons = buttons; 
    notifyDataSetChanged(); 
} 

//Keyboard button Listener 
public interface KeyboardButtonListener { 
    void onKeyboardButtonClick(VirtualKeyboardButton virtualKeyboardButton); 
} 

}

VIRTUAL字母鍵盤CLASS

public class VirtualAlphaKeyboard extends VirtualKeyboardBase { 

private KeyboardViewAdapter adapter; 

// Keyboard language modes 
private final int modeRussian = 0; 
private final int modeEnglish = 1; 

// Constructor 
public VirtualAlphaKeyboard() { 

    // RUSSIAN KEYBOARD BUTTONS - 11 COLUMNS PER ROW 
    final ArrayList<VirtualKeyboardButton> arrListKeyboardRussian = new ArrayList<>(); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_Q, "А")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_W, "Б")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_E, "В")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_R, "Г")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_T, "Д")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_Y, "Е")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_U, "Ё")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_I, "Ж")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_O, "З")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_P, "И")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_UNKNOWN, "Й")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_A, "К")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_S, "Л")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_D, "М")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_F, "Н")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_G, "О")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_H, "П")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_J, "Р")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_K, "С")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_L, "Т")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_UNKNOWN, "У")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_UNKNOWN, "Х")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_A, "Ф")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_S, "Ч")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_D, "Ц")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_F, "Ш")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_G, "Щ")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_H, "Ь")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_J, "Ы")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_K, "Ъ")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_L, "Э")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_UNKNOWN, "Ю")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_UNKNOWN, "Я")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_CTRL_LEFT, "?!#", true)); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_ALT_LEFT, "ENG", true)); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_AT, "@")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_SPACE, " ", 4)); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_NUMPAD_DOT, ".")); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_SHIFT_RIGHT, R.drawable.keyboard_button_shift, R.drawable.keyboard_button_shift_focus, 2, true)); 
    arrListKeyboardRussian.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_DEL, R.drawable.keyboard_button_back, R.drawable.keyboard_button_back_focus, true)); 

    // Store russian keys to hash map 
    registeredButtons.put(modeRussian, arrListKeyboardRussian); 

    // ENGLISH KEYBOARD BUTTONS - 9 COLUMNS PER ROW 
    final ArrayList<VirtualKeyboardButton> arrListKeyboardEnglish = new ArrayList<>(); 

    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_Q, "Q")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_W, "W")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_E, "E")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_R, "R")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_T, "T")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_Y, "Y")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_U, "U")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_I, "I")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_O, "O")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_P, "P")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_A, "A")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_S, "S")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_D, "D")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_F, "F")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_G, "G")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_H, "H")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_J, "J")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_K, "K")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_L, "L")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_Z, "Z")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_X, "X")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_C, "C")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_V, "V")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_B, "B")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_N, "N")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_M, "M")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_CTRL_LEFT, "?!#", true)); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_ALT_LEFT, "RUS", true)); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_AT, "@")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_SPACE, " ", 4)); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_NUMPAD_DOT, ".")); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_SHIFT_RIGHT, R.drawable.keyboard_button_shift, R.drawable.keyboard_button_shift_focus, 2, true)); 
    arrListKeyboardEnglish.add(new VirtualKeyboardButton(KeyEvent.KEYCODE_DEL, R.drawable.keyboard_button_back, R.drawable.keyboard_button_back_focus, true)); 

    // Store english keys to hash map 
    registeredButtons.put(modeEnglish, arrListKeyboardEnglish); 

    setup(); 
} 

@Override 
public void setup() { 
    final LayoutInflater inflater = (LayoutInflater) BeelineApplication.get().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    view = inflater.inflate(R.layout.layout_keyboard_alpha, null); 

    //Grid Layout Manager with 11 columns per row 
    GridLayoutManager gridLayoutManager = new GridLayoutManager(BeelineApplication.get(), 11); 

    //Recycler view adapter 
    adapter = new KeyboardViewAdapter(getKeyboardButtonsList(), new KeyboardViewAdapter.KeyboardButtonListener() { 
     @Override 
     public void onKeyboardButtonClick(VirtualKeyboardButton virtualKeyboardButton) { 


      if (virtualKeyboardButton.isFunctional()) { 

       switch (virtualKeyboardButton.getKeyCode()) { 
        case KeyEvent.KEYCODE_DEL: { 
         virtualKeyboardListener.onKeyPressed(virtualKeyboardButton); 
         break; 
        } 

        // LANGUAGE SUPPORT 
        case KeyEvent.KEYCODE_ALT_LEFT: { 
         switch (currentMode) { 
          case modeRussian: { 
           clearFocus(); 
           setMode(modeEnglish); 
           getKeyboardButton(virtualKeyboardButton.getKeyCode()).setFocused(true); 
           adapter.refreshButtons(getKeyboardButtonsList()); 
           break; 
          } 

          case modeEnglish: { 
           clearFocus(); 
           setMode(modeRussian); 
           getKeyboardButton(virtualKeyboardButton.getKeyCode()).setFocused(true); 
           adapter.refreshButtons(getKeyboardButtonsList()); 
           break; 
          } 
         } 
         break; 
        } 
       } 
      } else { 
       virtualKeyboardListener.onKeyPressed(virtualKeyboardButton); 
      } 
     } 
    }); 

    // Set registered buttons cell size 
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 
     @Override 
     public int getSpanSize(int position) { 
      return adapter.getItemViewType(position); 
     } 
    }); 

    final RecyclerView recycler = (RecyclerView) view.findViewById(R.id.rv_keyboard_recycler_alpha); 
    recycler.setLayoutManager(gridLayoutManager); 
    recycler.setAdapter(adapter); 
    recycler.requestFocus(); 
} 

}

虛擬鍵盤按鈕類

public class VirtualKeyboardButton { 

// Key code (ASCII table) 
private int keyCode; 

// Keyboard Button Text Character 
private String character; 

// Keyboard Button Cell size 
private int cellSize = 1; 

// Keyboard Button Image Icon 
private int icon; 

// Keyboard Button Image icon when focus is on 
private int selectedIcon; 

// Functional key is used to do some internal functionality inside keyboard view, it doesn't need to dispatched as real key event 
private boolean isFunctional; 

// Is Keyboard Button focused 
private boolean isFocused; 

// Constructor for keyboard with key code and character 
public VirtualKeyboardButton(int keyCode, String character) { 
    this(keyCode, character, 1, -1, -1, false); 
} 

// Constructor for keyboard with key code and character and different cell sizes 
public VirtualKeyboardButton(int keyCode, String character, int cellSize) { 
    this(keyCode, character, cellSize, -1, -1, false); 
} 

// Constructor for keyboard with key code and image icon 
public VirtualKeyboardButton(int keyCode, int icon, int selectedIcon) { 
    this(keyCode, "", 1, icon, selectedIcon, false); 
} 

// Constructor for keyboard with image key code, image icon functionality with different cell size 
public VirtualKeyboardButton(int keyCode, int icon, int selectedIcon, int cellSize, boolean isFunctional) { 
    this(keyCode, "", cellSize, icon, selectedIcon, isFunctional); 
} 

//Constructor for keyboard with image key that has functionality 
public VirtualKeyboardButton(int keyCode, int icon, int selectedIcon, boolean isFunctional) { 
    this(keyCode, "", 1, icon, selectedIcon, isFunctional); 
} 

// Constructor for keyboard with functionality 
public VirtualKeyboardButton(int keyCode, String character, boolean isFunctional) { 
    this(keyCode, character, 1, -1, -1, isFunctional); 
} 

// Constructor 
public VirtualKeyboardButton(int keyCode, String character, int cellSize, int icon, int selectedIcon, boolean isFunctional) { 
    this.keyCode = keyCode; 
    this.character = character; 
    this.cellSize = cellSize; 
    this.icon = icon; 
    this.selectedIcon = selectedIcon; 
    this.isFunctional = isFunctional; 
} 

public int getKeyCode() { 
    return keyCode; 
} 

public String getCharacter() { 
    return character; 
} 

public int getCellSize() { 
    return cellSize; 
} 

public int getIcon() { 
    return icon; 
} 

public int getSelectedIcon() { 
    return selectedIcon; 
} 

public boolean isFunctional() { 
    return isFunctional; 
} 

public void setFocused(boolean isFocused) { 
    this.isFocused = isFocused; 
} 

public boolean isFocused() { 
    return isFocused; 
} 

}

虛擬鍵盤基類

public abstract class VirtualKeyboardBase { 

// View container 
protected View view; 

// Hash Map list of registered keyboard buttons 
protected HashMap<Integer, ArrayList<VirtualKeyboardButton>> registeredButtons = new HashMap<>(); 


// Current keyboard mode - Initially set to Russian mode 
protected int currentMode = 0; 

// Constructor 
public VirtualKeyboardBase() {} 

// Setup metod 
public abstract void setup(); 

// Return filled view, ready to add to layout 
public View getView() { 
    return view; 
} 

// Set current mode 
public void setMode(int mode) { 
    this.currentMode = mode; 
} 

// Get keyboard buttons list 
public ArrayList<VirtualKeyboardButton> getKeyboardButtonsList() { 
    return registeredButtons.get(currentMode); 
} 

// Get keyboard button 
public VirtualKeyboardButton getKeyboardButton(int keyCode) { 
    for (VirtualKeyboardButton keyboardButton : getKeyboardButtonsList()) { 
     if (keyboardButton.getKeyCode() == keyCode) { 
      return keyboardButton; 
     } 
    } 

    return null; 
} 

// Clear focus 
public void clearFocus() { 
    for (VirtualKeyboardButton keyboardButton : getKeyboardButtonsList()) { 
     keyboardButton.setFocused(false); 
    } 
} 

// Virtual Keyboard Listener virtualKeyboardListener 
protected VirtualKeyboardListener virtualKeyboardListener; 

// Virtual Keyboard Listener 
public interface VirtualKeyboardListener { 
    void onKeyPressed(VirtualKeyboardButton virtualKeyboardButton); 
} 

// Set keyboard listener 
public void setKeyboardListener(VirtualKeyboardListener listener) { 
    this.virtualKeyboardListener = listener; 
} 

}

+0

謝謝,但一個不上我的代碼工作。在我的情況下,getSpanSize返回按鈕寬度大小(如空間4或換檔2)。但我需要一行中的按鈕數量是9或11取決於鍵盤。 – alezniki

回答

1

無論你更新的鍵盤佈局,這樣寫:

RecyclerView recycler = 
    (RecyclerView) view.findViewById(R.id.rv_keyboard_recycler_alpha); 

recycler.setLayoutManager(new GridLayoutManager([context], [num columns]); 

每當您撥打RecyclerView.setLayoutManager()時,它都會替換舊的佈局管理器。只需設置一個新的佈局管理器,您就可以輕鬆地在11列和9列之間切換。

+0

感謝Ben,解決了問題! – alezniki

相關問題