com.kesar.demo.GridFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.kesar.demo.GridFragment.java

Source

/*
 * Copyright [2016] [Clayman Twinkle]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kesar.demo;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kesar.lazy.recyclerview.CommonViewHolder;
import com.kesar.lazy.recyclerview.ListLazyAdapter;
import com.kesar.lazy.recyclerview.OnLoadingMoreScrollListener;

import static android.support.v7.widget.DividerItemDecoration.VERTICAL;

/**
 * gird
 * Created by kesar on 2016/11/23.
 */
public class GridFragment extends Fragment {

    SwipeRefreshLayout mRefreshLayout;
    RecyclerView mRecyclerView;

    GridAdapter mAdapter;

    public static GridFragment newInstance() {

        Bundle args = new Bundle();

        GridFragment fragment = new GridFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_grid, null);
        mRecyclerView = (RecyclerView) layout.findViewById(R.id.recyclerView);
        mRefreshLayout = (SwipeRefreshLayout) layout.findViewById(R.id.swipeRefreshLayout);

        mRecyclerView.setLayoutManager(new GridLayoutManager(inflater.getContext(), 3));

        mAdapter = new GridAdapter();
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), VERTICAL) {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.top = DensityUtils.dip2px(view.getContext(), 8);
                outRect.bottom = DensityUtils.dip2px(view.getContext(), 8);
                outRect.left = DensityUtils.dip2px(view.getContext(), 16);
                outRect.right = DensityUtils.dip2px(view.getContext(), 16);
            }
        });

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 0;
                mAdapter.clearAllItemsAndRefresh();
                loadData();
                mRefreshLayout.setRefreshing(false);
            }
        });
        mRecyclerView.addOnScrollListener(new OnLoadingMoreScrollListener() {
            @Override
            public void onLoadingMore() {
                loadData();
            }
        });
        loadData();
        return layout;
    }

    private int pageNo = 0;
    private int pageNum = 30;

    private void loadData() {
        for (int i = 0; i < pageNum; i++) {
            mAdapter.addItemAndRefresh(String.valueOf(++pageNo));
        }
    }

    private class GridAdapter extends ListLazyAdapter<String, CommonViewHolder> {

        @Override
        public void onBindViewHolder(CommonViewHolder holder, String data, int position) {
            TextView textView = holder.getView(R.id.text);
            textView.setText("?" + data);
        }

        @Override
        public CommonViewHolder onCreateViewHolder(ViewGroup parent, Context context, int viewType) {
            return new CommonViewHolder(
                    LayoutInflater.from(context).inflate(R.layout.list_item_grid, parent, false));
        }
    }
}