Demo entry 6637284

书架

   

Submitted by anonymous on Aug 31, 2017 at 08:57
Language: Java. Code size: 14.5 kB.

package com.xscm.bookos;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xscm.adapter.CommonAdapter;
import com.xscm.adapter.ViewHolder;
import com.xscm.bean.BkInfo;
import com.xscm.common.AppContext;
import com.xscm.common.AppException;
import com.xscm.common.ReadSetting;
import com.xscm.common.UIHelper;
import com.xscm.common.UserInfoSetting;
import com.xscm.common.Constant.BkConfig;
import com.xscm.common.Constant.BookSetting;
import com.xscm.common.Constant.GUIDE_TYPE;
import com.xscm.common.Constant.System;
import com.xscm.dbase.DBBkbaseInfo;
import com.xscm.dialog.BookStoreMenuDialog;
import com.xscm.dialog.BookToolDialog;
import com.sxcm.reader.R;
import com.xscm.task.OpenReadBookTask;
import com.xscm.task.PersonLoginTask;
import com.xscm.task.RecommendBookTask;
import com.xscm.util.BKStoreUtil;
import com.xscm.util.SDCardUtil;
import com.xscm.util.StringTools;
import com.xscm.widget.BaseActivity;
import com.xscm.widget.CircleBitmapDisplayer;
import com.xscm.widget.ScrollLayout;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.utils.L;
import com.umeng.analytics.MobclickAgent;

@SuppressLint({ "HandlerLeak", "Instantiatable", "NewApi" })
public class BKStoreActivity extends BaseActivity implements OnClickListener {

	private View layout;
	private Context mContext;
	private AppContext mAppContext;
	private DisplayImageOptions avatarOptions;
	private DisplayImageOptions options;
	private ImageView avatarIcon;
	private ImageView bookStoreMenu;

	private LinearLayout batchDeleteBookLayout;

	private L imageLoaderLog;

	private CommonAdapter<BkInfo> gridAdapter;
	private List<BkInfo> mData = new ArrayList<BkInfo>();
	private List<BkInfo> tempList = new ArrayList<BkInfo>();
	private GridView gridView;
	private SwitchClickListener switchClickListener;

	private ReadSetting readSetting;
	private DBBkbaseInfo dbBkbaseInfo;

	private TextView noBookPrompt;
	private ScrollLayout scrollLayout;

	private boolean isShowCb = false;

	@SuppressWarnings({ "static-access", "deprecation" })
	@SuppressLint("InflateParams")
	public BKStoreActivity(Context context, AppContext appContext) {
		LayoutInflater inflater = LayoutInflater.from(context);
		layout = inflater.inflate(R.layout.activity_bkstore, null);
		this.mContext = context;
		this.mAppContext = appContext;
		this.readSetting = ReadSetting.getIntance(mContext);
		dbBkbaseInfo = new DBBkbaseInfo(mContext);
		// 禁止imageloader包打印log
		imageLoaderLog.disableLogging();
		avatarOptions = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.default_avatar)
				// 加载图片时的图片
				.showImageForEmptyUri(R.drawable.default_avatar)
				// 没有图片资源时的默认图片
				// .showImageOnFail(R.drawable.bkstore_add_books)
				.showImageOnFail(R.drawable.default_avatar)
				.displayer(new CircleBitmapDisplayer())
				// 加载失败时的图片
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.default_cover)
				// 加载图片时的图片
				.showImageForEmptyUri(R.drawable.bkstore_add_books)
				// 没有图片资源时的默认图片
				// .showImageOnFail(R.drawable.bkstore_add_books)
				.showImageOnFail(R.drawable.default_cover)
				// .displayer(new ShadowBitmapDisplay())
				// 加载失败时的图片
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		initView();
		refreshData();
		if (mAppContext.isNetworkConnected()) {
			if (!TextUtils.isEmpty(mAppContext.getUserName())
					&& null != mAppContext.getPass()) {
				new PersonLoginTask(mAppContext, handler).execute(mAppContext
						.getPass());
			}
		}

