首页 > 代码库 > RecyclerView的用法
RecyclerView的用法
MainActivity的代码如下:
package com.example.zhangmeng.recyclerview;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;public class MainActivity extends AppCompatActivity { private RecyclerView rv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); rv=new RecyclerView(this); setContentView(rv); rv.setLayoutManager(new LinearLayoutManager(this)); rv.setAdapter(new MyAdapter()); }}
MyAdapter:
1 package com.example.zhangmeng.recyclerview; 2 3 import android.support.v7.widget.RecyclerView; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.TextView; 8 9 /**10 * Created by zhangmeng on 2016/9/15.11 */12 class MyAdapter extends RecyclerView.Adapter {13 14 15 @Override16 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {17 18 return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item,null));19 }20 21 @Override22 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {23 ViewHolder vh = (ViewHolder) holder;24 vh.getTv_title().setText("Title"+position);25 vh.getTv_content().setText("content"+position);26 27 }28 29 @Override30 public int getItemCount() {31 return 10;32 }33 }34 35 class ViewHolder extends RecyclerView.ViewHolder {36 private TextView tv_title;37 private TextView tv_content;38 public ViewHolder(View itemView) {39 super(itemView);40 tv_title=(TextView) itemView.findViewById(R.id.textView_title);41 tv_content=(TextView) itemView.findViewById(R.id.textView_content);42 }43 44 public TextView getTv_title() {45 return tv_title;46 }47 public TextView getTv_content() { return tv_content; }48 49 50 }
Activitylayout:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin"10 tools:context="com.example.zhangmeng.recyclerview.MainActivity">11 12 <TextView13 android:layout_width="wrap_content"14 android:layout_height="wrap_content"15 android:text="Hello World!" />16 </RelativeLayout>
Itemlayout:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <TextView 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:textAppearance="?android:attr/textAppearanceLarge"10 android:text="Large Text"11 android:id="@+id/textView_title"12 android:layout_gravity="center_horizontal" />13 14 <TextView15 android:layout_width="match_parent"16 android:layout_height="wrap_content"17 android:textAppearance="?android:attr/textAppearanceSmall"18 android:text="Small Text"19 android:id="@+id/textView_content" />20 </LinearLayout>
RecyclerView的用法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。