ViewPager 片段中的 RecyclerView 不显示
RecyclerView inside ViewPager Fragment don't show
所以我有一个片段,它有一个带有 tablayout 的 viewpager,它由两个选项卡 - 两个片段组成。
问题是,recyclerview 显示为空,我不知道为什么。,
选项卡片段布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
标记片段:
List<OrderListItem> orderList = new ArrayList<>();
orderList.add(new OrderListItem(333, "ABCDE", new Date(), new Date(), false, true));
adapter = new OrderListAdapter(orderList, this.getActivity());
layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
myRecyclerView.setLayoutManager(layoutManager);
myRecyclerView.setAdapter(adapter);
适配器:
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder>{
private List<OrderListItem> orderList;
@LayoutRes
private final int layoutRes;
private Context context;
private final LayoutInflater inflater;
public OrderListAdapter(List<OrderListItem> orderList, Context context){
this.orderList = orderList;
this.layoutRes = R.layout.order_list_item_layout;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(layoutRes, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final OrderListItem item = orderList.get(position);
}
public void setItems(List<OrderListItem> orderList){
this.orderList.clear();
this.orderList.addAll(orderList);
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return orderList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
}
RecycleView Item 一些丰富多彩的布局内幕,所以我知道子布局是否存在,但事实并非如此。知道为什么 recyclerview 是空的吗?
edit1:我知道 recyclerview 在那里,因为它在棒棒糖中 phone,如果我在回收站位置移动,它会显示波纹顶部和底部滚动边框。但是子布局是空的,应该是彩色的,正如我在子布局中指定的那样。
eit2。刚刚使用带有 simpleAdapter 的列表视图,它正在显示。房车一定有问题
edit3:行布局(除了没有设置任何值外,我应该清楚地看到一个带有颜色的空文本视图。)
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/black"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/material_deep_teal_200"
android:text="New Text"
android:id="@+id/textView" />
</LinearLayout>
您将 OrderListItem
属性设置为 ui 的逻辑在哪里?
您必须先设置 ViewHolder 组件:
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView mTitleTv;
public ViewHolder(View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.textview_id_in_your_xml_file);
}
}
然后在onBindViewHolder
中设置:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final OrderListItem item = orderList.get(position);
holder.mTitleTv.setText(item.getReplaceThisWithAStringProperty);
}
编辑
如果必须使用默认 LinearLayoutManager
属性,请使用此构造函数:
layoutManager = new LinearLayoutManager(getActivity());
而不是这个:
layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
同时为 RecyclerView 添加固定尺寸 属性:
recyclerView.setHasFixedSize = true;
变化:
android:layout_width="match_parent"
android:layout_height="match_parent"
到
android:layout_width="50dp" // set yourself
android:layout_height="50dp" // set yourself
所以我有一个片段,它有一个带有 tablayout 的 viewpager,它由两个选项卡 - 两个片段组成。
问题是,recyclerview 显示为空,我不知道为什么。,
选项卡片段布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
标记片段:
List<OrderListItem> orderList = new ArrayList<>();
orderList.add(new OrderListItem(333, "ABCDE", new Date(), new Date(), false, true));
adapter = new OrderListAdapter(orderList, this.getActivity());
layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
myRecyclerView.setLayoutManager(layoutManager);
myRecyclerView.setAdapter(adapter);
适配器:
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder>{
private List<OrderListItem> orderList;
@LayoutRes
private final int layoutRes;
private Context context;
private final LayoutInflater inflater;
public OrderListAdapter(List<OrderListItem> orderList, Context context){
this.orderList = orderList;
this.layoutRes = R.layout.order_list_item_layout;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(layoutRes, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final OrderListItem item = orderList.get(position);
}
public void setItems(List<OrderListItem> orderList){
this.orderList.clear();
this.orderList.addAll(orderList);
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return orderList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
}
RecycleView Item 一些丰富多彩的布局内幕,所以我知道子布局是否存在,但事实并非如此。知道为什么 recyclerview 是空的吗?
edit1:我知道 recyclerview 在那里,因为它在棒棒糖中 phone,如果我在回收站位置移动,它会显示波纹顶部和底部滚动边框。但是子布局是空的,应该是彩色的,正如我在子布局中指定的那样。
eit2。刚刚使用带有 simpleAdapter 的列表视图,它正在显示。房车一定有问题
edit3:行布局(除了没有设置任何值外,我应该清楚地看到一个带有颜色的空文本视图。)
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/black"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/material_deep_teal_200"
android:text="New Text"
android:id="@+id/textView" />
</LinearLayout>
您将 OrderListItem
属性设置为 ui 的逻辑在哪里?
您必须先设置 ViewHolder 组件:
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView mTitleTv;
public ViewHolder(View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.textview_id_in_your_xml_file);
}
}
然后在onBindViewHolder
中设置:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final OrderListItem item = orderList.get(position);
holder.mTitleTv.setText(item.getReplaceThisWithAStringProperty);
}
编辑
如果必须使用默认 LinearLayoutManager
属性,请使用此构造函数:
layoutManager = new LinearLayoutManager(getActivity());
而不是这个:
layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
同时为 RecyclerView 添加固定尺寸 属性:
recyclerView.setHasFixedSize = true;
变化:
android:layout_width="match_parent"
android:layout_height="match_parent"
到
android:layout_width="50dp" // set yourself
android:layout_height="50dp" // set yourself