所以我一直在开发Android应用程序,我想设计它的界面几乎就像Google智能助理的用户界面。
合乎逻辑的是使用BottomSheetDialogFragment,但是,在展开底部工作表时,我无法将聊天界面(编辑文本和发送按钮)固定在BottomSheet的底部。
是否有任何地方我可以获得有关我应该做些什么来解决这个问题的教程,或者我可以在哪里获得Google智能助理用户界面的克隆?
凭借我现在所拥有的,唯一的问题是RecyclerView似乎没有响应滚动
这是xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<RelativeLayout
android:id="@+id/rl_assistant_chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/ib_assistant_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="5dp"
android:src="@android:drawable/ic_menu_send" />
<EditText
android:id="@+id/et_assistant_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/ib_assistant_send"
android:maxLines="1"
android:padding="3dp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_assistant_suggestions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="-300dp"
android:layout_centerHorizontal="true"
android:layout_above="@id/rl_assistant_chat"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_assistant_chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/rv_assistant_suggestions"/>
</RelativeLayout>
和Java文件:
public void showAssistant() {
View v = getLayoutInflater().inflate(R.layout.layout_assistant, null);
final BottomSheetDialog dialog = new BottomSheetDialog(mContext);
final boolean[] flag = {true};
dialog.setContentView(v);
dialog.show();
final RecyclerView rv_suggestions = dialog.findViewById(R.id.rv_assistant_suggestions),
rv_chat = dialog.findViewById(R.id.rv_assistant_chat);
final EditText et_message = dialog.findViewById(R.id.et_assistant_message);
final ImageButton ib_send = dialog.findViewById(R.id.ib_assistant_send);
final RelativeLayout rl_chat = dialog.findViewById(R.id.rl_assistant_chat);
assert rv_chat != null && ib_send != null && rv_suggestions != null && et_message != null && rl_chat != null;
final ArrayList<String> suggestions = new ArrayList<>();
final ArrayList<Message> messages = new ArrayList<>();
messages.add(new Message("Hey, what can I do for you?", true));
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rl_chat.getLayoutParams();
suggestions.add("Schedule a meeting with the team");
suggestions.add("Add a reminder");
et_message.setEnabled(false);
ib_send.setEnabled(false);
final RecyclerView.LayoutManager mManager = new LinearLayoutManager(mContext),
sManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
final MessageAdapter mAdapter = new MessageAdapter(messages, mContext);
final BottomSheetBehavior b = BottomSheetBehavior.from((View) v.getParent());
final SuggestionAdapter sAdapter = new SuggestionAdapter(suggestions, new SuggestionAdapter.SuggestionClicked() {
@Override
public void onClick(String suggestion) {
et_message.setText(suggestion);
ib_send.callOnClick();
}
});
ib_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = et_message.getText().toString().trim();
if (!s.equals("")) {
et_message.setText("");
messages.add(new Message(s, false));
mAdapter.notifyItemInserted(messages.size() - 1);
mManager.scrollToPosition(messages.size() - 1);
handleResponse(s, messages, suggestions);
sAdapter.notifyDataSetChanged();
mAdapter.notifyItemInserted(messages.size()-1);
if(flag[0]) {
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl_chat.setLayoutParams(params);
b.setState(BottomSheetBehavior.STATE_EXPANDED);
b.setSkipCollapsed(true);
flag[0] = false;
}
}
}
});
rv_chat.setLayoutManager(mManager);
rv_suggestions.setLayoutManager(sManager);
rv_chat.setItemAnimator(new DefaultItemAnimator());
rv_suggestions.setItemAnimator(new DefaultItemAnimator());
rv_chat.setAdapter(mAdapter);
rv_suggestions.setAdapter(sAdapter);
rv_chat.setNestedScrollingEnabled(false);
}
public void handleResponse(String s, ArrayList<Message> messages, ArrayList<String> suggestions) {
if(first) {
messages.add(new Message("Wait till task data has been loaded please.", true));
}
else {
s = s.toLowerCase();
if (s.contains("meeting")) {
//schedule meeting
ArrayList<User> p = new ArrayList<>();
for (String key : admins.keySet())
p.add(new User(key, admins.get(key), true));
for (String key : peopleMap.keySet())
if(!admins.containsKey(key))
p.add(new User(key, peopleMap.get(key), true));
messages.add(new Message(taskKey, p, Message.Companion.getTYPE_INVITEES()));
suggestions.clear();
suggestions.add("Select time");
suggestions.add("Select time range");
suggestions.add("Cancel");
} else {
//could not understand
messages.add(new Message("I'm sorry, I didn't understand you, could you try again?", true));
}
}
}
消息是我定义的自定义类,用于确定RecyclerView中消息的类型
这是输出视频的链接
https://drive.google.com/file/d/0BymZtxAcj3obN3BoSGRPRS1faG9Za0YzbHdRNGdyTTlVdDE0/view?usp=sharing