我有一个包含三个片段的活动。有没有一种方法可以在片段之一上添加聊天功能。详细内容如下。让我知道是否还有其他要求。
活动的XML文件在下面列出。
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
.........
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
......
</android.support.design.widget.CoordinatorLayout>
下面列出了用于扩展RecyclerView的Fragment XML:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/messageList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_below ="@id/appbar"
>
</android.support.v7.widget.RecyclerView>
我的两个片段工作正常。对于第三个Fragment,我连接了适配器以列出聊天消息,但工作正常,但是我无法附加Edittext并发送用于聊天功能的按钮,我认为这应该是在适配器之外,但在Fragment内部。有人可以建议如何实现吗?我已经花了几个小时才知道如何实现这一目标?在此先感谢您的帮助。
下面是第三个Fragment的XML。有没有一种方法可以在同一片段中显示EditText,Send Button和Chatlist?聊天列表不是问题。真正的问题是设置EditText和Send Button。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/newMessageContainer"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/newMessage"
android:focusedByDefault="false"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/send"
android:text="Send"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/messageList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_below ="@id/appbar"
android:layout_above="@id/newMessageContainer"
/>
</RelativeLayout>
编辑
请参阅我已实现的Fragment.java文件。我可能由于ConstraintLayout而收到错误消息
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
RecyclerView rv = (RecyclerView) inflater.inflate(
R.layout.fragment_cheese_list, container, false);
setupRecyclerView(rv);
return rv;
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new
LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new
SimpleStringRecyclerViewAdapter(getActivity(),
getRandomSublist(Cheeses.sCheeseStrings, 30)));
}
最佳答案
这是您的代码更新。像这样的设计,您的传递方式有误,下面提供了链接,您可以在其中学习编写android应用程序的代码,并且由开发人员维护。
这样设置片段并通过我通过的布局
private RecyclerView recyclerView;
private MyAdapter myAdapter;
@Override
public View onCreate(Bundle savedInstanceState){
myAdapter=new MyAdpter();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sample, parentViewGroup, false);
recyclerView= rootView.findViewById(R.id.recyelerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(c);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(myAdapter);
return rootView;
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new
LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new
SimpleStringRecyclerViewAdapter(getActivity(),
getRandomSublist(Cheeses.sCheeseStrings, 30)));
}
如果您是新手,请遵循此步骤
https://guides.codepath.com/android/Using-the-RecyclerView
关于android - 如何将EditText和Send Button放入Fragment的Recyclerview中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53868984/