286 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			286 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| package com.novelbook.android;
 | |
| 
 | |
| import android.content.Context;
 | |
| import android.content.Intent;
 | |
| import android.os.Build;
 | |
| import android.support.v4.app.Fragment;
 | |
| import android.support.v7.widget.LinearLayoutManager;
 | |
| import android.support.v7.widget.RecyclerView;
 | |
| import android.text.TextUtils;
 | |
| import android.util.Log;
 | |
| import android.view.LayoutInflater;
 | |
| import android.view.TextureView;
 | |
| import android.view.View;
 | |
| import android.view.ViewGroup;
 | |
| import android.view.WindowManager;
 | |
| import android.widget.TextView;
 | |
| 
 | |
| import com.novelbook.android.bean.Site;
 | |
| import com.novelbook.android.db.Novel;
 | |
|  
 | |
| import com.novelbook.android.utils.PageFactory;
 | |
| 
 | |
| import java.util.ArrayList;
 | |
| import java.util.Arrays;
 | |
| import java.util.List;
 | |
| 
 | |
| import butterknife.BindView;
 | |
| import butterknife.ButterKnife;
 | |
| 
 | |
| public class Activity_ChgSource extends Activity_base {
 | |
| 
 | |
| 
 | |
|     public static final String TAG = Activity_ChgSource.class.getSimpleName();
 | |
| 
 | |
|     @BindView(R.id.recycleView)
 | |
|     RecyclerView recyclerView;
 | |
| 
 | |
|     private ArrayList<Fragment> mFragments;
 | |
|     String chaptTitle;
 | |
|     int chaptId;
 | |
|     String domain;
 | |
|    // String name;
 | |
|     public final static String EXTR_TITLE="title";
 | |
|     public final static String EXTR_ID="id";
 | |
|     public final static String EXTR_SITE="site";
 | |
|     //public static final String EXTR_NAME ="" ;
 | |
|     public final static String EXTR_cate ="cate";
 | |
|     PageFactory pageFactory;
 | |
|     boolean isFromCate;
 | |
|     List<Site> mSites;
 | |
|     SiteAdapter mAdapter;
 | |
|     @Override
 | |
|     public int getLayoutRes() {
 | |
|         return R.layout.activity_chgsource;
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     protected void initViews() {
 | |
| 
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     protected void setTitle() {
 | |
| 
 | |
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
 | |
|             WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
 | |
|             localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
 | |
| 
 | |
|         }
 | |
|         chaptTitle = getIntent().getStringExtra(EXTR_TITLE);
 | |
| 
 | |
|         chaptId = getIntent().getIntExtra(EXTR_ID,1);
 | |
| 
 | |
|         domain =   getIntent().getStringExtra(EXTR_SITE);
 | |
| 
 | |
|         isFromCate =getIntent().getBooleanExtra(EXTR_cate,false);
 | |
| 
 | |
|      //   name =   getIntent().getStringExtra(EXTR_NAME);
 | |
|         this.setTitle(chaptTitle);
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     protected void initData() {
 | |
| 
 | |
|         showProgressDialog(false,"正在加载源网站列表");
 | |
|         mSites = new ArrayList<Site>();
 | |
|         pageFactory =PageFactory.getInstance(getApplicationContext());
 | |
|         new Thread() {
 | |
|             @Override
 | |
|             public void run() {
 | |
|                 super.run();
 | |
|                 while(pageFactory.getNovelSites()==null){
 | |
|                     try {
 | |
|                         Thread.sleep(100);
 | |
|                     } catch (InterruptedException e) {
 | |
|                         e.printStackTrace();
 | |
|                     }
 | |
|                 }
 | |
|                 ArrayList<Site>   tmp   = new ArrayList<Site>(Arrays.asList(pageFactory.getNovelSites().getSites()));
 | |
| 
 | |
|                 for(Site site : tmp){
 | |
|                     if(!TextUtils.isEmpty(site.getMuluUrl())){
 | |
|                         mSites.add(site);
 | |
|                     }
 | |
|                 }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|                 handler.sendEmptyMessage(1);
 | |
|             }}.start();;
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|           mAdapter = new SiteAdapter(this, mSites, R.layout.recycle_list_one_item, new OnItemClickListener() {
 | |
| 
 | |
|             @Override
 | |
|             public void onItemClick(View view, int position) {
 | |
| 
 | |
|                  Site site = mSites.get(position);
 | |
|                 Log.d(TAG, String.format("changing Source:target site name %s, site domain %s  " ,site.getName(), site.getDomain()));
 | |
| 
 | |
|                 if(isFromCate){
 | |
|                     pageFactory.changeSourceForCate(site.getName(),site.getDomain());
 | |
|                     Intent intent = new Intent(Activity_ChgSource.this, MarkActivity.class);
 | |
|                     //    intent.putExtra(Activity_ChgSource.EXTR_NAME, pageFactory.getSite().getName());
 | |
|                     startActivity(intent);
 | |
|                 }else{
 | |
|                     pageFactory.changeSource(site.getName(),site.getDomain(),chaptId,chaptTitle);
 | |
|                 }
 | |
|                   finish();
 | |
|             }
 | |
|         });
 | |
|         recyclerView.setLayoutManager(new LinearLayoutManager(this));
 | |
|         recyclerView.setAdapter(mAdapter);
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     public void fillData() {
 | |
|         mAdapter.setData(mSites);
 | |
|     }
 | |
| 
 | |
|     interface OnItemClickListener
 | |
|     {
 | |
|         void onItemClick(View view, int position);
 | |
| 
 | |
|     }
 | |
| 
 | |
| 
 | |
|     class SiteAdapter extends RecyclerView.Adapter<SiteAdapter.SiteViewHolder> {
 | |
|         private final int EMPTY_VIEW = 1;
 | |
|         private final int PROGRESS_VIEW = 2;
 | |
|         private final int IMAGE_VIEW = 3;
 | |
| 
 | |
|         private Context context;
 | |
|         private List<Site> mDatas  ;
 | |
|         private OnItemClickListener mOnItemClickListener;
 | |
|         private int listItemID;
 | |
| 
 | |
|         private List<Novel> mSites;
 | |
| 
 | |
|         public SiteAdapter(Context context, List<Site> mSites, int listItemID, OnItemClickListener clickLitener) {
 | |
|             this.context = context;
 | |
|             this.mDatas = mSites;
 | |
|             this.mOnItemClickListener = clickLitener;
 | |
|             this.listItemID = listItemID;
 | |
|         }
 | |
| 
 | |
| 
 | |
| 
 | |
|         public SiteAdapter(Context context, OnItemClickListener clickLitener) {
 | |
|             this.context = context;
 | |
|             this.mOnItemClickListener = clickLitener;
 | |
| 
 | |
|         }
 | |
| 
 | |
|         public void setListener(OnItemClickListener clickLitener){
 | |
|             this.mOnItemClickListener = clickLitener;
 | |
| 
 | |
|         }
 | |
| 
 | |
| 
 | |
|         @Override
 | |
|         public int getItemViewType(int position) {
 | |
|             if (mDatas.size() == 0) {
 | |
|                 return EMPTY_VIEW;
 | |
|             } else if (mDatas.get(position) == null) {
 | |
|                 return PROGRESS_VIEW;
 | |
|             } else {
 | |
|                 return super.getItemViewType(position);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         @Override
 | |
|         public  SiteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 | |
|           SiteAdapter.SiteViewHolder holder = new  SiteViewHolder(LayoutInflater.from(
 | |
|                     context).inflate(listItemID, parent,
 | |
|                     false));
 | |
|             return holder;
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public void setParameters(List<Site> mDatas, int listItemID) {
 | |
|             this.mDatas = mDatas;
 | |
|             this.listItemID = listItemID;
 | |
|         }
 | |
| 
 | |
|         public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
 | |
|             this.mOnItemClickListener = mOnItemClickLitener;
 | |
|         }
 | |
| 
 | |
|         @Override
 | |
|         public void onBindViewHolder( SiteViewHolder holder, int position) {
 | |
| 
 | |
|             String title =mDatas.get(position).getName();
 | |
|               if(TextUtils.isEmpty(title)) {
 | |
|                   title = mDatas.get(position).getDomain();
 | |
|               }
 | |
|          //  title += mDatas.get(position).getDomain();
 | |
|             Log.d(TAG, String.format("onBindViewHolder: domain is '%s', title is '%s'",domain ,title));
 | |
|             if(mDatas.get(position).getDomain().equals(domain)){
 | |
|             //     title +=" (当前源)";
 | |
|                 holder.tvTitle.setTextColor(getResources().getColor( R.color.colorPrimaryDark));
 | |
|             }else{
 | |
|                 holder.tvTitle.setTextColor(getResources().getColor( R.color.blacktitle));
 | |
|             }
 | |
| 
 | |
|             holder.tvTitle.setText(title); //.getName()
 | |
| 
 | |
|             // 如果设置了回调,则设置点击事件
 | |
|             if (mOnItemClickListener != null) {
 | |
|                 holder.itemView.setOnClickListener(new View.OnClickListener() {
 | |
|                     @Override
 | |
|                     public void onClick(View v) {
 | |
|                         int pos = holder.getLayoutPosition();
 | |
|                         mOnItemClickListener.onItemClick(holder.itemView, pos);
 | |
|                     }
 | |
|                 });
 | |
| 
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         @Override
 | |
|         public int getItemCount() {
 | |
|             return mDatas.size();
 | |
|         }
 | |
| 
 | |
|         public void addData(int position) {
 | |
|             //   mDatas.add(position, "Insert One");
 | |
|             notifyItemInserted(position);
 | |
|         }
 | |
| 
 | |
|         public void removeData(int position) {
 | |
|             mDatas.remove(position);
 | |
|             notifyItemRemoved(position);
 | |
|         }
 | |
| 
 | |
|         public void setData(List<Site> mSites) {
 | |
|             mDatas = mSites;
 | |
|             notifyDataSetChanged();
 | |
|         }
 | |
| 
 | |
|         public   class SiteViewHolder extends RecyclerView.ViewHolder {
 | |
| 
 | |
|             @BindView(R.id.tvText)
 | |
|             TextView tvTitle;
 | |
|             public SiteViewHolder(View view) {
 | |
|                 super(view);
 | |
|                 ButterKnife.bind(this, view);
 | |
|                 //tvTitle = (TextView) view.findViewById(R.id.title);
 | |
|                 // tvAuthor = (TextView) view.findViewById(R.id.author);
 | |
| 
 | |
|             }
 | |
| 
 | |
|         }
 | |
| 
 | |
|     }
 | |
| 
 | |
| }
 |