码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView 和 ViewHolder

时间:2017-01-18 01:32:43      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:make   .text   horizon   range   post   ble   位置   nal   文本   

 

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

标签:make   .text   horizon   range   post   ble   位置   nal   文本   

原文地址:http://www.cnblogs.com/Oldz/p/6295128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!