首页 > 代码库 > RecyclerView 和 ViewHolder
RecyclerView 和 ViewHolder
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setActionBar(toolbar); final SwipeRefreshLayout srl = (SwipeRefreshLayout) findViewById(R.id.srl); srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { srl.postDelayed(new Runnable() { @Override public void run() { srl.setRefreshing(false); } },3000); } }); srl.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview); StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,LinearLayoutManager.VERTICAL); //layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(layoutManager); mRecyclerView.setAdapter(new Myadapter()); } private class Myadapter extends RecyclerView.Adapter<Myadapter.ListHoldewer>{ //设置条目 @Override public ListHoldewer onCreateViewHolder(ViewGroup parent, int viewType) { //TextView textview = new TextView(MainActivity.this); View view = View.inflate(MainActivity.this, R.layout.aaaa, null); return new ListHoldewer(view); } //获取条目数据 @Override public void onBindViewHolder(ListHoldewer holder, int position) { holder.setData(position); } //获取条目个数 @Override public int getItemCount() { return 100; } class ListHoldewer extends RecyclerView.ViewHolder{ TextView textview1; TextView textview2; View mitemView; public ListHoldewer(View itemView) { super(itemView); mitemView = itemView; textview1 = (TextView) itemView.findViewById(R.id.text1); textview2 = (TextView) itemView.findViewById(R.id.text2); } public void setData(int position) { textview1.setText("当前的位置:"+position); textview2.setText("当前显示的文本"); mitemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,textview1.getText(),Toast.LENGTH_LONG).show(); } }); } } } }
RecyclerView 和 ViewHolder
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。