天天看點

RecycleView的學習使用

RecyclerView出來很長時間了,一直沒有去嘗試學習,想想很是忏愧!今天整理了一點關于RecyclerView的簡單使用方法,算作學習筆記了。

RecyclerView的使用,有三個是必須的Adapter,ViewHolder,LayoutManager,而ItemDecoration和ItemAnimator是可選的。

以下是簡單的代碼示例:

首先是VIewHoder的:

class TestRecycleHolder extends RecyclerView.ViewHolder{
        protected TextView textView;
        public TestRecycleHolder(View itemView) {
            super(itemView);
            textView= (TextView)   itemView.findViewById(R.id.item_text);

        }

    }
           

該類必需繼承extends RecyclerView.ViewHolder。

擴充卡Adapter部分:

class RecycleAdapter extends RecyclerView.Adapter<TestRecycleHolder>
    {

        @Override
        public long getItemId(int position) {
            return super.getItemId(position);
        }


        @Override
        public TestRecycleHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View view= LayoutInflater.from(context).inflate(R.layout.item_view,null);
            return new TestRecycleHolder(view);
        }

        @Override
        public void onBindViewHolder(TestRecycleHolder holder, int position)
        {
           final String name=dataArrayList.get(position);
            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "You clickd TextView at index of "+name,
                                            Toast.LENGTH_SHORT).show();
                }
            });
            holder.textView.setText(name);
        }

        @Override
        public int getItemCount()
        {
            return dataArrayList.size();
        }


    }
           

該類必需繼承RecyclerView.Adapter類。

至于LayoutManager,可以使用系統提供的LinearLayoutManager類,代碼如下:

LinearLayoutManager layoutManager = new LinearLayoutManager(this,
                            LinearLayout.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
           

也可以自己繼承RecyclerView.LayoutManager的類,實作該類

class MyDefineLayoutManager extends RecyclerView.LayoutManager
    {
        private final MyDefineLayoutManager myff=this;

        @Override
        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)
        {
            super.onLayoutChildren(recycler, state);

            if (state.isPreLayout())
            {
                return;
            }
            if (getItemCount() == )
                return;

        }

        @Override
        public RecyclerView.LayoutParams generateDefaultLayoutParams()
        {
            return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        }
    }
           

如果想知道如何寫自己的LayoutManager,請參閱http://wiresareobsolete.com/2014/09/building-a-recyclerview-layoutmanager-part-1/文章

ItemDecoration,是對Item進行裝飾的,比如給Item加個邊距等。比如,下面的代碼僅僅加了個上邊距:

class SpacesItemDecoration extends RecyclerView.ItemDecoration{
        public SpacesItemDecoration() {
            super();
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
        }

        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDrawOver(c, parent, state);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.top=;
        }
    }
           

文章寫得比較粗糙,僅做為一個筆記參考。敬請見諒

參考文獻:http://www.tuicool.com/articles/URvIB32

https://www.javacodegeeks.com/2014/11/a-guide-to-android-recyclerview-and-cardview.html

http://www.cnblogs.com/littlepanpc/p/3859983.html

https://github.com/vilyever/HowToCustomLayoutManager

http://www.jianshu.com/p/de31cce8e62f

http://wiresareobsolete.com/2014/09/building-a-recyclerview-layoutmanager-part-1/

RecycleView的學習使用

http://blog.csdn.net/wyyl1/article/details/48112141