您好,登錄后才能下訂單哦!
RecyclerView是ListView的衍生而來(lái)的,它比ListView效率更高,使用起來(lái)也更加方便。
本文將會(huì)講解用RecyclerView如何實(shí)現(xiàn):垂直列表效果、橫向列表效果、網(wǎng)格列表效果、瀑布流(橫向與縱向),以及Item的點(diǎn)擊監(jiān)聽(tīng)(因?yàn)镽ecyclerView沒(méi)有item的點(diǎn)擊監(jiān)聽(tīng)功能,這里是我自創(chuàng)的,感覺(jué)還算好用)。效果還算強(qiáng)大吧?哈哈,下面看源碼
(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)Item
<?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 = {"直播", "番劇", "動(dòng)畫(huà)", "音樂(lè)", "舞蹈", "游戲", "科技", "娛樂(lè)", "鬼畜", "電影", "電視劇", "游戲中心",}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); /** * 設(shè)置固定尺寸 */ recyclerView.setHasFixedSize(false); /** * 想用線(xiàn)性布局就用這個(gè) */ //LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this); /** * 如果用線(xiàn)行布局的話(huà),可以設(shè)置垂直/橫向 */ // linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); /** * 想用網(wǎng)格布局就用這個(gè) */ //GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2); /** * 想用瀑布流布局就用這個(gè) */ StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL); /** * 在這里選擇排列布局(用哪個(gè)就放進(jìn)來(lái)就行了) */ recyclerView.setLayoutManager(staggeredGridLayoutManager); /** * 設(shè)置適配器 */ recyclerView.setAdapter(new MyAdapter(ibsValue)); } }
(4)適配器Adapter
/** * Created by Administrator on 2015/11/15. */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{ /** * 這里創(chuàng)建一個(gè)數(shù)組,準(zhǔn)備接收傳過(guò)來(lái)的數(shù)據(jù) */ public String[] datas; /** * 這里調(diào)用在創(chuàng)建MyAdapter實(shí)例的時(shí)候,可以將數(shù)據(jù)傳過(guò)來(lái) * @param mdatas */ public MyAdapter(String[] mdatas){ datas=mdatas; } /** * 這里加載加載Item,并且創(chuàng)建ViewHolder對(duì)象,把加載的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綁定數(shù)據(jù) * @param holder * @param position */ @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(datas[position]); } /** * 這里返回item數(shù)量 * @return */ @Override public int getItemCount() { return datas.length; } /** * ViewHolder類(lèi),注意要繼承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); } /** * 這里可以設(shè)置點(diǎn)擊監(jiān)聽(tīng) * @param v */ @Override public void onClick(View v) { Toast.makeText(context,mTextView.getText().toString(), Toast.LENGTH_SHORT).show(); } } }
運(yùn)行效果:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。