RecyclerView是ListView的衍生而來的,它比ListView效率更高,使用起來也更加方便。java
本文將會講解用RecyclerView如何實現:垂直列表效果、橫向列表效果、網格列表效果、瀑布流(橫向與縱向),以及Item的點擊監聽(由於RecyclerView沒有item的點擊監聽功能,這裏是我自創的,感受還算好用)。效果還算強大吧?哈哈,下面看源碼android
(1)主佈局數組
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
(2)Itemide
<?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:orientation="vertical"> <TextView android:id="@+id/tv" android:textSize="60dp" android:padding="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
(3)MainActivity代碼佈局
public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private String[] ibsValue = {"直播", "番劇", "動畫", "音樂", "舞蹈", "遊戲", "科技", "娛樂", "鬼畜", "電影", "電視劇", "遊戲中心",}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); /** * 設置固定尺寸 */ recyclerView.setHasFixedSize(false); /** * 想用線性佈局就用這個 */ //LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this); /** * 若是用線行佈局的話,能夠設置垂直/橫向 */ // linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); /** * 想用網格佈局就用這個 */ //GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2); /** * 想用瀑布流佈局就用這個 */ StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL); /** * 在這裏選擇排列布局(用哪一個就放進來就好了) */ recyclerView.setLayoutManager(staggeredGridLayoutManager); /** * 設置適配器 */ recyclerView.setAdapter(new MyAdapter(ibsValue)); } }
(4)適配器Adapter動畫
/** * Created by Administrator on 2015/11/15. */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{ /** * 這裏建立一個數組,準備接收傳過來的數據 */ public String[] datas; /** * 這裏調用在建立MyAdapter實例的時候,能夠將數據傳過來 * @param mdatas */ public MyAdapter(String[] mdatas){ datas=mdatas; } /** * 這裏加載加載Item,而且建立ViewHolder對象,把加載的Item(View)傳給viewholder * @param parent * @param viewType * @return */ @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false); ViewHolder viewHolder=new ViewHolder(view,parent.getContext()); return viewHolder; } /** * 這裏給item中的子View綁定數據 * @param holder * @param position */ @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(datas[position]); } /** * 這裏返回item數量 * @return */ @Override public int getItemCount() { return datas.length; } /** * ViewHolder類,注意要繼承RecyclerView.ViewHolder */ public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView mTextView; public Context context; public ViewHolder(View itemView,Context context) { super(itemView); mTextView= (TextView) itemView.findViewById(R.id.tv); this.context=context; itemView.setOnClickListener(this); } /** * 這裏能夠設置點擊監聽 * @param v */ @Override public void onClick(View v) { Toast.makeText(context,mTextView.getText().toString(), Toast.LENGTH_SHORT).show(); } } }
運行效果:this