android - 如何在 Fragment 的 Recyclerview 中放置 EditText 和发送按钮?
问题描述
我有一个包含三个片段的活动。有没有办法在其中一个片段上添加聊天功能。详情如下所述。让我知道是否需要其他任何东西。
下面列出了活动的 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>
下面列出了 Fragment 用于膨胀 RecyclerView 的 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>
我的两个片段工作正常。对于第三个片段,我附加了用于列出聊天消息的适配器,它工作正常,但是我无法为聊天功能附加 Edittext 和发送按钮,我认为它应该在适配器之外但在片段内部。有人可以建议如何实现这一目标吗?我花了几个小时来弄清楚如何实现这一目标?在此先感谢您的帮助。
下面是第三个片段的 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)));
}
如果您是新手,请遵循此
推荐阅读
- firebase - 持久化用户身份验证 Flutter Firebase
- python - dask、joblib、ipyparallel 和其他用于尴尬并行问题的调度程序
- awk - 如何获得文件名的 md5sum 值
- c++ - Qt 5.12.0 MinGW 7.3 w64 从 windows 64 交叉编译到 windows x86
- java - 将嵌套的 json 映射到具有原始 json 值的 pojo
- node.js - 使用 passport-facebook-token 进行用户注册羽毛身份验证
- c# - 在 ASP 中调用数据库上下文时出错。NET 应用程序 [VISUAL STUDIO 2012]
- sql-server - 如何在 Azure 数据工厂中使用预复制脚本来删除空/特殊字符行?
- python - 如何格式化 groupby year 函数 pandas - 返回 unicode 属性错误
- ios - 按照用户方向旋转collectionView