RecyclerView——只是稍微复杂一点儿

在本次移动应用的大作业中,有很多的地方都涉及到了列表展示,之前最常用的做法当然是使用ListView,但是偶然看到了RecyclerView,在试用了之后发现虽然稍微复杂一点,但能很好地实现横向滚动,更能轻松地实现瀑布式的布局,为开发过程减轻了许多负担。
在项目中,目前只需要用到线性布局,在此简单介绍一下RecyclerView的使用方法。
以下就是RecyclerView的根布局文件

 <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/classRView"
            >

        </android.support.v7.widget.RecyclerView>

同ListView差不多,他们的单项Item都需要在另外的xml文件中定义:

<?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="100dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/studentName" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/studentEmail"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/studentId" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/studentSex" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/studentGit"
        android:layout_gravity="right" />

</LinearLayout>

目前item的布局还没有进行调整,把数据放进去再说。
而使用RecyclerView的关键点就是继承重写 RecyclerView.Adapter 和 RecyclerView.ViewHolder,示例代码如下:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.student_main);
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        String name = bundle.getString("Identify");
        Log.i("name",name);
        Toast.makeText(studentMainActivity.this, "name is !"+name, Toast.LENGTH_SHORT).show();


        new Thread() {
            @Override
            public void run() {
                try {
                    initData();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }.start();

//以下就是RecyclerView在onCreate方法中需要进行的操作
        mRecyclerView = (RecyclerView) findViewById(R.id.classRView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(classAdapter = new classAdapter());

    }

在本Activity中使用了内部类,进行Adapter的重写

   class classAdapter extends RecyclerView.Adapter<classAdapter.MyViewHolder>
    {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                    studentMainActivity.this).inflate(R.layout.class_listview_item, parent,
                    false));
            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position)
        {
            holder.name.setText(names.get(position));
            holder.id.setText(ids.get(position));
            holder.sex.setText(sexs.get(position));
            holder.email.setText(emails.get(position));
            holder.gitAccount.setText(gitAccounts.get(position));
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder
        {
            TextView name;
            TextView id;
            TextView sex;
            TextView email;
            TextView gitAccount;

            public MyViewHolder(View view)
            {
                super(view);
                name = (TextView) view.findViewById(R.id.studentName);
                id = (TextView) view.findViewById(R.id.studentId);
                sex = (TextView) view.findViewById(R.id.studentSex);
                email = (TextView) view.findViewById(R.id.studentEmail);
                gitAccount = (TextView) view.findViewById(R.id.studentGit);
            }
        }
    }

这样,一些简单的数据就被放进RecyclerView中啦!

还有更高级的用法请移步以下博文:
Android RecyclerView 使用完全解析 体验艺术般的控件

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容