首页 > 解决方案 > 如何在 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)));
}

标签: androidandroid-xml

解决方案


这是您更新的代码。像这样设计你是通过错误的方式有下面的链接,你可以学习编写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


推荐阅读