//		// 获取书架推荐书籍
		if (!UserInfoSetting.getIntance(this).getIsShowRecommed()) {
			// 根据男生女获取书籍接口/不传就默认5本
			if (mAppContext.isNetworkConnected()) {
				new RecommendBookTask(mAppContext, handler).execute("3",
						String.valueOf((mContext.getResources().getInteger(
								R.integer.grid_num_columns) * 2) + 1));
			}
		}


		/**
		 * 是否显示书架指导页
		 */
		if (UserInfoSetting.getIntance(mContext).getGuide(
				GUIDE_TYPE.BOOK_STORE, mAppContext.getVersionCode())) {
			showBookStoreGuide();
		}

	}

	public View getLayout() {
		return layout;
	}

	private void initView() {
		avatarIcon = (ImageView) layout.findViewById(R.id.avatar_icon);
		avatarIcon.setOnClickListener(this);
		layout.findViewById(R.id.bookstore_to_shopbook)
				.setOnClickListener(this);

		avatarImage.displayImage(mAppContext.getLoginInfo().getAvatar(),
				avatarIcon, avatarOptions);

		noBookPrompt = (TextView) layout.findViewById(R.id.no_book_prompt);

		scrollLayout = (ScrollLayout) layout.findViewById(R.id.scroll_layout);
		gridView = (GridView) layout.findViewById(R.id.book_store_gridview);
		bookStoreMenu = (ImageView) layout.findViewById(R.id.book_store_menu);
		layout.findViewById(R.id.delete_book).setOnClickListener(this);
		layout.findViewById(R.id.delete_cancel).setOnClickListener(this);
		batchDeleteBookLayout = (LinearLayout) layout
				.findViewById(R.id.batch_delete_layout);
		bookStoreMenu.setOnClickListener(this);

		gridAdapter = new CommonAdapter<BkInfo>(mContext, mData,
				R.layout.item_book_store) {
			@Override
			public void convert(ViewHolder helper, BkInfo item, int postion) {
				if (item.getBkType() == BkConfig.LOCAL_BOOKS) {
					helper.isShowImage(R.id.new_update, false); // 不需要更新
					imageLoader.displayImage("drawable://"
							+ R.drawable.frame_book_shop_cover_local,
							(ImageView) helper.getView(R.id.book_image));
					helper.isShowImage(R.id.full_flag_prompt, false);
					helper.setText(
							R.id.read_progress,
							"已读:"
									+ readSetting.getReadPercent(item
											.getArticleId()) + "%");
					helper.setText(R.id.book_name, item.getArticleName());
					helper.setCheckBox(R.id.book_is_check, item.isCheck,
							isShowCb);
				} else {
					if (item.getLastReadChpOrder() < item.getChapters()
							&& !item.getArticleId().equals("0")) {
						// 目前读的章节排序号小于最新章节排序号 表示需要更新
						helper.isShowImage(R.id.new_update, true);
						helper.setCheckBox(R.id.book_is_check, item.isCheck,
								isShowCb);
					} else {
						helper.isShowImage(R.id.new_update, false);
						helper.setCheckBox(R.id.book_is_check, item.isCheck,
								false);
					}

					if (!item.getArticleId().equals("0")) {
						helper.setFullFlag(R.id.full_flag_prompt,
								item.getFullFlag());
						helper.setText(R.id.read_progress, "已读:"
								+ getReadProgress(item.getArticleId(), postion)
								+ "%");
						helper.setText(R.id.book_name, item.getArticleName());
					} else {
						helper.isShowImage(R.id.full_flag_prompt, false);
						helper.setFullFlag(R.id.full_flag_prompt,
								item.getFullFlag());
						helper.setText(R.id.read_progress, "");
						helper.setText(R.id.book_name, "");
					}
					// 连载
					if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
						((ImageView) helper.getView(R.id.book_image))
								.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
					}
					imageLoader.displayImage(item.getCover(),
							(ImageView) helper.getView(R.id.book_image),
							options);
				}
			}
		};
		gridView.setAdapter(gridAdapter);
		gridView.setOnItemClickListener(new MyOnItemClickListener());
		gridView.setOnItemLongClickListener(new MyOnItemLongClickListener());
		checkAll(isShowCb);

	}

	private View bookStoreGuide;

	private void showBookStoreGuide() {

		if (null != bookStoreGuide) {
			bookStoreGuide.setVisibility(View.VISIBLE);
		}

		ViewStub stub = (ViewStub) layout.findViewById(R.id.bookstore_viewstub);
		bookStoreGuide = stub.inflate();

		LinearLayout layout = (LinearLayout) bookStoreGuide
				.findViewById(R.id.shelf_guide_layout);
		layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				UserInfoSetting.getIntance(mContext).setGuide(
						GUIDE_TYPE.BOOK_STORE, mAppContext.getVersionCode(),
						false);
				bookStoreGuide.setVisibility(View.GONE);

			}
		});
		bookStoreGuide.setVisibility(View.VISIBLE);

	}

	private List<BkInfo> getCheckBook() {
		tempList.clear();
		for (int i = 0; i < mData.size(); i++) {
			if (mData.get(i).isCheck) {
				tempList.add(mData.get(i));
			}
		}
		return tempList;
	}

	/**
	 * 全部选择
	 * 
	 * @param status
	 * @return
	 */
	private void checkAll(boolean status) {
		tempList.clear();
		for (int i = 0; i < mData.size(); i++) {
			mData.get(i).isCheck = false;
		}
		if (status) {
			batchDeleteBookLayout.setVisibility(View.VISIBLE);
			scrollLayout.setVisibility(View.GONE);
		} else {
			batchDeleteBookLayout.setVisibility(View.GONE);
			scrollLayout.setVisibility(View.VISIBLE);
		}
		gridAdapter.refresh();
	}

	/**
	 * 刷新数据(执行删除、添加等操作后执行)
	 */
	public void refreshData() {
		mData.clear();
		try {
			mData = dbBkbaseInfo.findAllBook();
			mData.add(new BkInfo());
		} catch (AppException e) {
			e.printStackTrace();
		}
		if (null == mData || mData.size() == 1) {
			noBookPrompt.setVisibility(View.VISIBLE);
		} else {
			noBookPrompt.setVisibility(View.GONE);
		}
		gridAdapter.setList(mData);
		gridView.setAdapter(gridAdapter);
		gridAdapter.notifyDataSetChanged();
	}

	private class MyOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if (mData.get(position).getArticleId().equals("0")) {
				switchClickListener.switchsBKshop();
			} else {
				if (isShowCb) {
					mData.get(position).isCheck = !mData.get(position).isCheck;
					((CheckBox) view.findViewById(R.id.book_is_check))
							.setChecked(mData.get(position).isCheck);
				} else {
					if (SDCardUtil.checkSDCard()) {
						// 这里去读书
						String articleid = mData.get(position).getArticleId();
						OpenReadBookTask.getInstance(mAppContext, mContext)
								.readBook(
										mData.get(position).getBkType(),
										articleid,
										ReadSetting.getIntance(mContext)
												.getLastReadChp(articleid),
										ReadSetting.getIntance(mContext)
												.getLastReadOrd(articleid),
										mData.get(position).getCover(),
										mData.get(position).getArticleName());
					} else {
						UIHelper.toastSDError(mContext);
					}
				}
			}
		}
	}

	/**
	 * 书籍长按事件
	 * 
	 * @author Wangxu
	 * 
	 */
	private class MyOnItemLongClickListener implements OnItemLongClickListener {
		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view,
				int position, long id) {
			if (mData.size() > position
					&& "0".equals(mData.get(position).getArticleId())) {
				switchClickListener.switchsBKshop();
			} else {
				BookToolDialog.getInstance(mAppContext, mContext, handler,
						mData.get(position)).showView();
			}
			return true;
		}
	}

	/**
	 * 事件回调处理。
	 */
	private Handler handler = new Handler() {
		public void dispatchMessage(Message msg) {
			switch (msg.what) {
			case System.OK:
				refreshData();
				break;

			case BookSetting.BATCH_DELETE_BOOK:
				isShowCb = true;
				checkAll(isShowCb);
				break;

			case BookSetting.CANCEL_BATCH_DELETE_BOOK:
				isShowCb = false;
				checkAll(isShowCb);
				break;
			}
		};
	};




	public void setSwitchClickListener(SwitchClickListener listener) {
		this.switchClickListener = listener;
	}

	/**
	 * 获取阅读进度
	 * 
	 * @param articleId
	 * @param positon
	 * @return
	 */
	private String getReadProgress(String articleId, int positon) {
		int current = readSetting.getLastReadOrd(articleId);
		double percent = (double) current
				/ Integer.valueOf(mData.get(positon).getChapters());
		DecimalFormat df = new DecimalFormat("#");
		String point = df.format(percent * 100);
		if (!StringTools.isNum(point)) {
			point = "0";
		}
		if (Integer.parseInt(point) > 100) {
			return "100";
		}
		return point;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.book_store_menu:
			BookStoreMenuDialog.getInstance(mAppContext, mContext, handler,
					bookStoreMenu, scrollLayout).showView();
			break;
		case R.id.avatar_icon:
			switchClickListener.switchsPerson();
			break;
		case R.id.delete_book:
			tempList = getCheckBook();
			if (null != tempList && tempList.size() == 0) {
				UIHelper.ToastMessage(mContext, R.string.please_select_book);
				return;
			}
			for (int i = 0; i < getCheckBook().size(); i++) {
				BKStoreUtil.getInstance(mAppContext, mContext, handler)
						.deleteCurBook(tempList.get(i).getArticleId(), false);
			}

			isShowCb = false;
			batchDeleteBookLayout.setVisibility(View.GONE);
			scrollLayout.setVisibility(View.VISIBLE);
			gridAdapter.refresh();
			break;
		case R.id.bookstore_to_shopbook:
			switchClickListener.switchsBKshop();
			break;
		case R.id.delete_cancel:
			handler.sendEmptyMessage(BookSetting.CANCEL_BATCH_DELETE_BOOK);
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(BKStoreActivity.class.getSimpleName());
		MobclickAgent.onResume(BKStoreActivity.this);

	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(BKStoreActivity.class.getSimpleName());
		MobclickAgent.onPause(BKStoreActivity.this);
	}

	public void refreshPersonAvatar() {
		avatarImage.displayImage(mAppContext.getLoginInfo().getAvatar(),
				avatarIcon, avatarOptions);
	}

}

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).