RecyclerView编程记录

感觉里面的编程思路还不错,所以记录下来,O O

程序结构:

luffy_student_manager

  • FragmentBase.class
  • StudentActivity.class
  • StudentFragment.class
  • StudentListActivity.class
  • StudentListFragment.class
  • Student.class
  • StudentList.class
  • layout

  • fragment_student.xml
  • fragment_student_recycler.xml
  • recycler_item.xml
  • 程序代码:

    FragmentBase.class为封装了通用方法的抽象类:

    package luffy_student_manager;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import io.github.grooters.luffy.R;
    public abstract class FragmentBase extends FragmentActivity{
        public abstract Fragment createFragment();
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_student);
            FragmentManager manager=getSupportFragmentManager();
            FragmentTransaction transaction=manager.beginTransaction();
            //调用被重写的createFragment方法,创建对应的Fragment实例
            Fragment fragment=createFragment();
            transaction.replace(R.id.student_fragment,fragment,"StudentFragment");
            transaction.commit();
        }
    }

    StudentActivity.class为添加学生功能的活动:

    package luffy_student_manager;
    import android.support.v4.app.Fragment;
    public class StudentActivity extends FragmentBase {
        @Override
        public Fragment createFragment() {
            return  new StudentFragment();
        }
    }

    StudentFragment.class为添加学生功能的Fragment:

    package luffy_student_manager;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import io.github.grooters.luffy.R;
    public class StudentFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment_student,container,false);
            Button button=view.findViewById(R.id.submit);
            final EditText editId,editName,ediDepartment;
            editId=view.findViewById(R.id.edit_id);
            editName=view.findViewById(R.id.edit_name);
            ediDepartment=view.findViewById(R.id.edit_department);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(getActivity(),StudentListActivity.class);
                    //初始化学生列表学生个数
                    StudentList.setStudents();
                    //创建一个新的学生
                    Student student=new Student(editId.getText().toString(),editName.getText().toString(),ediDepartment.getText().toString());
                    //传给展示学生列表的activity
                    intent.putExtra("Student",student);
                    startActivity(intent);
                }
            });
            return view;
        }
    }

    StudentListActivity.class为显示学生列表的活动:

    package luffy_student_manager;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    
    
    public class StudentListActivity extends FragmentBase {
        private static final String TAG=StudentListActivity.class.getSimpleName();
        @Override
        public Fragment createFragment() {
            StudentListFragment studentListFragment=new StudentListFragment();
            Bundle bundle=new Bundle();
            if(getIntent().getSerializableExtra("Student")!=null){
                Log.i(TAG,"getSerializableExtra:not_null");
                //取出传过来的学生,再次封装到bundle中
                bundle.putSerializable("Student",getIntent().getSerializableExtra("Student"));
            }
            //通过setArguments方法存入该fragment
            studentListFragment.setArguments(bundle);
            return studentListFragment;
        }
    }
    

    StudentListFragment.class为显示学生列表的Fragment:

    package luffy_student_manager;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import java.util.List;
    import io.github.grooters.luffy.R;
    public class StudentListFragment extends Fragment{
        private static final String TAG=StudentListFragment.class.getSimpleName();
        private List<Student> students;
        public StudentListFragment(){
            students=StudentList.getStudents();
        }
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment_student_recycler,container,false);
            if(getArguments()!=null) {
                Log.i(TAG,"getArguments:not_null");
                //通过getArguments方法取出对象,不能在构造方法取,因为实例化该Fragment时,还未setArgument
                Student student=(Student) getArguments().getSerializable("Student");
                students.add(student);
            }
            RecyclerView recyclerView=view.findViewById(R.id.student_list);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            StudentRecyclerAdapter adapter=new StudentRecyclerAdapter();
            recyclerView.setAdapter(adapter);
            return view;
        }
        private class StudentViewHolder extends RecyclerView.ViewHolder{
            private TextView textViewId,textViewName,textViewDepartment;
            private StudentViewHolder(@NonNull View itemView) {
                super(itemView);
                textViewId=itemView.findViewById(R.id.textView_id);
                textViewName=itemView.findViewById(R.id.textVew_name);
                textViewDepartment=itemView.findViewById(R.id.textView_department);
            }
        }
        private class StudentRecyclerAdapter extends RecyclerView.Adapter<StudentViewHolder>{
            private StudentRecyclerAdapter() {
                super();
            }
            @Override
            public int getItemCount() {
                //Log.i(TAG,"getItemCount:"+StudentList.getStudents().size());
                return StudentList.getStudents().size();
            }
            @Override
            public long getItemId(int position) {
                return super.getItemId(position);
            }
            @NonNull
            @Override
            public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                return new StudentViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.recycler_item,viewGroup,false));
            }
            @Override
            public void onBindViewHolder(@NonNull StudentViewHolder studentViewHolder, int i) {
                studentViewHolder.textViewId.setText(students.get(i).getId());
                studentViewHolder.textViewName.setText(students.get(i).getName());
                studentViewHolder.textViewDepartment.setText(students.get(i).getDepartment());
            }
        }
    }
    

    fragment_student.xml为添加学生Fragment对应的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:text="学号"
                android:layout_marginStart="50dp"
                android:layout_marginEnd="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <EditText
                android:id="@+id/edit_id"
                android:layout_width="200dp"
                android:layout_height="40dp" />
    
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:text="姓名"
                android:layout_marginStart="50dp"
                android:layout_marginEnd="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <EditText
                android:id="@+id/edit_name"
                android:layout_width="200dp"
                android:layout_height="40dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:text="学院"
                android:layout_marginStart="50dp"
                android:layout_marginEnd="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <EditText
                android:id="@+id/edit_department"
                android:layout_width="200dp"
                android:layout_height="40dp" />
        </LinearLayout>
        <Button
            android:layout_gravity="center"
            android:id="@+id/submit"
            android:text="提交"
            android:layout_marginTop="30dp"
            android:layout_width="250dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    fragment_student_recycler.xml为显示学生列表Fragment对应的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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/student_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    recycler_item.xml为RecyclerView的Item布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView_id"
            android:layout_margin="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/textVew_name"
            android:layout_margin="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_margin="10dp"
            android:id="@+id/textView_department"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值