com.umeng.common.ui.dialogs.PickerDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.umeng.common.ui.dialogs.PickerDialog.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Umeng, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.umeng.common.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.location.Location;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.umeng.comm.core.CommunitySDK;
import com.umeng.comm.core.impl.CommunityFactory;
import com.umeng.comm.core.listeners.Listeners.FetchListener;
import com.umeng.comm.core.utils.Log;
import com.umeng.comm.core.utils.ResFinder;
import com.umeng.common.ui.adapters.CommonAdapter;
import com.umeng.common.ui.widgets.RefreshLayout;
import com.umeng.common.ui.widgets.RefreshLvLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * ?@??Dialog
 */
public abstract class PickerDialog<E> extends Dialog implements OnClickListener {

    /**
     * ???ListView
     */
    protected ListView mListView;

    /**
     * 
     */
    protected TextView mTitleTextView;

    /**
     * ??
     */
    protected Location mLocation = null;

    /**
     * ???
     */
    protected FetchListener<E> mDataListener;

    protected CommonAdapter<E, ?> mAdapter = null;

    /**
     * DialogRoot View
     */
    protected View mRootView;
    protected LinearLayout topicButtonGroup;
    /**
     * ?
     */
    protected List<Integer> mSelectedIndex = new ArrayList<Integer>();

    protected RefreshLvLayout mRefreshLvLayout;
    protected CommunitySDK mSdkImpl;
    protected E mSelectedItem;

    /**
     * @param context
     */
    public PickerDialog(Context context) {
        this(context, 0);
    }

    public PickerDialog(Context context, int theme) {
        super(context, theme);
        mSdkImpl = CommunityFactory.getCommSDK(context);
    }

    /**
     * ???, ?????
     * 
     * @param listener
     */
    public void setDataListener(FetchListener<E> listener) {
        mDataListener = listener;
    }

    /**
     * Parse the dialog's content view and fill datas, and so on.
     * 
     * @return the dialog's content view
     */
    protected View createContentView() {
        int layout = ResFinder.getLayout("umeng_comm_at_friends_layout");
        int listViewResId = ResFinder.getId("umeng_comm_friend_listview");
        int searchTvResId = ResFinder.getId("search_tv");
        int searchlasteTvResId = ResFinder.getId("search_lastes_btn");
        int backBtnResId = ResFinder.getId("search_back_btn");
        int okBtnResId = ResFinder.getId("search_ok_btn");
        int refreshLayoutResId = ResFinder.getId("umeng_comm_at_friend_listview");
        int topicselect = ResFinder.getId("topic_select");
        mRootView = LayoutInflater.from(getContext()).inflate(layout, null, false);
        topicButtonGroup = (LinearLayout) mRootView.findViewById(topicselect);

        mRefreshLvLayout = (RefreshLvLayout) mRootView.findViewById(refreshLayoutResId);
        mRefreshLvLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh() {
                loadDataFromServer();
            }
        });

        mRefreshLvLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {

            @Override
            public void onLoad() {
                loadMore();
            }
        });

        // listview
        // mListView = (ListView) mRootView
        // .findViewById(listViewResId);
        mListView = mRefreshLvLayout.findRefreshViewById(listViewResId);
        // mListView.requestFocus();
        // 
        mTitleTextView = (TextView) mRootView.findViewById(searchTvResId);
        mRootView.findViewById(searchlasteTvResId).setVisibility(View.GONE);
        setupAdater();
        setupLvOnItemClickListener();
        initSearchEdit();

        mRootView.findViewById(backBtnResId).setOnClickListener(this);
        mRootView.findViewById(okBtnResId).setOnClickListener(this);
        mRootView.findViewById(okBtnResId).setVisibility(View.GONE);

        return mRootView;
    }

    /**
     * server?</br>
     */
    public abstract void loadDataFromServer();

    /**
     * server?</br>
     */
    public abstract void loadMore();

    /**
     * 
     */
    private void initSearchEdit() {
        int searchEditResId = ResFinder.getId("umeng_comm_search_edittext");
        mRootView.findViewById(searchEditResId).setVisibility(View.GONE);
    }

    protected void pickItemAtPosition(int position) {
        if (mDataListener != null) {
            Log.d("item", "selected postion:" + position);
            mSelectedItem = mAdapter.getItem(position);
            mDataListener.onComplete(mSelectedItem);
        }
        this.dismiss();
    }

    /**
     * ?
     */
    protected abstract void setupAdater();

    /**
     * ListViewitem ?
     */
    protected abstract void setupLvOnItemClickListener();

    /*
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        if (mDataListener != null) {
            mDataListener.onComplete(mSelectedItem);
        }
        this.dismiss();
    }

}