各个
该App已经上传到百度应用市场:http://shouji.baidu.com/soft/item?docid=8928185&from=as&f=search_app_it%E8%B5%84%E8%AE%AF%40list_1_image%402%40header_all_input
有兴趣的可以下载看看。接下来我会公布源代码,不过该App并没有使用代码混淆,所以可以通过反编译清楚的看到源码。
Github地址 :
https://github.com/linhaosheng/CSDN
以下是 MainActivity的源码
public class MainActivity extends BaseActivityImpl implements View.OnClickListener{ private TabPageIndicator tabPageIndicator; private ViewPager mViewPage; private FragmentPagerAdapter mAdapter; private int select = 0; private CTOActivity cto=null; private CSDNActivity csdn=null; private BlogHouseActivity blog=null; private ITEYEActivity iteye=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle bundle = getIntent().getBundleExtra("IT_select"); int select_Num = bundle.getInt("select"); select = select_Num; switch (select) { case 1: setContentView(R.layout.csdnmain); csdn=new CSDNActivity(this,MainActivity.this); csdn.initTabLine(); initCSDN(); csdn.setViewPage(mViewPage); csdn.initViewPage(); csdn.setOnClickListener(this); csdn.textListener(); getCSDNApplication().addActivity(this); break; case 2: setContentView(R.layout.ctomain); cto=new CTOActivity(this,MainActivity.this); cto.initTabLine(); init_51CTO(); cto.setViewPage(mViewPage); cto.initViewPage(); cto.setOnClickListener(this); cto.textListener(); getCSDNApplication().addActivity(this); break; case 3: setContentView(R.layout.bloghousemain); blog=new BlogHouseActivity(this,MainActivity.this); blog.initTabLine(); init_BLOG_HOUSE(); blog.setViewPage(mViewPage); blog.initViewPage(); blog.setOnClickListener(this); blog.textListener(); getCSDNApplication().addActivity(this); break; case 4: setContentView(R.layout.iteyemain); iteye=new ITEYEActivity(this,MainActivity.this); iteye.initTabLine(); init_ITEYE(); iteye.setViewPage(mViewPage); iteye.initViewPage(); iteye.setOnClickListener(this); iteye.textListener(); getCSDNApplication().addActivity(this); break; } } @Override public void onClick(View v) { switch (select){ case 1: csdn.csdnClick(v); break; case 2: cto.ctoClick(v); break; case 3: blog.blogClick(v); break; case 4: iteye.iteyeClick(v); break; } } /** * 初始化CSDN */ public void initCSDN() { tabPageIndicator = (TabPageIndicator) findViewById(R.id.id_indicator); mViewPage = (ViewPager) findViewById(R.id.id_pager); Resources res = getResources(); String[] array = res.getStringArray(R.array.csdn); mAdapter = new TabAdapter(getSupportFragmentManager(), array, select); mViewPage.setAdapter(mAdapter); tabPageIndicator.setViewPager(mViewPage, 0); } /** * 初始化51CTO博客 */ public void init_51CTO() { tabPageIndicator = (TabPageIndicator) findViewById(R.id.id_indicator); mViewPage = (ViewPager) findViewById(R.id.id_pager); Resources res = getResources(); String[] array = res.getStringArray(R.array.cto); mAdapter = new TabAdapter(getSupportFragmentManager(), array, select); mViewPage.setAdapter(mAdapter); tabPageIndicator.setViewPager(mViewPage, 0); } /** * 初始化博客园 */ public void init_BLOG_HOUSE() { tabPageIndicator = (TabPageIndicator) findViewById(R.id.id_indicator); mViewPage = (ViewPager) findViewById(R.id.id_pager); Resources res = getResources(); String[] array = res.getStringArray(R.array.blog_house); mAdapter = new TabAdapter(getSupportFragmentManager(), array, select); mViewPage.setAdapter(mAdapter); tabPageIndicator.setViewPager(mViewPage, 0); } /** * 初始化ITEYE博客 */ public void init_ITEYE() { tabPageIndicator = (TabPageIndicator) findViewById(R.id.id_indicator); mViewPage = (ViewPager) findViewById(R.id.id_pager); Resources res = getResources(); String[] array = res.getStringArray(R.array.iteye); mAdapter = new TabAdapter(getSupportFragmentManager(), array, select); mViewPage.setAdapter(mAdapter); tabPageIndicator.setViewPager(mViewPage, 0); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK ) { Intent intent=new Intent(this,SelectActivity.class); startActivity(intent); return true; } else { return super.onKeyDown(keyCode, event); } }
主要是MainFragment的源码:
public class MainFragment extends Fragment implements IXListViewRefreshListener, IXListViewLoadMore { private static final int LOAD_MORE = 0x110; private static final int LOAD_REFREASH = 0x111; private static final int TIP_ERROR_NO_NETWORK = 0x112; private static final int TIP_ERROR_SERVER = 0x113; //是否是第一次进入 private boolean isFirstIn = true; //是否连接网络 private boolean isConnNet = false; //选择是选择哪个博客 1:CSDN 2:51CTO 3:博客园 4:ITEYE博客 private int select; //选择的是博客的哪个 private int selectType; /** * 当前数据是否从网络获取 */ private boolean isLoadingDataFromNetwork=true; //取CSDN数据 private int newTypes = Constant.NEW_TYPE_YEJIE; //取51CTO数据 private int newTypes_cto = Constant.NEWS_TYPE_NETWORK; //取博客园数据 private int newTypes_blog_house = Constant.NEWS_TYPE_HOME; //取ITeye数据 private int newTypes_iteye = Constant.NEWS_TYPE_NEW; private int currentPage = 1; //CSDN private NewsItemBiz newsItemBiz; //51CTO private NewItem51CTO newItem51CTO; //博客园 private NewItemBlogHouse blogHouse; //ITeye private NewItemITeye iTeye; private me.maxwin.view.XListView mXlistView; private NewsItemAdapter mAdapter; private List<NewsItem> mDatas = new ArrayList<NewsItem>(); /** * 与数据库交互 */ private NewsItemDao newsItemDao; public MainFragment() { } public MainFragment(int newsType, int select) { this.newTypes = newsType; this.newTypes_cto = newsType + 5; this.newTypes_blog_house = newsType + 9; this.newTypes_iteye = newsType + 13; this.select = select; newsItemBiz = new NewsItemBiz(); newItem51CTO = new NewItem51CTO(); blogHouse = new NewItemBlogHouse(); iTeye = new NewItemITeye(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab_item_fragment_main, container, false); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); newsItemDao = new NewsItemDao(getContext()); mAdapter = new NewsItemAdapter(getActivity(), mDatas, select); mAdapter.setSelectType(newTypes_iteye); mXlistView = (me.maxwin.view.XListView) getView().findViewById(R.id.id_xlistView); mXlistView.setAdapter(mAdapter); mXlistView.setPullRefreshEnable(this); mXlistView.setPullLoadEnable(this); switch (select) { case 1: mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes)); break; case 2: mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_cto)); break; case 3: mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_blog_house)); break; case 4: mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_iteye)); break; } // mXlistView.startRefresh(); mXlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { NewsItem newsItem = mDatas.get(position-1); NewsItem nextItem=mDatas.get(position); Intent intent=null; if (newTypes_iteye==17){ intent = new Intent(getActivity(), SubjectsActivity.class); intent.putExtra("url", newsItem.getLink()); }else { intent = new Intent(getActivity(), NewsContentActivity.class); intent.putExtra("url", newsItem.getLink()); intent.putExtra("nextUrl", nextItem.getLink()); intent.putExtra("select", select); intent.putExtra("cuttenrPage", currentPage); } startActivity(intent); } }); if (isFirstIn) { /** * 进来时直接刷新 */ mXlistView.startRefresh(); isFirstIn = false; } else { mXlistView.NotRefreshAtBegin(); } } @Override public void onLoadMore() { new LoadDatasTask().execute(LOAD_MORE); } @Override public void onRefresh() { new LoadDatasTask().execute(LOAD_REFREASH); } /** * 记载数据的异步任 * * @author */ class LoadDatasTask extends AsyncTask<Integer, Void, Integer> { @Override protected Integer doInBackground(Integer... params) { switch (params[0]) { case LOAD_MORE: loadMoreData(); break; case LOAD_REFREASH: refreashData(); break; } return -1; } @Override protected void onPostExecute(Integer result) { switch (result) { case TIP_ERROR_NO_NETWORK: Toast.makeText(getActivity(), "没网络连接", Toast.LENGTH_SHORT).show(); mAdapter.notifyDataSetChanged(); mXlistView.stopRefresh(); break; case TIP_ERROR_SERVER: Toast.makeText(getActivity(), "服务器错误", Toast.LENGTH_SHORT).show(); break; default: break; } switch (select) { case 1: mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes)); break; case 2: mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_cto)); break; case 3: mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_blog_house)); break; case 4: mXlistView.setRefreshTime(AppUtil.getRefreashTime(getActivity(), newTypes_iteye)); break; } mXlistView.stopRefresh(); mXlistView.stopLoadMore(); } } /** * 刷新获取数据 * * @return */ public Integer refreashData() { if (NetUtil.checkNet(getActivity())) { isConnNet = true; List<NewsItem> newsItems = null; try { switch (select) { case 1: newsItems = newsItemBiz.getNewsItems(newTypes, currentPage); List<NewsItem> newsItem1 = new ArrayList<>(); for (int i = 0; i <= newsItems.size() / 2; i++) { newsItem1.add(newsItems.get(i)); } mAdapter.setDatas(newsItem1); mDatas=newsItem1; isLoadingDataFromNetwork = true; // 设置刷新时间 AppUtil.setRefreashTime(getActivity(), newTypes); // 清除数据库数据 newsItemDao.deleteAll(newTypes,select); // 存入数据库 newsItemDao.add(newsItems,select); break; case 2: newsItems = newItem51CTO.getNewsItems(newTypes_cto, currentPage); List<NewsItem> newsItem2 = new ArrayList<>(); for (int i = 0; i <= newsItems.size() / 2; i++) { newsItem2.add(newsItems.get(i)); } mAdapter.setDatas(newsItem2); mDatas=newsItem2; isLoadingDataFromNetwork = true; // 设置刷新时间 AppUtil.setRefreashTime(getActivity(), newTypes_cto); // 清除数据库数据 newsItemDao.deleteAll(newTypes_cto,select); // 存入数据库 newsItemDao.add(newsItem2,select); break; case 3: newsItems = blogHouse.getNewsItems(newTypes_blog_house, currentPage); List<NewsItem> newsItem3 = new ArrayList<>(); for (int i = 0; i <= newsItems.size() / 2; i++) { newsItem3.add(newsItems.get(i)); } mAdapter.setDatas(newsItem3); mDatas=newsItem3; isLoadingDataFromNetwork = true; // 设置刷新时间 AppUtil.setRefreashTime(getActivity(), newTypes_blog_house); // 清除数据库数据 newsItemDao.deleteAll(newTypes_blog_house,select); // 存入数据库 newsItemDao.add(newsItem3,select); break; case 4: newsItems = iTeye.getNewsItems(newTypes_iteye, currentPage, newTypes_iteye); List<NewsItem> newsItem4 = new ArrayList<>(); for (int i = 0; i <= newsItems.size() / 2; i++) { newsItem4.add(newsItems.get(i)); } mAdapter.setDatas(newsItem4); mDatas=newsItem4; isLoadingDataFromNetwork = true; // 设置刷新时间 AppUtil.setRefreashTime(getActivity(), newTypes_iteye); // 清除数据库数据 newsItemDao.deleteAll(newTypes_iteye,select); // 存入数据库 newsItemDao.add(newsItem4,select); break; } // mDatas = newsItems; } catch (Exception e) { e.printStackTrace(); isLoadingDataFromNetwork = false; return TIP_ERROR_SERVER; } } else { isConnNet = false; isLoadingDataFromNetwork = false; List<NewsItem> newsItems = null; switch (select) { case 1: //从数据库取数据 newsItems = newsItemDao.list(newTypes, currentPage,select); break; case 2: newsItems = newsItemDao.list(newTypes_cto, currentPage,select); break; case 3: newsItems = newsItemDao.list(newTypes_blog_house, currentPage,select); break; case 4: newsItems = newsItemDao.list(newTypes_iteye, currentPage,select); break; } mDatas = newsItems; return TIP_ERROR_NO_NETWORK; } return -1; } /** * 根据网络决定是从哪里取数据 */ public void loadMoreData() { System.out.println("isLoadingDataFromNetwork------" + isLoadingDataFromNetwork); if (isLoadingDataFromNetwork) { currentPage =currentPage+ 1; List<NewsItem> newsItems = null; try { switch (select) { case 1: newsItems = newsItemBiz.getNewsItems(newTypes, currentPage); break; case 2: newsItems = newItem51CTO.getNewsItems(newTypes_cto, currentPage); break; case 3: newsItems = blogHouse.getNewsItems(newTypes_blog_house, currentPage); break; case 4: newsItems = iTeye.getNewsItems(newTypes_iteye, currentPage,newTypes_iteye); break; } newsItemDao.add(newsItems,select); mAdapter.addAll(newsItems); } catch (Exception e) { e.printStackTrace(); } } else { // 从数据库加载的 currentPage += 1; List<NewsItem> newsItems = null; switch (select) { case 1: newsItems = newsItemDao.list(newTypes, currentPage,select); break; case 2: newsItems = newsItemDao.list(newTypes_cto, currentPage,select); break; case 3: newsItems = newsItemDao.list(newTypes_blog_house, currentPage,select); break; case 4: newsItems = newsItemDao.list(newTypes_iteye, currentPage,select); break; } if(newsItems!=null) { mAdapter.addAll(newsItems); } } }
对于Listview的每个Item的布局,这样的写法也很常见了,所以都不多说了,注意一点就行,这里对图片的加载用的是Github上的ImageLoader项目。
Frament的布局文件
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <me.maxwin.view.XListView
- android:id="@+id/id_xlistView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:cacheColorHint="#00000000"
- android:divider="@drawable/base_list_divider_drawable"
- android:fadingEdge="none" >
- </me.maxwin.view.XListView>
- </RelativeLayout>
XlistView的Item的布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:paddingLeft="16dp"
- android:paddingRight="16dp"
- android:paddingTop="8dp"
- android:paddingBottom="8dp"
- >
- <TextView
- android:id="@+id/id_title"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:textSize="18sp"
- android:textColor="@color/black"
- android:text="如何有效地在海外市场推广产品?"/>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:baselineAligned="true"
- >
- <ImageView
- android:id="@+id/id_newsImg"
- android:src="@drawable/images"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginRight="8dp"
- android:visibility="visible"/>
- <LinearLayout
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_gravity="center_vertical"
- android:layout_weight="1"
- android:gravity="center_vertical"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:gravity="top" >
- <TextView
- android:id="@+id/id_content"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="top"
- android:maxLines="2"
- android:text="3D打印机一直以来只能进行单向操作,任务一旦开始便无法反悔。不过最近一批研究生研发了一种新型打印机,让你在打印的同时,可以修改重塑之前的设计。让我们一起来看看这个神奇的设备究竟是怎样的吧。"
- android:textColor="@color/nomalGray"
- android:textSize="14sp"
- />
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical" >
- <TextView
- android:id="@+id/id_date"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:gravity="center_vertical|right"
- android:paddingTop="8dp"
- android:singleLine="true"
- android:text="13分钟前|11次阅读"
- android:textSize="12sp"
- android:textColor="@color/nomalGray" />
- <TextView
- android:id="@+id/id"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:paddingTop="16dp"
- android:text="1"
- android:gravity="right"
- android:visibility="gone"
- android:textColor="@color/nomalGray"
- />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- <!-- <TextView
- android:id="@+id/availableNum"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="@color/nomalGray"
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- android:background="@drawable/bg_mark_pressed"
- android:text="可借:4"
- android:textSize="13sp"
- android:layout_marginLeft="2dp"
- android:layout_marginRight="2dp"
- android:layout_centerVertical="true"
- /> -->
- </LinearLayout>