AssemblyAdapter


Source link: https://github.com/panpf/AssemblyAdapter

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' 
}

lastVersionName? ???v?

????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. 

Resources

Most powerful wave load view, you can custom any style by yourself.

An avatar ImageView with any kind of shapes.

A demo project which has FlowLayout to work with tags.

A lot of utility classes for Android development.

A Java fluent assertion wrapper to improve readability of RxJava's TestSubscriber assertions.

Standalone ZXing barcode/qrcode scanner library so that you don't have to install the ZXing Barcode Scanner app explicitly.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes