Java 回收器视图适配器未被调用

Java 回收器视图适配器未被调用,java,android,android-recyclerview,Java,Android,Android Recyclerview,我正试图创建一个小的对话UI,但我的Recycler视图适配器似乎不起作用,或者它没有被正确调用。我在调用onCreateViewHolder时通过日志记录进行了测试,但它没有记录任何内容 聊天室的布局是我唯一看到的,循环视图完全是emtpy。 我做错了什么 chatbot.java } populateMessages_adapter.java } 聊天室布局 收到的信息 已发送的消息 PS:很抱歉代码太长。回收视图高度为0,请使用以下代码更新代码chat_layout.xml 在片段的onC

我正试图创建一个小的对话UI,但我的Recycler视图适配器似乎不起作用,或者它没有被正确调用。我在调用onCreateViewHolder时通过日志记录进行了测试,但它没有记录任何内容

聊天室的布局是我唯一看到的,循环视图完全是emtpy。 我做错了什么

chatbot.java }

populateMessages_adapter.java }

聊天室布局 收到的信息 已发送的消息 PS:很抱歉代码太长。

回收视图高度为0,请使用以下代码更新代码chat_layout.xml


在片段的onCreateView中,您正在做很多事情。使用该方法只能对布局进行充气。尝试在片段的onResume方法中移动代码。它仍然不起作用:/your recyclerview height为0,这就是为什么recyclerview为空,请更改height wrap\u content或match\u parent。哇,它起作用了。我没有仔细查看布局,因为问题是适配器没有被调用。这起作用了。谢谢你,伙计!
public class chatBot extends Fragment {
View myView;



public static String access_token;

private List<populate_messages> listItems;


private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private SwipeRefreshLayout mSwipeRefreshLayout;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    myView = inflater.inflate(R.layout.chat_layout, container, false);
    getActivity().setTitle("Home Chat");
    SharedPreferences prefData = getActivity().getApplicationContext().getSharedPreferences("app-data", MODE_PRIVATE);
    if(prefData.contains("access_token")){

        access_token=prefData.getString("access_token", null);

    }else{

        access_token="null";
    }
    Log.v("CHATBOT_access", access_token);



    listItems = new ArrayList<>();
    recyclerView = (RecyclerView)  myView.findViewById(R.id.reyclerview_message_list);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));

    prepareData();

    adapter = new populateMessages_adapter(listItems, myView.getContext());
    recyclerView.setAdapter(adapter);
    return myView;
}

private void prepareData(){


    populate_messages populate = new populate_messages("Hello, how are you","15:30","username","sent");
    listItems.add(populate);

    populate = new populate_messages("I am fine","15:30","bot","received");
    listItems.add(populate);


}}
private String messageText;
private String time;
private String sender;
private String type;

public populate_messages(String messageText, String time, String sender, String type){

    this.messageText = messageText;
    this.time = time;
    this.sender=sender;
    this.type = type;

}

public String getMessageText() {
    return messageText;
}

public String getTime() {
    return time;
}

public String getSender() {
    return sender;
}
public String getType() {
    return type;
}
public class populateMessages_adapter extends RecyclerView.Adapter<populateMessages_adapter.ViewHolder> {

private List<populate_messages> listItems;
private Context context;
public String imageType;

private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;

public populateMessages_adapter(List<populate_messages> listItems, Context context) {
    this.listItems = listItems;
    this.context = context;
}



@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    Log.v("are you created ", "yes");

    View view;


    if (viewType == VIEW_TYPE_MESSAGE_SENT) {
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.message_sent, parent, false);
        return new ViewHolder(view);
    } else {

        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.message_received, parent, false);
        return new ViewHolder(view);


}
}

@Override
public int getItemViewType(int position) {
    populate_messages message =  listItems.get(position);

    if (message.getType().equalsIgnoreCase("sent")) {
            return VIEW_TYPE_MESSAGE_SENT;
    } else {
        return VIEW_TYPE_MESSAGE_RECEIVED;
    }
}

@Override
public void onBindViewHolder(populateMessages_adapter.ViewHolder holder, int position) {

    populate_messages listItem = listItems.get(position);

    Log.v("MESSAGES ", listItem.getMessageText());
    Log.v("MESSAGES ",listItem.getTime());
    Log.v("MESSAGES ", listItem.getSender());

    holder.messageText.setText(listItem.getMessageText());
    holder.timeText.setText(listItem.getTime());
    holder.nameText.setText(listItem.getSender());

}


@Override
public int getItemCount() {


    return listItems.size();
}

private boolean isNetworkConnected() {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    return cm.getActiveNetworkInfo() != null;
}

public class ViewHolder extends RecyclerView.ViewHolder{

    TextView messageText, timeText, nameText;
    // ImageView profileImage;


    public ViewHolder(final View itemView) {
        super(itemView);

        messageText = (TextView) itemView.findViewById(R.id.text_message_body);
        timeText = (TextView) itemView.findViewById(R.id.text_message_time);
        nameText = (TextView) itemView.findViewById(R.id.text_message_name);
        // profileImage = (ImageView) itemView.findViewById(R.id.image_message_profile);


    }
}
<?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">

<android.support.v7.widget.RecyclerView
    android:id="@+id/reyclerview_message_list"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent">
</android.support.v7.widget.RecyclerView>


<View
    android:layout_width="0dp"
    android:layout_height="2dp"
    android:background="#dfdfdf"
    android:layout_marginBottom="0dp"
    app:layout_constraintBottom_toTopOf="@+id/layout_chatbox"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

<LinearLayout
    android:id="@+id/layout_chatbox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:minHeight="48dp"
    android:background="#ffffff"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent">

    <EditText
        android:id="@+id/edittext_chatbox"
        android:hint="Enter message"
        android:background="@android:color/transparent"
        android:layout_gravity="center"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:maxLines="6"/>

    <Button
        android:id="@+id/button_chatbox_send"
        android:text="SEND"
        android:textSize="14dp"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:layout_width="64dp"
        android:layout_height="48dp"
        android:gravity="center"
        android:layout_gravity="bottom" />

</LinearLayout>
<?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="wrap_content"
android:paddingTop="8dp">

<ImageView
    android:id="@+id/image_message_profile"
    android:layout_width="32dp"
    android:layout_height="32dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/text_message_name"
    android:text="John Doe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12sp"
    app:layout_constraintLeft_toRightOf="@+id/image_message_profile"
    android:layout_marginLeft="8dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="4dp" />

<TextView
    android:id="@+id/text_message_body"
    android:text="hi man, how are you?"
    android:background="@drawable/chat_bubble"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxWidth="240dp"
    android:padding="8dp"
    android:textColor="#ffffff"
    android:layout_marginTop="4dp"
    app:layout_constraintTop_toBottomOf="@+id/text_message_name"
    app:layout_constraintLeft_toRightOf="@+id/image_message_profile"
    android:layout_marginLeft="8dp" />

<TextView
    android:id="@+id/text_message_time"
    android:text="11:40"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="10sp"
    app:layout_constraintLeft_toRightOf="@+id/text_message_body"
    android:layout_marginLeft="4dp"
    app:layout_constraintBottom_toBottomOf="@+id/text_message_body" />

 </android.support.constraint.ConstraintLayout>
<?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="wrap_content"
android:paddingTop="8dp">

<TextView
    android:id="@+id/text_message_body"
    android:text="hello, hello!"
    android:background="@drawable/chat_bubble"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxWidth="240dp"
    android:padding="8dp"
    android:textColor="#ffffff"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/text_message_time"
    android:text="11:40"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="10sp"
    android:layout_marginRight="4dp"
    app:layout_constraintBottom_toBottomOf="@+id/text_message_body"
    app:layout_constraintRight_toLeftOf="@+id/text_message_body" />

   </android.support.constraint.ConstraintLayout>
<?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">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/reyclerview_message_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <View
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:layout_marginBottom="0dp"
        android:background="#dfdfdf"
        app:layout_constraintBottom_toTopOf="@+id/layout_chatbox"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <LinearLayout
        android:id="@+id/layout_chatbox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:minHeight="48dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <EditText
            android:id="@+id/edittext_chatbox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:hint="Enter message"
            android:maxLines="6" />

        <Button
            android:id="@+id/button_chatbox_send"
            android:layout_width="64dp"
            android:layout_height="48dp"
            android:layout_gravity="bottom"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:gravity="center"
            android:text="SEND"
            android:textSize="14dp" />

    </LinearLayout>
</android.support.constraint.ConstraintLayout>
<?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">


    <View
        android:id="@+id/view"
        android:layout_width="384dp"
        android:layout_height="2dp"
        android:background="#dfdfdf"
        app:layout_constraintBottom_toTopOf="@+id/layout_chatbox"
        tools:layout_editor_absoluteY="517dp" />

    <LinearLayout
        android:id="@+id/layout_chatbox"
        android:layout_width="384dp"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:minHeight="48dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_editor_absoluteY="519dp">

        <EditText
            android:id="@+id/edittext_chatbox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:hint="Enter message"
            android:maxLines="6" />

        <Button
            android:id="@+id/button_chatbox_send"
            android:layout_width="64dp"
            android:layout_height="48dp"
            android:layout_gravity="bottom"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:gravity="center"
            android:text="SEND"
            android:textSize="14dp" />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/reyclerview_message_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>