191 lines
6.1 KiB
Java
191 lines
6.1 KiB
Java
|
package com.novelbook.android.Fragments;
|
||
|
|
||
|
|
||
|
import android.app.AlertDialog;
|
||
|
import android.content.Context;
|
||
|
import android.content.DialogInterface;
|
||
|
import android.content.Intent;
|
||
|
import android.content.res.Resources;
|
||
|
import android.net.Uri;
|
||
|
import android.os.Bundle;
|
||
|
import android.support.annotation.Nullable;
|
||
|
import android.support.design.widget.TabLayout;
|
||
|
import android.support.v4.app.Fragment;
|
||
|
import android.support.v4.app.FragmentActivity;
|
||
|
import android.support.v4.app.FragmentManager;
|
||
|
import android.support.v4.app.FragmentTransaction;
|
||
|
import android.support.v7.widget.RecyclerView;
|
||
|
import android.text.TextUtils;
|
||
|
import android.util.Log;
|
||
|
import android.util.TypedValue;
|
||
|
import android.view.LayoutInflater;
|
||
|
import android.view.View;
|
||
|
import android.view.ViewGroup;
|
||
|
import android.widget.ImageView;
|
||
|
import android.widget.LinearLayout;
|
||
|
import android.widget.TextView;
|
||
|
import android.widget.Toast;
|
||
|
|
||
|
import com.novelbook.android.Activity_paihangbang;
|
||
|
import com.novelbook.android.Activity_shudan;
|
||
|
import com.novelbook.android.BookActivity;
|
||
|
import com.novelbook.android.R;
|
||
|
import com.novelbook.android.ReadActivity;
|
||
|
import com.novelbook.android.adapter.BookListAdapter;
|
||
|
import com.novelbook.android.db.Book;
|
||
|
import com.novelbook.android.utils.OnItemClickListener;
|
||
|
|
||
|
import org.litepal.LitePal;
|
||
|
|
||
|
import java.io.File;
|
||
|
import java.lang.reflect.Field;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
|
import butterknife.BindView;
|
||
|
import butterknife.ButterKnife;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* A simple {@link Fragment} subclass.
|
||
|
*/
|
||
|
public abstract class BasicFragment extends Fragment {
|
||
|
|
||
|
public static String TAG = "need to be replaced";
|
||
|
private View rootView;
|
||
|
protected abstract int getLayoutRes();
|
||
|
protected abstract void initData();
|
||
|
protected void initListener(){}
|
||
|
protected abstract void initViews();
|
||
|
// Main2Activity activity;
|
||
|
FragmentActivity activity;
|
||
|
|
||
|
public BasicFragment() {
|
||
|
// Required empty public constructor
|
||
|
}
|
||
|
@Override
|
||
|
public void onCreate(Bundle savedInstanceState) {
|
||
|
super.onCreate(savedInstanceState);
|
||
|
setFTag();
|
||
|
// Log.d(TAG,"fragment onCreate ");
|
||
|
// activity =(Main2Activity)getActivity();
|
||
|
activity = getActivity();
|
||
|
initData();
|
||
|
|
||
|
// QLog.getLogger().e(dTAG,TAG+" "+ "onCreate");
|
||
|
// mediumFontSize = activity.getMiddleSizeFont();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||
|
Bundle savedInstanceState) {
|
||
|
Log.d(TAG,"onCreateView");
|
||
|
View view = inflater.inflate(getLayoutRes(), container, false);
|
||
|
rootView = view;
|
||
|
// 初始化View注入
|
||
|
ButterKnife.bind(this,view);
|
||
|
|
||
|
initListener();
|
||
|
|
||
|
initViews();
|
||
|
return view;
|
||
|
// Inflate the layout for this fragment
|
||
|
|
||
|
}
|
||
|
|
||
|
public String getCurrentTag() {
|
||
|
setFTag();
|
||
|
return TAG;
|
||
|
}
|
||
|
|
||
|
|
||
|
public abstract void setFTag();
|
||
|
|
||
|
|
||
|
|
||
|
//--------------------------------------------------------------book list adapter------------------------------------------------
|
||
|
/* interface mOnItemClickListener
|
||
|
{
|
||
|
void onItemClick(View view, int position);
|
||
|
void onItemLongClick(View view , int position);
|
||
|
void onLinearOutClick(View view,int bookId);
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
protected List<Book> initData(List<Book> mDatas, char x)
|
||
|
{
|
||
|
mDatas = new ArrayList<Book>();
|
||
|
for (int i = 'A'; i < x; i++)
|
||
|
{
|
||
|
Book bk = new Book();
|
||
|
bk.setAuthor("金庸");
|
||
|
bk.setBookname("射雕英雄传" +(char)i);
|
||
|
bk.setCate("武侠");
|
||
|
bk.setDesc("南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事");
|
||
|
mDatas.add(bk);
|
||
|
}
|
||
|
return mDatas;
|
||
|
}
|
||
|
|
||
|
void showBookDetail(int id) {
|
||
|
Intent intent = new Intent(activity, BookActivity.class);
|
||
|
intent.putExtra("BOOKNAME",id);
|
||
|
startActivity(intent);
|
||
|
}
|
||
|
|
||
|
|
||
|
void openBook(Book book, BookListAdapter mAdapter) {
|
||
|
|
||
|
//从新从数据库抓取该书的最新阅读进度
|
||
|
|
||
|
Book book1 = (Book)LitePal.find(Book.class,book.getId());
|
||
|
if(book1!=null) { //读取本地最新进度
|
||
|
book.setBegin(book1.getBegin());
|
||
|
book.setBiginChapt(book1.getBiginChapt());
|
||
|
}
|
||
|
Toast.makeText(activity, book.getBookname() + "加载", Toast.LENGTH_SHORT).show();
|
||
|
|
||
|
final String path = book.getBookpath();
|
||
|
if(null ==path) {
|
||
|
List<Book> bookLists = LitePal.findAll(Book.class);
|
||
|
if(bookLists.size()>0) {
|
||
|
openBook(bookLists.get(0),null);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
File file = new File(path);
|
||
|
if (!file.exists()){
|
||
|
new AlertDialog.Builder(activity)
|
||
|
.setTitle(activity.getString(R.string.app_name))
|
||
|
.setMessage(path + "文件不存在,是否删除该书本?")
|
||
|
.setPositiveButton("删除", new DialogInterface.OnClickListener() {
|
||
|
@Override
|
||
|
public void onClick(DialogInterface dialog, int which) {
|
||
|
LitePal.deleteAll(Book.class, "bookpath = ?", path);
|
||
|
if(mAdapter!=null) {mAdapter.setBookList( LitePal.findAll(Book.class));}
|
||
|
}
|
||
|
}).setCancelable(true).show();
|
||
|
return;
|
||
|
}
|
||
|
ReadActivity.openBook(book ,activity);
|
||
|
|
||
|
}
|
||
|
void showShudanDetail(int shuandanId){
|
||
|
Intent intent = new Intent(activity, Activity_shudan.class);
|
||
|
intent.putExtra("NAME",shuandanId);
|
||
|
startActivity(intent);
|
||
|
|
||
|
}
|
||
|
|
||
|
//-------------------------------------------------------------------------------------book list adapter end--------------------------------------------------
|
||
|
|
||
|
void showPaihangbang(String type) { //show paihangbang activity
|
||
|
|
||
|
Intent intent = new Intent(activity, Activity_paihangbang.class);
|
||
|
intent.putExtra("BANGNAME",type);
|
||
|
startActivity(intent);
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|