回收站视图未在 Fragment (Android) 上显示项目

Posted

技术标签:

【中文标题】回收站视图未在 Fragment (Android) 上显示项目【英文标题】:Recycler view is not showing items on Fragment (Android) 【发布时间】:2021-03-03 22:46:25 【问题描述】:

我试图让我的 RecyclerView 在从服务器接收到“消息室”项目后显示它。

目前,RecyclerView 位于与 BottomNavigationView 相连的 Fragment 上。

我使用“MessageRoomAdapter”设置来自服务器响应的信息,但不知何故,在成功通信后,我的 RecyclerView 只显示空白(白色)屏幕。

是否与生命周期问题有关?

我用谷歌搜索了几个小时,但找不到答案。

MessageRoomAdapter.java

public class MessageRoomAdapter extends RecyclerView.Adapter<MessageRoomAdapter.CustomViewHolder> 

private ArrayList<MessageRoomItem> message_room_item_list;

private OnItemClickListener mListener = null;

public MessageRoomAdapter(ArrayList<MessageRoomItem> message_room_item_list) 
    this.message_room_item_list = message_room_item_list;


public interface OnItemClickListener 
    void onItemClick(View v, int position);



@NonNull
@Override
public MessageRoomAdapter.CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_room, parent, false);
    CustomViewHolder holder = new CustomViewHolder(view);

    return holder;


@Override
public void onBindViewHolder(@NonNull MessageRoomAdapter.CustomViewHolder holder, int position) 

    holder.tv_item_message_room_idx.setText(Integer.toString(message_room_item_list.get(position).getMessage_room_idx()));
    holder.tv_item_message_user_idx.setText(Integer.toString(message_room_item_list.get(position).getUser_idx()));
    holder.tv_item_message_user_nickname.setText(message_room_item_list.get(position).getUser_nickname());
    holder.tv_item_message_message_idx.setText(Integer.toString(message_room_item_list.get(position).getMessage_idx()));
    holder.tv_item_message_content.setText(message_room_item_list.get(position).getContent());
    holder.tv_item_message_time.setText(message_room_item_list.get(position).getTime());

    holder.itemView.setTag(position);
    


@Override
public int getItemCount() 
    return (null != message_room_item_list ? message_room_item_list.size() : 0);


public class CustomViewHolder extends RecyclerView.ViewHolder 

    protected TextView tv_item_message_user_nickname;
    protected TextView tv_item_message_time;
    protected TextView tv_item_message_content;
    protected TextView tv_item_message_room_idx;
    protected TextView tv_item_message_user_idx;
    protected TextView tv_item_message_message_idx;

    public CustomViewHolder(@NonNull final View itemView) 
        super(itemView);

        this.tv_item_message_user_nickname = itemView.findViewById(R.id.tv_item_message_user_nickname);
        this.tv_item_message_time = itemView.findViewById(R.id.tv_item_message_time);
        this.tv_item_message_content = itemView.findViewById(R.id.tv_item_message_content);
        this.tv_item_message_room_idx = itemView.findViewById(R.id.tv_item_message_room_idx);
        this.tv_item_message_user_idx = itemView.findViewById(R.id.tv_item_message_user_idx);
        this.tv_item_message_message_idx = itemView.findViewById(R.id.tv_item_message_message_idx);
    

MainActivity.java

public class MainActivity extends BaseActivity implements MainActivityView 

private long mBackKeyPressedTime = 0;
private Toast mToast;

BottomNavigationView bottom_navigation_view_main;

FragmentAgora fragmentAgora;
FragmentRestaurant fragmentRestaurant;
FragmentMessage fragmentMessage;
FragmentMyPage fragmentMyPage;


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bottom_navigation_view_main = findViewById(R.id.bottom_navigation_view_main);

    fragmentAgora = new FragmentAgora();
    fragmentRestaurant = new FragmentRestaurant();
    fragmentMessage = new FragmentMessage();
    fragmentMyPage = new FragmentMyPage();


    getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_main, fragmentAgora).commitAllowingStateLoss();

    bottom_navigation_view_main.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() 
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) 
            switch (menuItem.getItemId())  
                case R.id.item_agora_main: 
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_main, fragmentAgora).commitAllowingStateLoss();
                    return true;
                
                case R.id.item_restaurant_main: 
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_main, fragmentRestaurant).commitAllowingStateLoss();
                    return true;
                
                case R.id.item_message_main: 
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_main, fragmentMessage).commitAllowingStateLoss();
                    return true;
                
                case R.id.item_mypage_main: 
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_main, fragmentMyPage).commitAllowingStateLoss();
                    return true;
                

                default:
                    return false;
            
        
    );

FragmentMessage.java

public class FragmentMessage extends Fragment implements FragmentMessageView
ViewGroup viewGroup;

private ArrayList<MessageRoomItem> m_message_room_item_list;
private MessageRoomAdapter messageRoomAdapter;
private RecyclerView rv_message_box;
private LinearLayoutManager linear_layout_manager;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
    viewGroup = (ViewGroup) inflater.inflate(R.layout.frag_message, container, false);

    rv_message_box = viewGroup.findViewById(R.id.rv_message_box_message_list);

    linear_layout_manager = new LinearLayoutManager(viewGroup.getContext());
    rv_message_box.setLayoutManager(linear_layout_manager);

    m_message_room_item_list = new ArrayList<>();
    messageRoomAdapter = new MessageRoomAdapter(m_message_room_item_list);
    rv_message_box.setAdapter(messageRoomAdapter);

    trygetMessageList();

    return viewGroup;


