单击内部回收器视图项时将嵌套回收器视图的数据显示到片段

时间:2021-01-30 10:05:11

标签: java android android-fragments android-recyclerview

这是我的内部适配器类:

public class NestedInnerRecyclerAdapterClass extends RecyclerView.Adapter<NestedInnerRecyclerAdapterClass.InnerViewHolder> {
Context innerContext;
List<NestedInnerModelClass> innerList;

    public NestedInnerRecyclerAdapterClass(Context innerContext, List<NestedInnerModelClass> innerList ) {
        this.innerContext = innerContext;
        this.innerList = innerList;
        
    }

    @NonNull
    @Override
    public InnerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new InnerViewHolder(LayoutInflater.from(innerContext).inflate(R.layout.innerrecycleritems, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull InnerViewHolder holder, int position) {
        holder.textName.setText(innerList.get(position).getName());
        holder.textPrice.setText("Price " + innerList.get(position).getPrice());
        Glide.with(innerContext).load(innerList.get(position).getImage_Url()).placeholder(R.mipmap.ic_launcher).into(holder.imageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                innerInterFace.sendInnerData(holder.textName.getText().toString(), holder.textPrice.getText().toString(), innerList.get(position).getImage_Url());

                Toast.makeText(innerContext, innerList.get(position).getName(), Toast.LENGTH_SHORT).show();
//         NavController navController = (NavController) Navigation.createNavigateOnClickListener(R.id.action_homeFragment_to_detailFragment);
                Navigation.createNavigateOnClickListener(R.id.action_homeFragment_to_detailFragment).onClick(holder.itemView);
               
            }
        });
    }

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

    public class InnerViewHolder extends RecyclerView.ViewHolder {
        TextView textName, textPrice;
        CircleImageView imageView;

        public InnerViewHolder(@NonNull View itemView) {
            super(itemView);
            textName = itemView.findViewById(R.id.innertextviewName);
            textPrice = itemView.findViewById(R.id.innertextviewPrice);
            imageView = itemView.findViewById(R.id.innerimageView);
        }
    }


}

这是我的外部回收器适配器类:

public class NestedOuterRecyclerAdapterClass extends RecyclerView.Adapter<NestedOuterRecyclerAdapterClass.OuterViewHolder> {
Context context;
List<NestedOuterModelClass> outerList;

public NestedOuterRecyclerAdapterClass(Context context, List<NestedOuterModelClass> outerList) {
    this.context = context;
    this.outerList = outerList;
}

@NonNull
@Override
public OuterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new OuterViewHolder(LayoutInflater.from(context).inflate(R.layout.outerrecycleritem, parent, false));
}

@Override
public void onBindViewHolder(@NonNull OuterViewHolder holder, int position) {
    holder.textView.setText(outerList.get(position).getName());
    setInnerRecyclerView(holder.textView.getText().toString(), holder.innerRecyclerView, outerList.get(position).getInnerlist());
}

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


public class OuterViewHolder extends RecyclerView.ViewHolder {
    TextView textView;
    RecyclerView innerRecyclerView;

    public OuterViewHolder(@NonNull View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.outerTextView);
        innerRecyclerView = itemView.findViewById(R.id.InnerRecyclerView);

    }
}

private void setInnerRecyclerView(RecyclerView recyclerView, List<NestedInnerModelClass> innerModellist) {
    NestedInnerRecyclerAdapterClass innerAdapter = new NestedInnerRecyclerAdapterClass(context, innerModellist, title);
    recyclerView.setAdapter(innerAdapter);
    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL));
}


}

这是我的 DetailFragment 类:

 public class DetailFragment extends Fragment {
        private CircleImageView circleImageView;
        private TextView name, price, description;
        String namez;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_detail, container, false);
        }
    
        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            name = view.findViewById(R.id.nametxt1);
            price = view.findViewById(R.id.pricetxt1);
            circleImageView = view.findViewById(R.id.circleImageView);
    
        }
    }

我想像 Play 商店一样显示我的数据。这意味着当我点击内部回收器视图项时,我想在详细信息片段中显示两个回收器视图数据。

以下是我的 XML 文件。

这是我家的片段布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawerlayout"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    tools:context=".HomeFragment">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="0.5"
            android:orientation="horizontal"
            android:weightSum="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <ImageButton

                android:id="@+id/navOpener"
                android:layout_width="20dp"
                android:layout_height="match_parent"
                android:layout_weight="0.2"
                android:background="@drawable/navopener" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:layout_weight="1.6"
                android:padding="10dp"
                android:text="My Shopping App"
                android:textSize="24sp"
                android:textStyle="bold" />


            <ImageView
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:layout_weight="0.2"
                android:src="@drawable/cart" />


        </LinearLayout>

        <SearchView
            android:id="@+id/searchitems"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:background="@drawable/corner"

            android:iconifiedByDefault="false"
            android:padding="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.351"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout"
            app:layout_constraintVertical_bias="0.0" />


        <include
            android:layout_marginTop="3dp"
            layout="@layout/homescreenother"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/searchitems"
            app:layout_constraintVertical_bias="0.0" />


    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationDrawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/navigationmenu" />

</androidx.drawerlayout.widget.DrawerLayout>

这是设置主回收器视图的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/horizontalRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:padding="5dp"
            app:cardElevation="10dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/imageSlider"
                    android:layout_width="match_parent"
                    android:layout_height="250dp" />

                <com.tbuonomo.viewpagerdotsindicator.SpringDotsIndicator
                    android:id="@+id/spring_dots_indicator"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_marginLeft="100dp"
                    app:dampingRatio="0.5"
                    app:dotsColor="@color/white"
                    app:dotsCornerRadius="2dp"
                    app:dotsSize="16dp"
                    app:dotsSpacing="6dp"
                    app:dotsStrokeColor="@color/teal_700"
                    app:dotsStrokeWidth="2dp"
                    app:stiffness="300" />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>

        <androidx.recyclerview.widget.RecyclerView

            tools:listitem="@layout/outerrecycleritem"
            android:id="@+id/outerRecycle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp" />


    </LinearLayout>


</androidx.core.widget.NestedScrollView>

这是外部回收器视图项菜单布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:padding="5dp">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardCornerRadius="5dp"
        app:cardElevation="10dp"
        app:cardPreventCornerOverlap="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:textAllCaps="true"
                android:id="@+id/outerTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/coiny"
                android:text="type here"
                android:textSize="18sp" />

            <androidx.recyclerview.widget.RecyclerView

                tools:listitem="@layout/innerrecycleritems"
                android:id="@+id/InnerRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

</LinearLayout>

这是内部回收器视图项目布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:elevation="10dp"
    android:padding="5dp"
    app:cardCornerRadius="10dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/innerimageView"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/house" />

        <TextView
            android:id="@+id/innertextviewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:letterSpacing=".08"
            android:text="Name of Item"
            android:textAlignment="center"
            android:textSize="20sp" />

        <TextView
            android:layout_marginTop="4dp"
            android:id="@+id/innertextviewPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Price of Item"
            android:textAlignment="center"
            android:textSize="20sp" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

0 个答案:

没有答案