如何在 Fragment 的 Recyclerview 中放置 EditText 和 Send Button?
How to put EditText and Send Button in Fragment's Recyclerview?
我有一个包含三个片段的 activity。有没有办法在其中一个片段上添加聊天功能。详情如下。让我知道是否还有其他需要。
下面列出了 activity 的 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 的 XML 用于填充 RecyclerView 的列表如下:-
<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 第三个片段如下。有没有办法在同一个 Fragment 中显示 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)));
}
试试这个它对我有用也有按钮点击。并在 Fragment 上保留按钮点击代码,而不是在 recyclerview
中
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="@+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText"
app:layout_constraintTop_toTopOf="@+id/editText" />
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintStart_toStartOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
您的代码已更新。像这样设计你走错了路 link 下面你可以学习编写 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)));
}
如果您是新手,请按照此操作
我有一个包含三个片段的 activity。有没有办法在其中一个片段上添加聊天功能。详情如下。让我知道是否还有其他需要。
下面列出了 activity 的 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 的 XML 用于填充 RecyclerView 的列表如下:-
<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 第三个片段如下。有没有办法在同一个 Fragment 中显示 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)));
}
试试这个它对我有用也有按钮点击。并在 Fragment 上保留按钮点击代码,而不是在 recyclerview
中<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="@+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText"
app:layout_constraintTop_toTopOf="@+id/editText" />
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintStart_toStartOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
您的代码已更新。像这样设计你走错了路 link 下面你可以学习编写 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)));
}
如果您是新手,请按照此操作