该应用程序包含两个回收站视图,每个视图都有自己的布局XML文件。当数据提供给与回收器视图关联的适配器时,它会呈现项目,但视图不可滚动,并且视图中的项目都不可单击。
片段类
public class OrderListFragment extends Fragment {
private RecyclerView orderListRecyclerView;
private TextView orderListEmptyTextView;
private RecyclerView.Adapter adapter;
protected OrderList orderList;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
orderList = (OrderList)
getArguments().getSerializable(ORDER_LIST_SERIALIZED);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_order_list, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle
savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initializeViews(view);
initAdapter();
}
private void initializeViews(View view) {
orderListRecyclerView = view.findViewById(R.id.order_list_recycler_view);
orderListRecyclerView.setHasFixedSize(true);
orderListRecyclerView.setLayoutManager(new
LinearLayoutManager(getActivity()));
orderListEmptyTextView = view.findViewById(R.id.order_list_text_view);
if (orderList != null) {
orderListRecyclerView.setVisibility(VISIBLE);
orderListEmptyTextView.setVisibility(GONE);
}
}
private void initAdapter() {
if (orderList != null) {
adapter = new OrderListAdapter(getActivity(),
orderList.getOrderDetails(), ORDER_LIST_FLAG);
orderListRecyclerView.setAdapter(adapter);
}
}
}
它的布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/order_list_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:visibility="gone"/>
<TextView
android:id="@+id/order_list_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:text="@string/no_order" />
</RelativeLayout>
你们可以看看并建议应该采取什么措施让它发挥作用?
修改
OrderListAdapter
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder> {
private Context context;
private List<Order> orderList;
private String orderFlag;
public OrderListAdapter(Context context, List<Order> orderList, String orderFlag) {
this.context = context;
this.orderList = orderList;
this.orderFlag = orderFlag;
}
@NonNull
@Override
public OrderListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.orderlist_row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull OrderListAdapter.ViewHolder holder, int position) {
final Order order = orderList.get(position);
if (order != null) {
if (!TextUtils.isEmpty(order.getName())) {
holder.getOrderName().setText(order.getName());
}
if (!TextUtils.isEmpty(order.getStatus())) {
holder.getOrderStatus().setText(order.getStatus());
}
if (orderFlag.equals(ORDER_LIST_FLAG)) {
holder.getViewOrder().setVisibility(View.VISIBLE);
holder.getViewOrder().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialogFragment().createSingleChoiceDialog(context,
context.getString(R.string.order_detail),
order.getName(),
R.string.yes_button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).show();
}
});
}
}
}
@Override
public int getItemCount() {
return orderList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView orderName;
private TextView orderStatus;
private TextView viewOrder;
public ViewHolder(View itemView) {
super(itemView);
orderName = itemView.findViewById(R.id.tv_order_name);
orderStatus = itemView.findViewById(R.id.tv_order_status);
viewOrder = itemView.findViewById(R.id.tv_view_order);
}
public TextView getOrderName() {
return orderName;
}
public TextView getOrderStatus() {
return orderStatus;
}
public TextView getViewOrder() {
return viewOrder;
}
}
}
Item_row.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:elevation="3dp"
tools:targetApi="lollipop">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_order_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textStyle="bold"
tools:text="Name of order" />
<TextView
android:id="@+id/tv_order_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
tools:text="Order Status" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_margin="5dp">
<TextView
android:id="@+id/tv_view_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textStyle="bold"
android:text="@string/view"
android:visibility="gone"
android:textColor="@color/text_view_button"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>