Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# SimpleNews
基于Material Design和MVP的新闻客户端
基于Material Design和MVP的新闻客户端,fork了之后我干了啥?
* 修改部分MVP逻辑,完善可能造成内存泄露的代码。
* 增添一个懒加载,让ViewPager+FragmentPagerAdapter的结合更加优雅。

# Screenshot
###### 新闻列表
Expand Down Expand Up @@ -34,4 +36,4 @@ Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public void onFailure(String msg, Exception e) {
mImageView.hideProgress();
mImageView.showLoadFailMsg();
}

public void onDestroy() {
if (mImageView != null)
mImageView = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
R.color.accent);
mSwipeRefreshWidget.setOnRefreshListener(this);

mRecyclerView = (RecyclerView)view.findViewById(R.id.recycle_view);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycle_view);
mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(getActivity());
Expand Down Expand Up @@ -84,7 +84,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE
&& lastVisibleItem + 1 == mAdapter.getItemCount() ) {
&& lastVisibleItem + 1 == mAdapter.getItemCount()) {
//加载更多
Snackbar.make(getActivity().findViewById(R.id.drawer_layout), getString(R.string.image_hit), Snackbar.LENGTH_SHORT).show();
}
Expand All @@ -93,15 +93,15 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

@Override
public void onRefresh() {
if(mData != null) {
if (mData != null) {
mData.clear();
}
mImagePresenter.loadImageList();
}

@Override
public void addImages(List<ImageBean> list) {
if(mData == null) {
if (mData == null) {
mData = new ArrayList<ImageBean>();
}
mData.addAll(list);
Expand All @@ -123,4 +123,12 @@ public void showLoadFailMsg() {
View view = getActivity() == null ? mRecyclerView.getRootView() : getActivity().findViewById(R.id.drawer_layout);
Snackbar.make(view, getString(R.string.load_fail), Snackbar.LENGTH_SHORT).show();
}

@Override
public void onDestroy() {
super.onDestroy();
if (mImagePresenter != null)
((ImagePresenterImpl) mImagePresenter).onDestroy();
mImagePresenter = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.lauren.simplenews.main.view.MainView;
import com.lauren.simplenews.R;

/**
* Description :
* Author : lauren
Expand Down Expand Up @@ -37,4 +38,9 @@ public void switchNavigation(int id) {
break;
}
}

public void onDestroy() {
if (mMainView != null)
mMainView = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,13 @@ public void switch2About() {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new AboutFragment()).commit();
mToolbar.setTitle(R.string.navigation_about);
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mMainPresenter != null)
((MainPresenterImpl) mMainPresenter).onDestroy();
mMainPresenter = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void loadNewsDetail(final String docId) {

@Override
public void onSuccess(NewsDetailBean newsDetailBean) {
if(newsDetailBean != null) {
if (newsDetailBean != null) {
mNewsDetailView.showNewsDetialContent(newsDetailBean.getBody());
}
mNewsDetailView.hideProgress();
Expand All @@ -45,4 +45,9 @@ public void onSuccess(NewsDetailBean newsDetailBean) {
public void onFailure(String msg, Exception e) {
mNewsDetailView.hideProgress();
}

public void onDestroy() {
if (mNewsDetailView != null)
mNewsDetailView = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ public void onFailure(String msg, Exception e) {
mNewsView.hideProgress();
mNewsView.showLoadFailMsg();
}

public void onDestroy(){
if(mNewsView != null)
mNewsView = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.lauren.simplenews.news.widget;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
* Created by Luo_xiasuhuei321@163.com on 2016/8/29.
*
* 实现懒加载的Fragment
*/
public abstract class BaseLazyFragment extends Fragment {

protected View mRootView;
protected Context mContext;
protected boolean isVisible;
private boolean isPrepared;
private boolean isFirst = true;

//--------------------system method callback------------------------//

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
isPrepared = true;
initPrepare();
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(getUserVisibleHint()){
isVisible = true;
lazyLoad();
}else{
isVisible = false;
onInvisible();
}
}

@Override
public void onResume() {
super.onResume();
if(getUserVisibleHint()){
setUserVisibleHint(true);
}
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(mRootView == null){
mRootView = initView(inflater,container,savedInstanceState);
}

return mRootView;
}

//--------------------------------method---------------------------//

/**
* 懒加载
*/
protected void lazyLoad(){
if(!isPrepared || !isVisible || !isFirst){
return;
}
initData();
isFirst = false;
}

//--------------------------abstract method------------------------//

/**
* 在onActivityCreated中调用的方法,可以用来进行初始化操作。
*/
protected abstract void initPrepare();

/**
* fragment被设置为不可见时调用
*/
protected abstract void onInvisible();

/**
* 这里获取数据,刷新界面
*/
protected abstract void initData();

/**
* 初始化布局,请不要把耗时操作放在这个方法里,这个方法用来提供一个
* 基本的布局而非一个完整的布局,以免ViewPager预加载消耗大量的资源。
*/
protected abstract View initView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,12 @@ public void showProgress() {
public void hideProgress() {
mProgressBar.setVisibility(View.GONE);
}

@Override
protected void onDestroy() {
super.onDestroy();
if(mNewsDetailPresenter != null)
((NewsDetailPresenterImpl)mNewsDetailPresenter).onDestroy();
mNewsDetailPresenter = null;
}
}
Loading