如何从片段到底页检索数据并在活动中显示

时间:2019-07-29 06:32:08

标签: java android android-recyclerview bottom-sheet

嘿,我正在使用底部带有View-Pager和两个片段的Sheet过滤项目中的数据,我可以将数据从活动传递到View-Pager适配器,并通过构造函数进入片段,但是如何从片段向活动获取数据使用底页

这就是我从活动中发送数据的方式

   adapter = new PagerAdapters(this.getSupportFragmentManager(), 
   tabLayout.getTabCount(),_id);

并使用片段中的构造函数接收数据

  public Filterfragment(String _id) {
    this._id = _id;
}

我正在使用recyclerview显示类似enter image description here

的数据

所以我该如何将商品的ID发送到活动中,以便单击“应用”按钮时可以重新加载recyclerview数据。

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/bottom_sheet"
    android:background="@android:color/white"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabPadding="0dp"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_below="@id/tabs"
        android:layout_above="@id/applyBtn"
        android:layout_height="match_parent"
        />

    <Button
        android:id="@+id/applyBtn"
        android:layout_width="match_parent"
        android:text="Apply"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content" />

</RelativeLayout>

这是主要的recyclerview适配器

public class FilterAdapter extends RecyclerView.Adapter<FilterAdapter.FilterViewHolder>  {
    private static final String TAG = "FilterAdapter";
    Context context;
    List<TagTypeResult> tagTypeModels;
    public static int current_pos = -1;
    TagAdapter tagAdapter;
    int rotationAngle = 0;
    public FilterAdapter(Context context, List<TagTypeResult> tagTypeModels) {
        this.context = context;
        this.tagTypeModels = tagTypeModels;
    }

    @NonNull
    @Override
    public FilterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.filter_row_item,parent,false);
        return new FilterAdapter.FilterViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull FilterViewHolder filterViewHolder, int i) {
        TagTypeResult tagTypeModel = tagTypeModels.get(i);
        filterViewHolder.txt.setText(tagTypeModel.getName());

        filterViewHolder.sub_list_recycler.setVisibility(View.GONE);


        if (tagTypeModel.getTagsLists() != null) {
            if (tagTypeModel.getTagsLists().size() <= 0) {
                filterViewHolder.arrow.setVisibility(View.GONE);
                filterViewHolder.arrow.setRotationX(180);
            } else {
                filterViewHolder.arrow.setVisibility(View.VISIBLE);
                filterViewHolder.arrow.setRotationX(0);
            }
        }else {
            filterViewHolder.arrow.setVisibility(View.GONE);

        }


        tagAdapter = new TagAdapter(context, tagTypeModel.getTagsLists(), new TagInterface() {
            @Override
            public void tagClick(View view, int pos, String tagID) {

                Log.d(TAG, "tagClick: "+tagID);


            }
        });
        filterViewHolder.sub_list_recycler.setAdapter(tagAdapter);
        tagAdapter.notifyDataSetChanged();


        if (current_pos == filterViewHolder.getAdapterPosition()){
            if (filterViewHolder.sub_list_recycler.getVisibility() == View.GONE) {
                filterViewHolder.sub_list_recycler.setVisibility(View.VISIBLE);
            }else {
                filterViewHolder.sub_list_recycler.setVisibility(View.GONE);

            }
        }else {
            Log.i(TAG, "onBindViewHolder: sublist gone "+tagTypeModel.getName());
            filterViewHolder.sub_list_recycler.setVisibility(View.GONE);
        }

    }

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

    class FilterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        ImageView arrow;
        RecyclerView sub_list_recycler;
        RelativeLayout linearLayout;
        FilterViewHolder(@NonNull View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            arrow = itemView.findViewById(R.id.arrow);
            linearLayout = itemView.findViewById(R.id.main_cat_lay);

            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i(TAG, "onClick: "+current_pos);

                    if (current_pos != getAdapterPosition()) {
                        current_pos = getAdapterPosition();
                        notifyDataSetChanged();
                    }
                    else{
                        current_pos = -1;
                        notifyDataSetChanged();
                    }
                }
            });

            sub_list_recycler = itemView.findViewById(R.id.sub_list_recycler);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            sub_list_recycler.setLayoutManager(mLayoutManager);
            sub_list_recycler.addItemDecoration(new SimpleDividerItemDecoration(context));
        }
    }
}

sub recyclerView适配器:

public class TagAdapter extends RecyclerView.Adapter<TagAdapter.TagViewHolder> {
    private static final String TAG = "SublistAdapter";
    Context context;
    List<TagsList> tagsLists;
    int pos = -1;
    TagInterface tagInterface;

    public TagAdapter(Context context, List<TagsList> tagsLists,TagInterface tagInterface) {
        this.context = context;
        this.tagsLists = tagsLists;
        this.tagInterface = tagInterface;
    }

    @NonNull
    @Override
    public TagViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tag_item,parent,false);
        return new TagAdapter.TagViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull TagViewHolder tagViewHolder, int i) {

        TagsList tagsList = tagsLists.get(i);
        tagViewHolder.tagtxt.setText(tagsList.getName());

        if (tagsList.isChecked()){
            tagViewHolder.tagName.setChecked(true);
        }else {
            tagViewHolder.tagName.setChecked(false);

        }

    }

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

    public class TagViewHolder extends RecyclerView.ViewHolder {
        TextView tagtxt;
        CheckBox tagName;
        RelativeLayout childClik;

        public TagViewHolder(@NonNull View itemView) {
            super(itemView);

            //pri_txt = itemView.findViewById(R.id.pri_txt);
            tagName = itemView.findViewById(R.id.tagName);
            tagtxt = itemView.findViewById(R.id.tagtxt);
            childClik = itemView.findViewById(R.id.childClik);
            tagName.setEnabled(false);
            childClik.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    boolean isChecked = true;
                    if (isChecked)
                    {
                        tagsLists.get(getAdapterPosition()).setChecked(true);
                        tagName.setChecked(true);
                        tagInterface.tagClick(v,getAdapterPosition(),tagsLists.get(getAdapterPosition()).get_id());
                        Toast.makeText(context, "[pos]"+tagsLists.get(getAdapterPosition()).get_id(), Toast.LENGTH_SHORT).show();
                    }else
                        {
                            tagsLists.get(getAdapterPosition()).setChecked(false);
                            tagName.setChecked(false);
                        }

                }
            });


        }
    }
}

0 个答案:

没有答案