首页 > 代码库 > RecyclerView的简单用法

RecyclerView的简单用法

 

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;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;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 RecyclerView.Adapter() {            class ViewHolder extends RecyclerView.ViewHolder{                private TextView tv;                public ViewHolder(TextView itemView) {                    super(itemView);                    tv=itemView;                }                public TextView getTv() {                    return tv;                }            }            @Override            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {                return new ViewHolder(new TextView(parent.getContext()));            }            @Override            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {                ViewHolder vh=(ViewHolder)holder;                vh.getTv().setText("hello"+position);            }            @Override            public int getItemCount() {                return 10;            }        });    }}

  

RecyclerView的简单用法