private void trygetMessageList() 

    final FragmentMessageService fragmentMessageService = new FragmentMessageService(this);
    fragmentMessageService.getMessageRoomList();



@Override
public void getMessageRoomListSuccess(MessageRoomResponse messageRoomResponse) 

    Toast.makeText(getContext(), messageRoomResponse.getMessage(), Toast.LENGTH_SHORT).show();
    switch (messageRoomResponse.getCode())
        default:
            System.out.println(messageRoomResponse.getMessage());
            loadMessageRooms(messageRoomResponse);
    


public void loadMessageRooms(MessageRoomResponse messageRoomResponse)
    switch (messageRoomResponse.getCode())
        case 100:
            int num_of_message_rooms = messageRoomResponse.getMessageRoomResults().size();

            for (int i = 0; i < num_of_message_rooms; i++)
                MessageRoomItem messageRoomItem = new MessageRoomItem();

                messageRoomItem.setMessage_room_idx(messageRoomResponse.getMessageRoomResults().get(i).getMessage_room_idx());
                messageRoomItem.setUser_idx(messageRoomResponse.getMessageRoomResults().get(i).getUser_idx());
                messageRoomItem.setUser_nickname(messageRoomResponse.getMessageRoomResults().get(i).getUser_nickname());
                messageRoomItem.setMessage_idx(messageRoomResponse.getMessageRoomResults().get(i).getMessage_idx());
                messageRoomItem.setContent(messageRoomResponse.getMessageRoomResults().get(i).getContent());
                messageRoomItem.setTime(messageRoomResponse.getMessageRoomResults().get(i).getTime());

                m_message_room_item_list.add(messageRoomItem);
            

            break;

        default:
            break;
    

FragmentMessageService.java

public class FragmentMessageService 
private final FragmentMessageView mFragmentMessageView;
private HashMap<String, Object> mParams;

public FragmentMessageService(final FragmentMessageView fragmentMessageView) 
    this.mFragmentMessageView = fragmentMessageView;


public FragmentMessageService(FragmentMessageView mFragmentMessageView, HashMap<String, Object> mParams) 
    this.mFragmentMessageView = mFragmentMessageView;
    this.mParams = mParams;


void getMessageRoomList() 
    final FragmentMessageRetrofitInterface fragmentMessageRetrofitInterface = getRetrofit().create(FragmentMessageRetrofitInterface.class);
    fragmentMessageRetrofitInterface.getMessageRoomList(X_ACCESS_TOKEN).enqueue(new Callback<MessageRoomResponse>() 
        @Override
        public void onResponse(Call<MessageRoomResponse> call, Response<MessageRoomResponse> response) 

            final MessageRoomResponse messageRoomResponse = response.body();
            if (messageRoomResponse == null) 
                mFragmentMessageView.validateFailure(null);
                return;
            
            mFragmentMessageView.getMessageRoomListSuccess(messageRoomResponse);
        

        @Override
        public void onFailure(Call<MessageRoomResponse> call, Throwable t) 

            mFragmentMessageView.validateFailure(null);
        
    );

【问题讨论】:

您的代码从不通知适配器插入项目,您应该避免在片段中保留对列表的引用并将其移动到适配器内,然后创建一个方法将项目添加到列表并 notifyItemRangeInserted。跨度> 【参考方案1】:

您正在创建一个m_message_room_item_list 的空列表并将该列表设置为适配器,然后您实际上是在加载列表,但从不更新适配器。

你应该做的是在适配器内部创建一个方法,例如:

public void setList(ArrayList<MessageRoomItem> message_room_item_list) 
    this.message_room_item_list = message_room_item_list;
    notifyDataSetChanged();

然后在 loadMessageRooms() 中调用它,比如:

public void loadMessageRooms(MessageRoomResponse messageRoomResponse)
    switch (messageRoomResponse.getCode())
        case 100:
            int num_of_message_rooms = messageRoomResponse.getMessageRoomResults().size();

            for (int i = 0; i < num_of_message_rooms; i++)
                MessageRoomItem messageRoomItem = new MessageRoomItem();

                messageRoomItem.setMessage_room_idx(messageRoomResponse.getMessageRoomResults().get(i).getMessage_room_idx());
                messageRoomItem.setUser_idx(messageRoomResponse.getMessageRoomResults().get(i).getUser_idx());
                messageRoomItem.setUser_nickname(messageRoomResponse.getMessageRoomResults().get(i).getUser_nickname());
                messageRoomItem.setMessage_idx(messageRoomResponse.getMessageRoomResults().get(i).getMessage_idx());
                messageRoomItem.setContent(messageRoomResponse.getMessageRoomResults().get(i).getContent());
                messageRoomItem.setTime(messageRoomResponse.getMessageRoomResults().get(i).getTime());

                m_message_room_item_list.add(messageRoomItem);
            

            messageRoomAdapter.setList(m_message_room_item_list);

            break;

        default:
            break;
    

【讨论】:

我真的非常感谢您的友好解释。我今天加入 *** 就是为了问这个问题。我有多傻……没有通知适配器……哈哈。真的再次感谢。

以上是关于回收站视图未在 Fragment (Android) 上显示项目的主要内容,如果未能解决你的问题,请参考以下文章

回收站视图未在 android studio 中显示一张卡片视图

Listview 项目未在 Fragment Android 中显示

回收站视图在 Fragment 中不起作用

Recycler视图在android studio中未显示一张卡片视图

Android 管理多个fragment(处理Activity被回收的情况)

Lateinit 属性未在 Fragment 上初始化