使用recyclerView生成带有折叠单元库的列表

时间:2019-03-25 09:21:02

标签: android android-recyclerview

“我的代码”不使用折叠单元库中的recyclerView生成列表。任何人都可以帮忙.. ??

/ MyAdapter代码 /

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

            List<CoursesFoldingCellModelClass> coursesFoldingCellModelClasses;
            Context context;

            public MyAdapter(List<CoursesFoldingCellModelClass> coursesFoldingCellModelClasses, Context context) {
                this.coursesFoldingCellModelClasses = coursesFoldingCellModelClasses;
                this.context = context;
            }

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

            @Override
            public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

                CoursesFoldingCellModelClass coursesFoldingCellModelClass = coursesFoldingCellModelClasses.get(i);

                viewHolder.coursename.setText(coursesFoldingCellModelClass.getCoursename());
                viewHolder.eligibility.setText(coursesFoldingCellModelClass.getEligibility());
                viewHolder.courseimage.setImageResource(coursesFoldingCellModelClass.getCourseimage());
                viewHolder.coursedescription.setText(coursesFoldingCellModelClass.getCoursedescription());
                viewHolder.admissionprocedure.setText(coursesFoldingCellModelClass.getAdmissionprocedure());
            }

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

            public class ViewHolder extends RecyclerView.ViewHolder{

                public TextView coursename;
                public TextView eligibility;
                public ImageView courseimage;
                public TextView coursedescription;
                public TextView admissionprocedure;
                CardView cv;

                public ViewHolder(@NonNull View itemView) {
                    super(itemView);
                    coursename =(TextView)itemView.findViewById(R.id.course_name);
                    eligibility =(TextView)itemView.findViewById(R.id.eligibility);
                    courseimage =(ImageView) itemView.findViewById(R.id.course_image);
                    coursedescription =(TextView)itemView.findViewById(R.id.course_description);
                    admissionprocedure =(TextView)itemView.findViewById(R.id.admission_procedure);

                    cv = (CardView) itemView.findViewById(R.id.card_view);

                    final FoldingCell fc = (FoldingCell)itemView.findViewById(R.id.folding_cell);
                    fc.initialize(1000, Color.DKGRAY, 2);
                    fc.setTag(cv);


                    fc.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fc.toggle(false);
                        }
                    });
                }
            }
        }

/ ModelClassCode /

         public class CoursesFoldingCellModelClass {

            private String coursename;
            private String eligibility;
            private int courseimage;
            private String coursedescription;
            private String admissionprocedure;

            public CoursesFoldingCellModelClass(String coursename, String eligibility, int courseimage, String coursedescription, String admissionprocedure) {
                this.coursename = coursename;
                this.eligibility = eligibility;
                this.courseimage = courseimage;
                this.coursedescription = coursedescription;
                this.admissionprocedure = admissionprocedure;
            }

            public String getCoursename() {
                return coursename;
            }

            public void setCoursename(String coursename) {
                this.coursename = coursename;
            }

            public String getEligibility() {
                return eligibility;
            }

            public void setEligibility(String eligibility) {
                this.eligibility = eligibility;
            }

            public int getCourseimage() {
                return courseimage;
            }

            public void setCourseimage(int courseimage) {
                this.courseimage = courseimage;
            }

            public String getCoursedescription() {
                return coursedescription;
            }

            public void setCoursedescription(String coursedescription) {
                this.coursedescription = coursedescription;
            }

            public String getAdmissionprocedure() {
                return admissionprocedure;
            }

            public void setAdmissionprocedure(String admissionprocedure) {
                this.admissionprocedure = admissionprocedure;
            }
        }

/ * MainActivity.java * /

        public class MainActivity extends AppCompatActivity {

            private RecyclerView recyclerView;
            private RecyclerView.Adapter adapter;

            private List<CoursesFoldingCellModelClass> coursesFoldingCellModelClasses;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                recyclerView = (RecyclerView)findViewById(R.id.recyclerViewForCourses);
                recyclerView.setHasFixedSize(true);
                recyclerView.setLayoutManager(new LinearLayoutManager(this));

                coursesFoldingCellModelClasses = new ArrayList<>();
                coursesFoldingCellModelClasses.add(new CoursesFoldingCellModelClass("Computer Science Engineering With AI","(10+2)PCM",R.drawable.cat1,"My Name is Addy Prakash Nautiyal","U Can Apply Online as Well as Offline"));
                coursesFoldingCellModelClasses.add(new CoursesFoldingCellModelClass("Computer Science Engineering With AI","(10+2)PCM",R.drawable.cat1,"My Name is Addy Prakash Nautiyal","U Can Apply Online as Well as Offline"));
                coursesFoldingCellModelClasses.add(new CoursesFoldingCellModelClass("Computer Science Engineering With AI","(10+2)PCM",R.drawable.cat1,"My Name is Addy Prakash Nautiyal","U Can Apply Online as Well as Offline"));

                adapter =new MyAdapter(coursesFoldingCellModelClasses,this);
                recyclerView.setAdapter(adapter);

                // get our folding cell
            }

}

/ activity_main.xml /

        <?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"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

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


        </RelativeLayout>

