您好,登錄后才能下訂單哦!
Android中怎么利用RecyclerView實現數據列表展示效果,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
1.首先導入依賴:
compile 'com.android.support:recyclerview-v7:25.3.1'
2.適配器類:
public class NewsAdapter extends RecyclerView.Adapter { private View view; private Context mcontext; private ArrayList<NewsData.DataBean> mlist=new ArrayList<>(); public NewsAdapter(Context mcontext) { this.mcontext = mcontext; } //找到需要導入的布局 @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { view = LayoutInflater.from(mcontext).inflate(R.layout.news_item, null); return new ViewHolder(view); } //加載相應的內容 @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ViewHolder viewholder=(ViewHolder)holder; viewholder.textview.setText(""+getItem(position).getNews_title()); Glide.with(mcontext).load(getItem(position).getPic_url()).into(viewholder.ima); } //手寫的重置數據的方法 public void setdata(ArrayList<NewsData.DataBean> list){ if (list!=null&&list.size()>0){ mlist.addAll(list); } } //手寫的獲得視圖的方法 public NewsData.DataBean getItem(int position){ return mlist.get(position); }; @Override public int getItemCount() { return mlist.size(); } //定義的ViewHolder類繼承RecyclerView.ViewHolder 并找到布局中的ID public class ViewHolder extends RecyclerView.ViewHolder{ private TextView textview; private ImageView ima; public ViewHolder(View itemView) { super(itemView); textview=(TextView)itemView.findViewById(R.id.tv); ima=(ImageView)itemView.findViewById(R.id.image); } } }
3.主界面(用到RecyclerView的主要是initdata()方法和Handler):
public class MainActivity extends AppCompatActivity implements IMainView{ private MainPresenter mainp; private NewsAdapter md; private RecyclerView recy; private ArrayList<NewsData.DataBean> mlist=new ArrayList<>(); private String url="http://api.expoon.com/AppNews/getNewsList/type/1/p/1"; private Handler hand=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Gson gson=new Gson(); if (msg.what==1) { NewsData newsData = gson.fromJson(msg.obj.toString(), NewsData.class); mlist=(ArrayList<NewsData.DataBean>) newsData.getData(); md.setdata((ArrayList<NewsData.DataBean>) mlist); md.notifyDataSetChanged(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); initdata(); initdatafromserver(); } private void initview() { recy=(RecyclerView)findViewById(R.id.recy); } private void initdatafromserver() { //加載網絡數據,找P層去要 mainp.loadDataFromServer(url); } private void initdata() { mainp=new MainPresenter(this); //設置適配器 md=new NewsAdapter(this); //創建默認的線性LayoutManager recy.setLayoutManager(new LinearLayoutManager(this)); recy.setAdapter(md); } //重寫的IMainView類接口的兩個方法 @Override public void successfulCallback(String str) { Message msg = Message.obtain(); msg.what=1; msg.obj=str; hand.sendMessage(msg); } @Override public void errCallback(int code, String msg) { } }
看完上述內容,你們掌握Android中怎么利用RecyclerView實現數據列表展示效果的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。