本篇文章給大家分享的是有關(guān)怎么在Android 應(yīng)用中利用RecyclerView實現(xiàn)一個網(wǎng)格布局,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
專業(yè)領(lǐng)域包括成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)、電子商務(wù)商城網(wǎng)站建設(shè)、微信營銷、系統(tǒng)平臺開發(fā), 與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,創(chuàng)新互聯(lián)公司的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。activity_main.xml
message_main_notice_list.xml
適配器MyRecyclerViewAdapter.java:
package com.example.administrator.recyclerviewtest; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class MyRecyclerViewAdapter extends RecyclerView.Adapter{ private List mList; static class ViewHolder extends RecyclerView.ViewHolder{ View myView; ImageView imageView; TextView title; public ViewHolder(View itemView) { super(itemView); myView = itemView; imageView = (ImageView) itemView.findViewById(R.id.iv_image); title = (TextView) itemView.findViewById(R.id.tv_title); } } public MyRecyclerViewAdapter(List list){ this.mList = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_main_notice_list,null); final ViewHolder holder = new ViewHolder(view); return holder; } //將數(shù)據(jù)綁定到控件上 @Override public void onBindViewHolder(ViewHolder holder, int position) { ItemBean bean = mList.get(position); holder.imageView.setBackgroundResource(bean.itemImage); holder.title.setText(bean.itemTitle); } @Override public int getItemCount() { return mList.size(); } //下面兩個方法提供給頁面刷新和加載時調(diào)用 public void add(List addMessageList) { //增加數(shù)據(jù) int position = mList.size(); mList.addAll(position, addMessageList); notifyItemInserted(position); } public void refresh(List newList) { //刷新數(shù)據(jù) mList.removeAll(mList); mList.addAll(newList); notifyDataSetChanged(); } }
主方法:
package com.example.administrator.recyclerviewtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.Date; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private Listlist; private MyRecyclerViewAdapter myAdapte1r; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = new ArrayList (); for ( int i=0; i< 19;i++){ list.add(new ItemBean( R.mipmap.logo, "Hello", new Date().toString()+"" )); } myAdapte1r = new MyRecyclerViewAdapter(list); recyclerView = (RecyclerView) findViewById(R.id.message_notice_list_item); //縱向線性布局 //LinearLayoutManager layoutManager = new LinearLayoutManager(this); //縱向線性布局 GridLayoutManager layoutManager = new GridLayoutManager(this,2); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(myAdapte1r); } }
以上就是怎么在Android 應(yīng)用中利用RecyclerView實現(xiàn)一個網(wǎng)格布局,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。