me.zhang.accountbook.ui.LicenseFragment.java Source code

Java tutorial

Introduction

Here is the source code for me.zhang.accountbook.ui.LicenseFragment.java

Source

package me.zhang.accountbook.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;

import in.srain.cube.util.LocalDisplay;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.header.StoreHouseHeader;
import me.zhang.accountbook.R;
import me.zhang.accountbook.callback.SwipeDismissListViewTouchListener;

/**
 * Created by Zhang on 2/14/2015.
 */
public class LicenseFragment extends Fragment {

    private PtrFrameLayout mPtrFrame;
    public static boolean mCanDoRefresh;
    private final ArrayList<String> mObjects = new ArrayList<>();
    private ArrayAdapter<String> adapter;
    private final Handler mHandler;

    public LicenseFragment() {
        // , ??
        Handler.Callback mCallback = new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 0x10) {
                    adapter.clear();
                    adapter.addAll(getResources().getStringArray(R.array.licenses));
                    return true;
                }
                return false;
            }
        };
        mHandler = new Handler(mCallback);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_license, container, false);
        initUI(rootView);
        initPtr(rootView);
        return rootView;
    }

    private void initUI(View rootView) {
        ListView licenseList = (ListView) rootView.findViewById(R.id.license_list);
        // String[]?ArrayList?
        Collections.addAll(mObjects, getResources().getStringArray(R.array.licenses));
        adapter = new ArrayAdapter<>(getActivity(), R.layout.list_item_license, mObjects);
        licenseList.setAdapter(adapter);
        // ??
        licenseList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO ??, ?
            }
        });
        SwipeDismissListViewTouchListener touchListener = new SwipeDismissListViewTouchListener(licenseList,
                new SwipeDismissListViewTouchListener.DismissCallbacks() {
                    @Override
                    public boolean canDismiss(int position) {
                        return true;
                    }

                    @Override
                    public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                        for (int position : reverseSortedPositions) {
                            adapter.remove(adapter.getItem(position));
                        }
                    }
                });
        licenseList.setOnTouchListener(touchListener);
        licenseList.setOnScrollListener(touchListener.makeScrollListener());
    }

    private void initPtr(View rootView) {
        mCanDoRefresh = true;
        mPtrFrame = (PtrFrameLayout) rootView.findViewById(R.id.ptr_frame);

        StoreHouseHeader header = new StoreHouseHeader(getActivity());
        header.setTextColor(getResources().getColor(R.color.myPrimaryColor));
        header.setPadding(0, LocalDisplay.dp2px(20), 0, LocalDisplay.dp2px(20));
        header.initWithString("Loading...");

        mPtrFrame.setDurationToCloseHeader(1500);
        mPtrFrame.setHeaderView(header);
        mPtrFrame.addPtrUIHandler(header);

        mPtrFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(2 * 1000);
                        mPtrFrame.refreshComplete();
                        mHandler.sendEmptyMessage(0x10);
                    }
                }).start();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header) && mCanDoRefresh;
            }
        });
    }

}