com.kesar.demo.LinearFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.kesar.demo.LinearFragment.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.LinearLayoutManager;
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;

/**
 * linear
 * Created by kesar on 2016/11/23.
 */

public class LinearFragment extends Fragment {

    SwipeRefreshLayout mRefreshLayout;
    RecyclerView mRecyclerView;
    LinearAdapter mAdapter;

    public static LinearFragment newInstance() {

        Bundle args = new Bundle();

        LinearFragment fragment = new LinearFragment();
        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_linear, null);

        mRecyclerView = (RecyclerView) layout.findViewById(R.id.recyclerView);
        mRefreshLayout = (SwipeRefreshLayout) layout.findViewById(R.id.swipeRefreshLayout);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(inflater.getContext()));
        mAdapter = new LinearAdapter();
        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();
            }
        });
        return layout;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadData();
    }

    private int pageNo = 0;
    private int pageNum = 10;

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

    private class LinearAdapter 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_linear, parent, false));
        }
    }
}