0
我需要Android TV的自定義鍵盤,就像下圖中的這個一樣。 該佈局有一個Recycler View和Grid Layout Manager來定製行和列。Android Grid佈局管理器列的跨度
但是當我運行應用程序佈局不顯示按鈕的正確寬度最後一排。該 「空間」 按鈕應具有4跨度, 「Shift」 鍵2和 「零」 鍵3.
public class KeyboardButton {
private String character;
private int index;
// button span size
private int keyType;
// Constructor
public KeyboardButton(String character, int index, int keyType) {
this.character = character;
this.index = index;
this.keyType = keyType;
}
// Getters and Setters
public String getCharacter() {
return character;
}
public void setCharacter(String character) {
this.character = character;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getKeyType() {
return keyType;
}
public void setKeyType(int keyType) {
this.keyType = keyType;
}
}
class KeyboardViewAdapter extends RecyclerView.Adapter<KeyboardViewAdapter.ViewHolder> {
// List of keyboard buttons
private ArrayList<KeyboardButton> buttons;
private LinearLayout layout;
// Constructor
public KeyboardViewAdapter(ArrayList<KeyboardButton> buttons) {
this.buttons = buttons;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
// Create new views (invoked by the layout manager)
if (viewType == 1) {
itemView = inflater.inflate(R.layout.layout_view_keyboard_row_span_1, parent, false);
} else if (viewType == 2) {
itemView = inflater.inflate(R.layout.layout_view_keyboard_row_span_2, parent, false);
} else if (viewType == 3){
itemView = inflater.inflate(R.layout.layout_view_keyboard_row_span_3, parent, false);
} else {
itemView = inflater.inflate(R.layout.layout_view_keyboard_row_span_4, parent, false);
}
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final KeyboardButton kb = buttons.get(position);
int itemType = getItemViewType(position);
holder.btnKeyboard.setBackgroundColor(Color.parseColor("#242c33"));
holder.btnKeyboard.setTextColor(Color.parseColor("#ffffff"));
holder.btnKeyboard.setText(kb.getCharacter());
holder.btnKeyboard.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
}
});
holder.btnKeyboard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(BeelineApplication.get(), kb.getCharacter(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return buttons.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private Button btnKeyboard;
public ViewHolder(View itemView) {
super(itemView);
btnKeyboard = (Button) itemView.findViewById(R.id.btn_keyboard);
layout = (LinearLayout) itemView.findViewById(R.id.ll_keyboard_alphabetical);
}
}
@Override
public int getItemViewType(int position) {
KeyboardButton kb = buttons.get(position);
if (kb.getKeyType() == 1) {
return 1;
} else if (kb.getKeyType() == 2) {
return 2;
} else if (kb.getKeyType() == 3){
return 3;
} else {
return 4;
}
}
}
public class KeyboardView {
private Context context;
public KeyboardView(Context context) {
this.context = context;
setupView();
}
private void setupView() {
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
llKeyboardMain = (LinearLayout) inflater.inflate(R.layout.layout_view_keyboard_large, null);
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 14, GridLayoutManager.VERTICAL,false);
rvKeyboardRecycler = (RecyclerView) llKeyboardMain.findViewById(R.id.rv_keyboard_recycler);
rvKeyboardRecycler.setLayoutManager(gridLayoutManager);
RecyclerView.Adapter kbAdapter = new KeyboardViewAdapter(getData());
rvKeyboardRecycler.setAdapter(kbAdapter);
private ArrayList<KeyboardButton> getData() {
ArrayList<KeyboardButton> data = new ArrayList<>();
// First row dummy data
data.add(new KeyboardButton("А", 0, 1));
data.add(new KeyboardButton("Б", 1, 1));
data.add(new KeyboardButton("В", 2, 1));
data.add(new KeyboardButton("Г", 3, 1));
data.add(new KeyboardButton("Д", 4, 1));
data.add(new KeyboardButton("Е", 5, 1));
data.add(new KeyboardButton("Ё", 6, 1));
data.add(new KeyboardButton("Ж", 7, 1));
data.add(new KeyboardButton("З", 8, 1));
data.add(new KeyboardButton("И", 9, 1));
data.add(new KeyboardButton("Й", 10, 1));
data.add(new KeyboardButton("1", 11, 1));
data.add(new KeyboardButton("2", 12, 1));
data.add(new KeyboardButton("3", 13, 1));
// Second row dummy data
data.add(new KeyboardButton("К", 14, 1));
data.add(new KeyboardButton("Л", 15, 1));
data.add(new KeyboardButton("М", 16, 1));
data.add(new KeyboardButton("Н", 17, 1));
data.add(new KeyboardButton("О", 18, 1));
data.add(new KeyboardButton("П", 19, 1));
data.add(new KeyboardButton("Р", 20, 1));
data.add(new KeyboardButton("С", 21, 1));
data.add(new KeyboardButton("Т", 22, 1));
data.add(new KeyboardButton("У", 23, 1));
data.add(new KeyboardButton("Х", 24, 1));
data.add(new KeyboardButton("4", 25, 1));
data.add(new KeyboardButton("5", 26, 1));
data.add(new KeyboardButton("6", 27, 1));
// Third row dummy data
data.add(new KeyboardButton("Ф", 28, 1));
data.add(new KeyboardButton("Ч", 29, 1));
data.add(new KeyboardButton("Ц", 30, 1));
data.add(new KeyboardButton("Ш", 31, 1));
data.add(new KeyboardButton("Щ", 32, 1));
data.add(new KeyboardButton("Ь", 33, 1));
data.add(new KeyboardButton("Ы", 34, 1));
data.add(new KeyboardButton("Ъ", 35, 1));
data.add(new KeyboardButton("Э", 36, 1));
data.add(new KeyboardButton("Ю", 37, 1));
data.add(new KeyboardButton("Я", 38, 1));
data.add(new KeyboardButton("7", 39, 1));
data.add(new KeyboardButton("8", 40, 1));
data.add(new KeyboardButton("9", 41, 1));
// Fourth row dummy data
data.add(new KeyboardButton("?!#", 42, 1));
data.add(new KeyboardButton("ENG", 43, 1));
data.add(new KeyboardButton("@", 44, 1));
data.add(new KeyboardButton("Space", 45, 4));
data.add(new KeyboardButton(".", 49, 1));
data.add(new KeyboardButton("Shift", 50, 2));
data.add(new KeyboardButton("Back", 52, 1));
data.add(new KeyboardButton("0", 53, 3));
return data;
}
public View getView() {
return llKeyboardMain;
}
}
llKeyboardMain是一個Layout文件,其中包含一個LinearLayout和RecyclerView它
這解決了佈局,感謝Veeshal – alezniki