0

我已经在这个问题上(herehereand here)调查了几个SO答案,并没有提出的解决方案已经奏效。我的问题是我的RecyclerView列表项没有被显示。我在MessengerRecyclerAdapter,onCreateViewHolder,onBindViewHolder和getItemCount中设置了断点,并且只有第一个被调用。而在断点我已经进入了表达式求值和执行RecyclerView没有要求getItemCount

MessengerRecyclerAdapter.getItemCount(); 

,并得到了20所期望的回答本身RecyclerView占用这表现在下面的截图预期的内容区(我把RecyclerView洋红突出它占据的空间)。

RecyclerView location

我RecyclerView XML代码如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/thread_list" 
    android:background="@color/colorAccent" 
    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:name="com.jypsee.jypseeconnect.orgPicker.MessengerListFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layoutManager="LinearLayoutManager" 
    tools:context="com.jypsee.jypseeconnect.orgPicker.MessengerListFragment" 
    tools:listitem="@layout/fragment_messenger_cell"/> 

</LinearLayout> 

我RecyclerView细胞XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

<TextView 
    android:id="@+id/id" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/text_margin" 
    android:textAppearance="?attr/textAppearanceListItem" 
    android:textColor="@color/blueText"/> 

<TextView 
    android:id="@+id/content" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/text_margin" 
    android:textAppearance="?attr/textAppearanceListItem" 
    android:textColor="@color/darkText"/> 
</LinearLayout> 

我ListFragment类:

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

    List<DummyContent.DummyItem> items = new ArrayList<>(); 
    for (Integer i = 0; i<20; i++){ 
     DummyContent.DummyItem item = new DummyContent.DummyItem(i.toString(),"Content","Details"); 
     items.add(item); 
    } 

    View view = inflater.inflate(R.layout.fragment_messenger_list, container, false); 
    mRecyclerView = (RecyclerView) view.findViewById(R.id.thread_list); 
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    mRecyclerAdapter = new MessengerThreadRecyclerAdapter(items, mListener); 
    mRecyclerView.setAdapter(mRecyclerAdapter); 
    mRecyclerAdapter.notifyDataSetChanged(); 
    return view; 
} 

我的适配器类:

public class MessengerRecyclerAdapter 
    extends RecyclerView.Adapter<MessengerRecyclerAdapter.MessageThreadHolder>{ 

private final List<DummyItem> mValues; 
private final RecyclerViewClickListener mListener; 

public MessengerRecyclerAdapter(List<DummyItem> items, RecyclerViewClickListener listener) { 
    mValues = items; 
    mListener = listener; 
} 

@Override 
public MessageThreadHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.fragment_messenger_cell, parent, false); 
    return new MessageThreadHolder(view); 
} 

@Override 
public void onBindViewHolder(final MessageThreadHolder holder, final int position) { 
    holder.mItem = mValues.get(position); 
    holder.mIdView.setText(mValues.get(position).id); 
    holder.mContentView.setText(mValues.get(position).content); 
    holder.mView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (mListener != null) { 
       mListener.recyclerViewListClicked(v, position); 
      } 
     } 
    }); 
} 

@Override 
public int getItemCount() { 
    return mValues.size(); 
} 

public class MessageThreadHolder extends RecyclerView.ViewHolder { 
    public final View mView; 
    public final TextView mIdView; 
    public final TextView mContentView; 
    public DummyItem mItem; 

    public MessageThreadHolder(View view) { 
     super(view); 
     mView = view; 
     mIdView = (TextView) view.findViewById(R.id.id); 
     mContentView = (TextView) view.findViewById(R.id.content); 
    } 
} 

}

正如你可以看到我已经设置的LinearLayout方向垂直,并设置布局管理器,这是2个最常见的解决方案。我真的不知道下一步该做什么,所以任何帮助都是值得赞赏的。

+0

编写代码在onViewCreated()方法,而不是onCreateView(),在onCreateView的截图,你应该只返回你的虚拟视图,其余代码必须在onViewCreated中。 –

+0

显示您的'fragment_messenger_cell.xml'并尝试'mRecyclerView.setHasFixedSize(true);' –

回答

1

正如我在前面的答案中所说的那样编辑。问题出在你的xml中。它没有显示的主要原因是因为,您试图使用include标记而不是片段标记添加片段,因此相应的片段类从未在要添加到活动中的片段布局上调用。 以下是您正确添加碎片所需的代码。

<fragment 
     android:id="@+id/message_thread" 
     android:name="com.jypsee.jypseeconnect.orgPicker.MessengerThreadListFragment" 
     layout="@layout/fragment_messengerthread_list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:layout="@layout/fragment_messengerthread_list" /> 

和你的片段布局应该是这样的

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".orgPicker.MessengerThreadListFragment"> 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/thread_list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

这里是工作enter image description here

+1

@JamesJordanTaylor如果可能的话通过电子邮件向我发送您的项目。我的电子邮件地址是[email protected] –

+0

@JamesJordanTaylor查看并回复您 –

+0

@JamesJordanTaylor我已经更新了我的答案并向您发送了电子邮件。请检查 –