Giter Site home page Giter Site logo

hanmichael / listviewhelper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nicewarm/listviewhelper

0.0 2.0 0.0 472 KB

MVCHelper 替代该类库不再局限于ListView,不再局限于某个下拉刷新的类库,https://github.com/LuckyJayce/MVCHelper

Java 100.00%

listviewhelper's Introduction

#MVCHelper 替代该类库可以任意view使用该mvc架构,可以自由选择主流的下拉刷新框架,地址:https://github.com/LuckyJayce/MVCHelper

ListViewHelper

ListViewHelper. 实现下拉刷新,滚动底部自动加载更多,分页加载,自动切换显示网络失败布局,暂无数据布局,,真正的MVC架构.

1.Model (IDataSource) 负责获取数据

    //数据源
public interface IDataSource<DATA> {
	// 获取刷新的数据
	public DATA refresh() throws Exception;

	// 获取加载更多的数据
	public DATA loadMore() throws Exception;

	// 是否还可以继续加载更多
	public boolean hasMore();
}

例如:分页加载书籍列表数据

public class BooksDataSource implements IDataSource<List<Book>> {
	private int page = 1;
	private int maxPage = 5;

	@Override
	public List<Book> refresh() throws Exception {
		return loadBooks(1);
	}

	@Override
	public List<Book> loadMore() throws Exception {
		return loadBooks(page + 1);
	}

	private List<Book> loadBooks(int page) {
		List<Book> books = new ArrayList<Book>();
		for (int i = 0; i < 20; i++) {
			books.add(new Book("page" + page + "  Java编程** " + i, 108.00d));
		}
		this.page = page;
		return books;
	}

	@Override
	public boolean hasMore() {
		return page < maxPage;
	}

}

2.View(IDataAdapter)负责显示数据

public interface IDataAdapter<DATA> extends ListAdapter {

	public abstract void setData(DATA data, boolean isRefresh);

	public abstract DATA getData();

	public void notifyDataSetChanged();
}

例如:分页显示书籍列表数据

public class BooksAdapter extends BaseAdapter implements IDataAdapter<List<Book>> {
	private List<Book> books = new ArrayList<Book>();
	private LayoutInflater inflater;

	public BooksAdapter(Context context) {
		super();
		inflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_book, parent, false);
		}
		TextView textView = (TextView) convertView;
		textView.setText(books.get(position).getName());
		return convertView;
	}

	@Override
	public void setData(List<Book> data, boolean isRefresh) {
		if (isRefresh) {
			books.clear();
		}
		books.addAll(data);
	}

	@Override
	public int getCount() {
		return books.size();
	}

	@Override
	public List<Book> getData() {
		return books;
	}


	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}


}

3.Controller (Activity,Fragment,ListViewHelper) 负责调度显示和调度获取数据

Activity负责调度,代码如下

public class MainActivity extends Activity {

	private ListViewHelper<List<Book>> listViewHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置LoadView的factory,用于创建用户自定义的加载失败,加载中,加载更多等布局
		// ListViewHelper.setLoadViewFractory(new LoadViewFractory());

		listViewHelper = new ListViewHelper<List<Book>>(this, R.layout.pulltofrefreshlistview);
		setContentView(listViewHelper.getPullToRefreshAdapterViewBase());

		// 设置数据源
		listViewHelper.setDataSource(new BooksDataSource());
		// 设置适配器
		listViewHelper.setAdapter(new BooksAdapter(this));

		// 加载数据
		listViewHelper.refresh();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 释放资源
		listViewHelper.destory();
	}
}

只要写了上述几行代码,恭喜你,你已经实现了分页加载显示书籍列表,实现下拉刷新,滚动底部自动加载更多,在网络请求失败的时候自动显示网络失败,没有数据时显示无数据布局,加载成功时显示书籍列表

4.ILoadViewFractory 自定义 失败布局,无数据布局,加载中布局

实现ILoadViewFractory
然后ListViewHelper.setLoadViewFractory(new LoadViewFractory());
就这样,就会显示你自定义的布局

5.说明

本项目使用了Android-PullToRefresh-Library第三方下拉刷新类库
地址https://github.com/chrisbanes/Android-PullToRefresh
使用者可以根据需求换成其他下拉刷新类库

License

Copyright 2015 shizhefei(LuckyJayce)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

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.

listviewhelper's People

Contributors

luckyjayce avatar

Watchers

hanmichael avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.