/ foldingcell.xml /

            <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:background="@drawable/bgcolor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:folding-cell="http://schemas.android.com/apk/res-auto"
            android:orientation="vertical"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:padding="10dp">


            <androidx.cardview.widget.CardView
                android:id="@+id/card_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <com.ramotion.foldingcell.FoldingCell
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/folding_cell"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                folding-cell:animationDuration="1000"
                folding-cell:backSideColor="#373737"
                folding-cell:additionalFlipsCount="1"
                folding-cell:cameraHeight="30">
                ​
                <FrameLayout
                    android:id="@+id/cell_content_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:background="#E5E5E5">

                        <TextView
                            android:id="@+id/course_name_content"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="10dp"
                            android:text="Computer Science with Cloud Computing"
                            android:textAlignment="center"
                            android:textColor="@android:color/white"
                            android:textSize="18sp"
                            android:textStyle="bold"
                            android:background="@drawable/border_layout"/>

                        <ImageView
                            android:id="@+id/course_image"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:src="@drawable/cat1"
                            android:layout_margin="10dp"
                            android:scaleType="fitXY"
                            android:elevation="4dp"/>


                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_marginEnd="10dp">


                            <TextView
                                android:id="@+id/course_description"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:text="Hello My name is Bhanu Prakash Nautiyal And i am pursuing my Graducation from Graphic Era Hill University and currently i am in my 8th Semester"
                                android:textColor="#000"
                                android:textSize="15sp"
                                android:layout_marginTop="8dp"/>

                            <RelativeLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center">
                                <ImageView
                                    android:id="@+id/logo"
                                    android:layout_width="130dp"
                                    android:layout_height="130dp"
                                    android:scaleType="fitCenter"
                                    android:alpha="0.2"
                                    android:src="@drawable/logo_geu_deemed_flame" />

                            </RelativeLayout>
                            <LinearLayout
                                android:id="@+id/admission_procedure_container"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="horizontal"
                                android:layout_marginTop="8dp"
                                android:layout_marginBottom="8dp"
                                android:layout_below="@+id/course_description">

                                <TextView
                                    android:layout_width="300dp"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="1"
                                    android:textStyle="bold"
                                    android:textColor="#000"
                                    android:text="Admission Procedure:"/>

                                <TextView
                                    android:id="@+id/admission_procedure"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:textColor="#000"
                                    android:layout_weight="1"
                                    android:text="Merit prepared on basis of (10+2)% / JEE Main Score"/>

                            </LinearLayout>

                            <Button
                                android:id="@+id/Apply_Now_Btn"
                                android:layout_below="@+id/admission_procedure_container"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:background="@android:color/holo_red_dark"
                                android:text="Apply Now"
                                android:layout_marginTop="10dp"
                                android:layout_marginBottom="10dp"
                                android:textColor="@android:color/white" />

                        </RelativeLayout>




                    </LinearLayout>

                </FrameLayout>
                ​
                <FrameLayout
                    android:id="@+id/cell_title_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:background="#E5E5E5"
                        android:baselineAligned="false"
                        android:orientation="vertical">

                        <!-- LEFT TITLE PART -->



                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <LinearLayout
                                android:layout_width="150dp"
                                android:layout_height="match_parent"
                                android:background="#343434"
                                android:gravity="center"
                                android:orientation="vertical">


                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:text="Eligibility"
                                    android:textAlignment="center"
                                    android:textColor="@android:color/white"
                                    android:textSize="18sp"
                                    android:textStyle="bold" />

                                <TextView
                                    android:id="@+id/eligibility"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:text="(10+2) PCM"
                                    android:layout_marginTop="4dp"
                                    android:textAlignment="center"
                                    android:textColor="#fff"
                                    android:textSize="18sp" />

                            </LinearLayout>

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


                                <TextView
                                    android:id="@+id/course_name"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:padding="8dp"
                                    android:text="Computer Science with Cloud Computing"
                                    android:textAlignment="center"
                                    android:textColor="@android:color/black"
                                    android:textSize="20sp"
                                    android:textStyle="bold"/>

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@+id/course_name"
                                    android:layout_marginTop="1dp"
                                    android:textAlignment="center"
                                    android:padding="8dp"
                                    android:text="Show Details"
                                    android:textSize="20sp"
                                    android:textColor="@android:color/black"/>
                            </RelativeLayout>

                        </LinearLayout>


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

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:background="@android:color/holo_red_dark"
                                android:textColor="@android:color/white"
                                android:text="Course Fee"
                                android:elevation="2dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:background="@android:color/holo_red_dark"
                                android:textColor="@android:color/white"
                                android:text="Curriculum"
                                android:elevation="2dp"/>

                            <Button
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:background="@android:color/holo_red_dark"
                                android:textColor="@android:color/white"
                                android:text="Apply Now"
                                android:elevation="2dp"/>

                        </LinearLayout>
                    </LinearLayout>
                </FrameLayout>
                ​

            </com.ramotion.foldingcell.FoldingCell>
            </androidx.cardview.widget.CardView>

        </LinearLayout>

enter image description here 我的代码无法使用折叠式单元库中的recyclerView生成列表。任何人都可以帮忙。

1 个答案:

答案 0 :(得分:0)

在下面的onCreateViewHolder方法上设置,它对我有用。

fc.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fc.toggle(false);
                        }
                    });