如何正确设置带有片段作为子代的RecyclerView

时间:2018-11-28 19:23:48

标签: java android android-fragments firebase-realtime-database android-recyclerview

我一直试图将Fragments作为我的RecyclerView的孩子,但无济于事。

我想做的是从我的ArrayList加载一些POJO个自定义Firebase Database类,其中每个项目都有要显示为标题和内容的内容。主要内容。

为此,我创建了一个可折叠布局,用户首先会看到页眉布局,然后单击布局中的所需子项,然后显示主要内容。

标题布局是简单的ConstraintLayout,因此将数据绑定到视图没有问题。但是我的内容布局ViewPagerTabLayout结合在一起,可托管三个Fragments,其数据来自从ArrayList提取的Firebase

我的问题在于,只有第一个孩子的FragmentsonClick事件中显示数据,而其余孩子却不显示数据。

这是我的RecyclerView Adapter

public class OrdersAdapter extends RecyclerView.Adapter<OrdersAdapter.viewHolder> {

public static boolean canScroll;
public static String image;
public static String message;
private static boolean isUser;
private static int scrollTo;
private static List<Order> beanOrders;
private FragmentManager fragmentManager;

public OrdersAdapter(List<Order> beanOrders, FragmentManager fragmentManager) {
    OrdersAdapter.beanOrders = beanOrders;
    this.fragmentManager = fragmentManager;
}

@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new viewHolder(new FoldableLayout(parent.getContext()));
}

@Override
public void onBindViewHolder(@NonNull final viewHolder holder, int i) {
    ArrayList<String> location = beanOrders.get(i).getLocation();
    String county = location.get(0);
    String consti = location.get(1);
    String ward = location.get(2);
    String estate = location.get(3);
    String apartm = location.get(4);

    if (beanOrders.get(i).isTaken()) {
        holder.flHolder.setBackground(holder.itemView.getContext().getResources().getDrawable(R.drawable.vector_free));
    } else if (beanOrders.get(i).isDelivered()) {
        holder.flHolder.setBackground(holder.itemView.getContext().getResources().getDrawable(R.drawable.vector_delid));
    }

    holder.tvWorth.setText(String.format("Ksh %s", beanOrders.get(i).getsWorth()));
    holder.tvNumber.setText(beanOrders.get(i).getsPhone());
    holder.tvUserName.setText(beanOrders.get(i).getsName());
    holder.tvCouConWard.setText(String.format("%s , %s , %s", county, consti, ward));
    holder.tvEstApart.setText(String.format("%s , %s", estate, apartm));
}

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

private void setUpData(viewHolder holder, String image, String message) {
    setUpViewPager(holder.vpAdmin, image, message, fragmentManager);
    holder.tbAdmin.setupWithViewPager(holder.vpAdmin);
    holder.vpAdmin.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            isUser = i == 2;
            if (i == 2) {
                holder.fabClose.setImageResource(R.drawable.vector_eraser);
            } else holder.fabClose.setImageResource(R.drawable.vector_fold);
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });
}

private void setUpViewPager(HViewPager viewPager, String image, String message, FragmentManager fragmentManager) {
    FragmentsAdapter fragmentsAdapter = new FragmentsAdapter(fragmentManager);

    Image fragmentImage = Image.newInstance(image);
    Message fragmentMessage = Message.newInstance(message);

    fragmentsAdapter.addFragment(new Image(), "Image");
    fragmentsAdapter.addFragment(new Message(), "Confirmation");
    fragmentsAdapter.addFragment(new Delivery(), "Delivery");

    viewPager.setAdapter(fragmentsAdapter);
    viewPager.setPagingEnabled(false);
}

class viewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    FoldableLayout flAdmin;
    @BindView(R.id.tbAdmin)
    TabLayout tbAdmin;
    @BindView(R.id.vpAdmin)
    HViewPager vpAdmin;
    @BindView(R.id.fabClose)
    FloatingActionButton fabClose;
    @BindView(R.id.tvUserName)
    AppCompatTextView tvUserName;
    @BindView(R.id.tvNumber)
    AppCompatTextView tvNumber;
    @BindView(R.id.tvWorth)
    AppCompatTextView tvWorth;
    @BindView(R.id.tvCouConWard)
    AppCompatTextView tvCouConWard;
    @BindView(R.id.tvEstApart)
    AppCompatTextView tvEstApart;
    @BindView(R.id.tvLoyalty)
    AppCompatTextView tvLoyalty;
    @BindView(R.id.tvPoints)
    AppCompatTextView tvPoints;
    @BindView(R.id.flHolder)
    FrameLayout flHolder;

    viewHolder(FoldableLayout foldableLayout) {
        super(foldableLayout);
        flAdmin = foldableLayout;
        foldableLayout.setupViews(R.layout.layout_c_header, R.layout.layout_c_content, R.dimen.cover_height, itemView.getContext());
        ButterKnife.bind(this, foldableLayout);

        vpAdmin.setPagingEnabled(false);

        canScroll = true;

        fabClose.setOnClickListener(this);

        flAdmin.setOnClickListener((View v) -> {
            image = beanOrders.get(getLayoutPosition()).getsImage();
            message = beanOrders.get(getLayoutPosition()).getsMessage();

            setUpData(this, image, message);
            tbAdmin.setupWithViewPager(vpAdmin);

            //Toast.makeText(itemView.getContext(), beanOrders.get(getLayoutPosition()).getsMessage(), Toast.LENGTH_SHORT).show();
            new Handler().postDelayed(() -> canScroll = false, 1000);

            Intent intent = new Intent(ConstHelper.START_SCROLL);
            intent.putExtra(ConstHelper.SMOOTH_SCROLL, true);
            intent.putExtra(ConstHelper.SCROLL_TO, getLayoutPosition());
            LocalBroadcastManager.getInstance(itemView.getContext()).sendBroadcast(intent);

            if (flAdmin.isFolded()) {
                flAdmin.unfoldWithAnimation();
            } else {
                flAdmin.foldWithAnimation();
            }
        });

        flAdmin.setOnLongClickListener(view -> {
            canScroll = true;
            return true;
        });

    }

    @Override
    public void onClick(View view) {
        canScroll = true;
        if (isUser) {
            canScroll = false;
            Intent intent = new Intent(ConstHelper.SIGNATURE_CALL);
            intent.putExtra(ConstHelper.CLEAR_CUSTOMER, true);
            LocalBroadcastManager.getInstance(viewHolder.this.itemView.getContext()).sendBroadcast(intent);
        } else {
            if (!flAdmin.isFolded()) {
                flAdmin.foldWithAnimation();
            }
        }
    }

    @Override
    public boolean onLongClick(View view) {
        //TODO send report that order has been received

        return true;
    }
}}

片段很简单,其中两个遵循这种模式:

public class Message extends Fragment {
@BindView(R.id.tvConfirmation)
AppCompatTextView tvConfirmation;
Unbinder unbinder;

public Message() {
}

public static Message newInstance(String message) {
    Bundle bundle = new Bundle();
    bundle.putString("Message", message);

    Message fragmentMessage = new Message();
    fragmentMessage.setArguments(bundle);

    return fragmentMessage;
}

private void readBundle(Bundle bundle) {
    if (bundle != null) {
        //message = bundle.getString("Message");
    }
}

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

    unbinder = ButterKnife.bind(this, view);
    return view;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    tvConfirmation.setText(OrdersAdapter.message);

    Toast.makeText(getActivity(), OrdersAdapter.message, Toast.LENGTH_SHORT).show();
    //readBundle(getArguments());
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
}}

另一个仅加载一张图像,而最后一个未链接到我正在获取的数据。

我尝试过的事情

  1. 使用Bundles将数据传递到Fragment
  2. Fragments类从ViewHolder类加载到onBindViewHolder方法的位置。

第一个似乎可以通过我的调试工作,但是无论如何,问题仍然存在。

这是上述情况的图像: Image here

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

首先从适配器的变量中删除所有static限定词。之后,看到要完成的任务,我强烈建议您从Fragment的复杂性转移到简单的Views和ViewHolders。最后,请确保您notify进行任何更改的适配器。