AssemblyAdapter
AssemblyAdapter ? Android ???? Adapter ??????????? Adapter ???????? Item????? header ? footer?????????
??
Item ????????
. ??????? item layout ??? ItemFactory??????? ItemFactory ????????? Item
. ????????? ItemFactory??? ItemFactory ???? itemType?? header ? footer
. ?? AssemblyAdapter ??? ExpandableListView?GridView?RecyclerView?ViewPager ???? header ? footer??????? header ? footer
. header ? footer ?????? setEnabled(boolean) ?????????????????
. ?????????????????????????????????? ItemFactory ?????? Adapter
. ?? BaseAdapter?RecyclerView.Adapter?BaseExpandableListAdapter?PagerAdapter?FragmentPagerAdapter ? FragmentStatePagerAdapter???? Android ????????? Adapter?? SpanSize
. AssemblyRecyclerItemFactory ?? SpanSize?????????????????
. ????????????????????????
????
1. ? JCenter ?? AssemblyAdapter
dependencies {
compile 'me.xiaopan:assemblyadapter:lastVersionName'
}
????API 7
2. ??
?? 6 ? Adapter?
Adapter | ?? | ??? | ???? |
---|---|---|---|
AssemblyAdapter | BaseAdapter | ListView?GridView?Spinner?Gallery | ?Item?header?footer????? |
AssemblyRecyclerViewAdapter | RecyclerView.Adapter | RecyclerView | ?Item?header?footer????? |
AssemblyExpandableAdapter | BaseExpandableListAdapter | ExpandableListView | ?Item?header?footer????? |
AssemblyPagerAdapter | PagerAdapter | ViewPager + View | ?Item?header?footer |
AssemblyFragmentPagerAdapter | FragmentPagerFragment | ViewPager + Fragment | ?Item?header?footer |
AssemblyFragmentStatePagerAdapter | FragmentStatePagerFragment | ViewPager + Fragment | ?Item?header?footer |
???? AssemblyAdapter ???????????? Adapter ??????????? ItemFactory ? Item ???????????????? sample ??
AssemblyAdapter ??????
- Adapter????????itemType ?????????
- ItemFactory?????????? Item
- Item????? itemView?????????????
AssemblyAdapter ????? Adapter ?????????? item ????????????? ItemFactory ?????????????? Adapter ? addItemFactory(AssemblyItemFactory) ??? ItemFactory ?? Adapter ???
??????????????????????????????????????? addItemFactory(AssemblyItemFactory) ?????? ItemFactory??? ItemFactory ????? ItemType??????? AssemblyAdapter ??? Assembly ??????
??????? Item??? Adapter ???????????????????????? Object
3. ?? ItemFactory
??? AssemblyAdapter ?????? ItemFactory ? Item????
public class UserItemFactory extends AssemblyItemFactory<UserItemFactory.UserItem> {
@Override
public boolean isTarget(Object itemObject) {
return itemObject instanceof User;
}
@Override
public UserListItem createAssemblyItem(ViewGroup parent) {
return new UserListItem(R.layout.list_item_user, parent);
}
public class UserItem extends AssemblyItem<User> {
private ImageView headImageView;
private TextView nameTextView;
private TextView sexTextView;
private TextView ageTextView;
private TextView jobTextView;
public UserListItem(int itemLayoutId, ViewGroup parent) {
super(itemLayoutId, parent);
}
@Override
protected void onFindViews(View itemView) {
headImageView = (ImageView) findViewById(R.id.image_userListItem_head);
nameTextView = (TextView) findViewById(R.id.text_userListItem_name);
sexTextView = (TextView) findViewById(R.id.text_userListItem_sex);
ageTextView = (TextView) findViewById(R.id.text_userListItem_age);
jobTextView = (TextView) findViewById(R.id.text_userListItem_job);
}
@Override
protected void onConfigViews(Context context) {
getItemView().setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(v.getConext(), "?" + (getPosition() + 1) + "???", Toast.LENGTH_LONG).show();
}
}
);
}
@Override
protected void onSetData(int position, User user) {
headImageView.setImageResource(user.headResId);
nameTextView.setText(user.name);
sexTextView.setText(user.sex);
ageTextView.setText(user.age);
jobTextView.setText(user.job);
}
}
}
???
ItemFactory ???
?????? createAssemblyItem(ViewGroup) ???????- ItemFactory ?
isTarget()
?????????????????Adapter ????????????????????????? ItemFactory ? isTarget(Object) ?????? true ???????????- ItemFactory ?
createAssemblyItem(ViewGroup)
?????? Item???????????ItemFactory ????????Item???
??????????????? onSetData ? getData() ?????- Item ?
onFindViews(View)
?onConfigViews(Context)
????????? View ??? View?????? Item ???????
???? onFindViews ??????????findViewById(int)
??? View- Item ?
onSetData()
?????????????? getView() ???????
- ????? Item ?
getPosition()
?getData()
????????????????????????? click ????????? setTag() ????????????????- ??????? Item ?
getItemView()
??????? itemView
4. ?? ItemFactory
?????????? new ?? AssemblyAdapter????? Adapter ? addItemFactory(AssemblyItemFactory)
???? ItemFactory ??????
ListView listView = ...; List<Object> dataList = new ArrayList<Object>; dataList.add(new User("????"));
dataList.add(new User("????"));
AssemblyAdapter adapter = new AssemblyAdapter(dataList);
adapter.addItemFactory(new UserItemFactory());
listView.setAdapter(adapter);
?????????? ItemFactory????
ListView listView = ...; List<Object> dataList = new ArrayList<Object>; dataList.add(new User("????"));
dataList.add(new Game("????"));
dataList.add(new User("????"));
dataList.add(new Game("????"));
AssemblyAdapter adapter = new AssemblyAdapter(dataList);
adapter.addItemFactory(new UserItemFactory());
adapter.addItemFactory(new GameItemFactory());
listView.setAdapter(adapter);
5. ?? header ? footer
?? Adapter ????? header ? footer????????????????????????????????? GridView?RecyclerView ???? header ? footer
?? header?footer
????????? header ? footer ? ItemFactory
???? addHeaderItem(AssemblyItemFactory, Object)
? addFooterItem(AssemblyItemFactory, Object)
??????????
AssemblyAdapter adapter = new AssemblyAdapter(objects);
adapter.addHeaderItem(new HeaderItemFactory(), "???????");
... adapter.addFooterItem(new HeaderItemFactory(), "???????");
addHeaderItem(AssemblyItemFactory, Object) ? addFooterItem(AssemblyItemFactory, Object) ??????? Item ?????????????
?????header?footer
addHeaderItem() ? addFooterItem() ?????????? header ? footer ? FixedItemInfo ??????
AssemblyAdapter adapter = new AssemblyAdapter(objects);
FixedItemInfo userFixedItemInfo = adapter.addHeaderItem(new HeaderItemFactory(), "???????");
// ?? userFixedItemInfo.setEnabled(false);
// ?? userFixedItemInfo.setEnabled(true);
???? header ? footer ?? Item.getPosition() ????????? Item ? Adapter ??????????????????????? Adapter ? getPositionInPart(int)
??
6. ????????
???????????? AssemblyLoadMoreItemFactory ? ItemFactory? AssemblyLoadMoreItemFactory ?????????????????????????????????
public class LoadMoreItemFactory extends AssemblyLoadMoreItemFactory {
public LoadMoreListItemFactory(OnLoadMoreListener eventListener) {
super(eventListener);
}
@Override
public AssemblyLoadMoreItem createAssemblyItem(ViewGroup parent) {
return new LoadMoreListItem(R.layout.list_item_load_more, parent);
}
public class LoadMoreItem extends AssemblyLoadMoreItem {
private View loadingView;
private View errorView;
private View endView;
public LoadMoreListItem(int itemLayoutId, ViewGroup parent) {
super(itemLayoutId, parent);
}
@Override
protected void onFindViews(View itemView) {
loadingView = findViewById(R.id.text_loadMoreListItem_loading);
errorView = findViewById(R.id.text_loadMoreListItem_error);
endView = findViewById(R.id.text_loadMoreListItem_end);
}
@Override
public View getErrorRetryView() {
return errorView;
}
@Override
public void showLoading() {
loadingView.setVisibility(View.VISIBLE);
errorView.setVisibility(View.INVISIBLE);
endView.setVisibility(View.INVISIBLE);
}
@Override
public void showErrorRetry() {
loadingView.setVisibility(View.INVISIBLE);
errorView.setVisibility(View.VISIBLE);
endView.setVisibility(View.INVISIBLE);
}
@Override
public void showEnd() {
loadingView.setVisibility(View.INVISIBLE);
errorView.setVisibility(View.INVISIBLE);
endView.setVisibility(View.VISIBLE);
}
}
}
???? Adapter ? setLoadMoreItem(AssemblyLoadMoreItemFactory)
???????? ItemFactory ??????
AssemblyAdapter adapter = ...; adapter.setLoadMoreItem(new LoadMoreItemFactory(new OnLoadMoreListener(){
@Override
public void onLoadMore(AssemblyAdapter adapter) {
// ????????
...
boolean loadSuccess = ...;
if (loadSuccess) {
// ??????????????????????Adapter?loadMoreFinished(boolean)????????????
boolean loadMoreEnd = ...;
adapter.loadMoreFinished(loadMoreEnd);
}
else {
// ???????Adapter?loadMoreFailed()?????????????????????????????
adapter.loadMoreFailed();
}
}
}
));
?????? setDisableLoadMore(boolean)
???? setLoadMoreEnd(boolean) ????????????????????? setLoadMoreEnd(boolean) ? true ????????? end ???? setDisableLoadMore(boolean) ?????????????
7. ? RecyclerView ? GridLayoutManager ??? Item ????????
?? RecyclerView ? GridLayoutManager ????????????????? GridLayoutManager ??????? SpanSizeLookup ????????????? Item ??????????AssemblyRecyclerAdapter ???????????????????????????
???? SpanSizeLookup???? AssemblyRecyclerAdapter ? getSpanSize(int) ?????????? Item ? SpanSize?
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 4);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
RecyclerView.Adapter adapter = recyclerView.getAdapter();
if (adapter == null || !(adapter instanceof AssemblyRecyclerAdapter)) {
return 1;
}
return ((AssemblyRecyclerAdapter) adapter).getSpanSize(position);
}
}
);
recyclerView.setLayoutManager(gridLayoutManager);
???? AssemblyRecyclerAdapter??? ItemFactory ??? SpanSize
AssemblyRecyclerAdapter adapter = new AssemblyRecyclerAdapter(dataList);
adapter.addItemFactory(new AppListHeaderItemFactory().setSpanSize(4));
recyclerView.setAdapter(adapter);
AppListHeaderItemFactory ??? AssemblyRecyclerItemFactory ????? setSpanSize(int) ??
???????? fullSpan(RecyclerView) ?????????fullSpan ????? RecyclerView ???GridLayoutManager ? SpanCount ?? SpanSize
AssemblyRecyclerAdapter adapter = new AssemblyRecyclerAdapter(dataList);
adapter.addItemFactory(new AppListHeaderItemFactory().fullSpan(recyclerView));
recyclerView.setAdapter(adapter);
fullSpan() ??????? RecyclerView ? LayoutManager ? StaggeredGridLayoutManager ???????? Item ?? setFullSpan(true)??? Item ? StaggeredGridLayoutManager ???????
8.? Kotlin ?????? Kotterknife
? 2.3.2 ?? AssemblyAdapter ??? Kotlin ??? Kotterknife ??? View????????????????????????
AssemblyRecyclerItem ??? RecyclerView.ViewHolder ???? Kotterknife ????? bindView
AssemblyItem?AssemblyGroupItem?AssemblyChildItem ????????? Kotterknife ????????? Kotterknife ? Botterknife.kt ????
public fun <V : View> AssemblyItem<*>.bindView(id: Int)
: ReadOnlyProperty<AssemblyItem<*>, V> = required(id, viewFinder) private val AssemblyItem<*>.viewFinder: AssemblyItem<*>.(Int) -> View?
get() = {
itemView.findViewById(it)
}
public fun <V : View> AssemblyGroupItem<*>.bindView(id: Int)
: ReadOnlyProperty<AssemblyGroupItem<*>, V> = required(id, viewFinder) private val AssemblyGroupItem<*>.viewFinder: AssemblyGroupItem<*>.(Int) -> View?
get() = {
itemView.findViewById(it)
}
public fun <V : View> AssemblyChildItem<*>.bindView(id: Int)
: ReadOnlyProperty<AssemblyChildItem<*>, V> = required(id, viewFinder) private val AssemblyChildItem<*>.viewFinder: AssemblyChildItem<*>.(Int) -> View?
get() = {
itemView.findViewById(it)
}
??????? app ?? Sample Botterknife.kt ??
License
Copyright (C) 2016 Peng fei Pan <[email protected]> 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.