在本次移動應(yīng)用的大作業(yè)中滴劲,有很多的地方都涉及到了列表展示攻晒,之前最常用的做法當(dāng)然是使用ListView,但是偶然看到了RecyclerView班挖,在試用了之后發(fā)現(xiàn)雖然稍微復(fù)雜一點(diǎn)鲁捏,但能很好地實(shí)現(xiàn)橫向滾動,更能輕松地實(shí)現(xiàn)瀑布式的布局萧芙,為開發(fā)過程減輕了許多負(fù)擔(dān)给梅。
在項(xiàng)目中,目前只需要用到線性布局双揪,在此簡單介紹一下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差不多,他們的單項(xiàng)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的布局還沒有進(jìn)行調(diào)整渔期,把數(shù)據(jù)放進(jìn)去再說运吓。
而使用RecyclerView的關(guān)鍵點(diǎn)就是繼承重寫 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方法中需要進(jìn)行的操作
mRecyclerView = (RecyclerView) findViewById(R.id.classRView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(classAdapter = new classAdapter());
}
在本Activity中使用了內(nèi)部類疯趟,進(jìn)行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);
}
}
}
這樣拘哨,一些簡單的數(shù)據(jù)就被放進(jìn)RecyclerView中啦!
還有更高級的用法請移步以下博文:
Android RecyclerView 使用完全解析 體驗(yàn)藝術(shù)般的控件