CommonAdapter


Source link: https://github.com/qyxxjd/CommonAdapter

  • ????????
  • ?????
  • ???????
  • ??????
  • view??
  • RecyclerView item ???????

gradle??

dependencies {

  compile 'com.classic.adapter:commonadapter:1.8.1'
  //??????RecyclerView,??????
  compile 'com.android.support:recyclerview-v7:25.3.1' 
}

v1.8.1 ????

  1. ?? CommonRecyclerAdapter ? OnItemLongClickListener ???????? boolean.
  2. CommonRecyclerAdapter ?? addChildViewListener(viewId, listener) ??, ??? OnChildViewClickListener ? OnChildViewLongClickListener ? OnChildViewTouchListener.

????

ListView/GridView ????

List<News> newsList = ...; //????? listView = (ListView) findViewById(R.id.listview);
 listView.setAdapter(new CommonAdapter<News>(context,
  //item????
  R.layout.item_none_picture, newsList ) {

  @Override public void onUpdate(BaseAdapterHelper helper, News item, int position) {

//BaseAdapterHelper????????

 helper.setText(R.id.xxx, item.getTitle())

 //1.???????????????????????????????

 //2.????????????????????????adapter??????????????demo

.setImageLoad(new GlideImageLoad())

.setImageUrl(R.id.xxx,item.getCoverUrl());

  
}
 
}
);
  //????? private final class MultipleLayoutAdapter extends CommonAdapter<News>{

public MultipleLayoutAdapter(Context context, int layoutResId, List<News> data) {

super(context, layoutResId, data);

  
}

  //???????????
  @Override public int getLayoutResId(News item, int position) {

int layoutResId = -1;

switch (item.getNewsType()){

 case News.TYPE_NONE_PICTURE: //?????

  layoutResId = R.layout.item_none_picture;

  break;

 case News.TYPE_SINGLE_PICTURE: //?????

  layoutResId = R.layout.item_single_picture;

  break;

 case News.TYPE_MULTIPLE_PICTURE: //?????

  layoutResId = R.layout.item_multiple_picture;

  break;

  ??????? ...

}

return layoutResId;
  
}

@Override public void onUpdate(BaseAdapterHelper helper, News item, int position) {

switch (item.getNewsType()){

 case News.TYPE_NONE_PICTURE: //?????

  helper.setText(R.id.xxx, item.getTitle())

  .setImageUrl(R.id.xxx,item.getCoverUrl());

  break;

 case News.TYPE_SINGLE_PICTURE: //?????

  helper.setText(R.id.xxx, item.getTitle())

  .setImageUrl(R.id.xxx,item.getCoverUrl());

  break;

 case News.TYPE_MULTIPLE_PICTURE: //?????

  helper.setText(R.id.xxx, item.getTitle())

  .setImageUrl(R.id.xxx,item.getCoverUrl());

  break;

  ??????? ...

}

  
}
 
}

RecyclerView ????

?? DiffUtil ???? RecyclerView ????????

List<News> newsList = ...; //????? recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
 recyclerView.setLayoutManager(new LinearLayoutManager(this));
 recyclerView.setHasFixedSize(true);
 recyclerView.setItemAnimator(new DefaultItemAnimator());
 newsAdapter = new NewsAdapter(this, R.layout.item_none_picture, newsList);
 recyclerView.setAdapter(newsAdapter);
  private class NewsAdapter extends CommonRecyclerAdapter<News>{

public NewsAdapter(Context context, int layoutResId, List<News> data) {

super(context, layoutResId, data);

  
}

@Override public void onUpdate(BaseAdapterHelper helper, News item, int position) {

helper.setText(R.id.xxx, item.getTitle())

.setImageUrl(R.id.xxx,item.getCoverUrl());

  
}
 
}
  //????? private final class MultipleLayoutAdapter extends CommonRecyclerAdapter<News>{

public MultipleLayoutAdapter(Context context, int layoutResId, List<News> data) {

super(context, layoutResId, data);

  
}

  //???????????
  @Override public int getLayoutResId(News item, int position) {

int layoutResId = -1;

switch (item.getNewsType()){

 case News.TYPE_NONE_PICTURE: //?????

  layoutResId = R.layout.item_none_picture;

  break;

 case News.TYPE_SINGLE_PICTURE: //?????

  layoutResId = R.layout.item_single_picture;

  break;

 case News.TYPE_MULTIPLE_PICTURE: //?????

  layoutResId = R.layout.item_multiple_picture;

  break;

  ??????? ...

}

return layoutResId;
  
}

@Override public void onUpdate(BaseAdapterHelper helper, News item, int position) {

switch (item.getNewsType()){

 case News.TYPE_NONE_PICTURE: //?????

  helper.setText(R.id.xxx, item.getTitle())

  .setImageUrl(R.id.xxx,item.getCoverUrl());

  break;

 case News.TYPE_SINGLE_PICTURE: //?????

  helper.setText(R.id.xxx, item.getTitle())

  .setImageUrl(R.id.xxx,item.getCoverUrl());

  break;

 case News.TYPE_MULTIPLE_PICTURE: //?????

  helper.setText(R.id.xxx, item.getTitle())

  .setImageUrl(R.id.xxx,item.getCoverUrl());

  break;

  ??????? ...

}

  
}
 
}

RecyclerView ????

commonRecyclerAdapter.setOnItemClickListener(this) // ?? item ????

 .setOnItemLongClickListener(this) // ?? item ????

 // ?? Child view ???????Touch??(???)

 // OnChildViewClickListener ? OnChildViewLongClickListener ? OnChildViewTouchListener

 .addChildViewListener(R.id.viewId, listener);

BaseAdapterHelper ????

helper.setText(R.id.viewId, text)

 .setTag(R.id.viewId, objectTag)

 .setAlpha(R.id.viewId, 0.6f)

 .setBackgroundColor(R.id.viewId, R.color.colorResId)

 .setBackgroundRes(R.id.viewId, R.drawable.drawableResId)

 .setChecked(R.id.viewId, true)

 .setImageBitmap(R.id.viewId, bitmap)

 .setImageDrawable(R.id.viewId, drawable)

 .setImageResource(R.id.viewId, R.drawable.drawableResId)

 .setImageUrl(R.id.viewId, url)

 .setProgress(R.id.viewId, progress)

 .setProgress(R.id.viewId, progress, max)

 .setRating(R.id.viewId, rating)

 .setRating(R.id.viewId, rating, max)

 .setTextColor(R.id.viewId, R.color.colorResId)

 .setTextColorRes(R.id.viewId, R.color.colorResId)

 .setTextColorRes(R.id.viewId, R.color.colorResId, theme)

 //TextView?????????????android.text.util.Linkify#addLinks(TextView text, int mask)

 .addLinks(R.id.viewId, Linkify.ALL)

 //????

 .addAllLinks(R.id.viewId)

 //??TextView??Typeface

 .setTypeface(R.id.viewId, typeface)

 //??TextView??Typeface

 .setTypeface(typeface, R.id.xxx1, R.id.xxx2, R.id.xxx3, ...)

 .setVisible(R.id.viewId, View.VISIBLE)

 //ProgressBar??Max

 .setMax(R.id.viewId, max)

 //ListView??adapter

 .setAdapter(R.id.viewId, adapter)

 // ?? Child view ???????Touch??(???)

 .setOnTouchListener(R.id.viewId, onTouchListener)

 .setOnClickListener(R.id.viewId, onClickListener)

 .setOnLongClickListener(R.id.viewId, onLongClickListener)

 .setOnItemClickListener(R.id.viewId, onItemClickListener)

 .setOnItemLongClickListener(R.id.viewId, onItemLongClickListener)

 .setOnItemSelectedClickListener(R.id.viewId, onItemSelectedListener);
  //??item?convertView View convertView = helper.getView();
  //??????????,????getView(viewId)??View,???????? View childView = helper.getView(R.id.viewId);
 

????????????

public class YourApplication extends Application {

@Override public void onCreate() {

super.onCreate();

 ...

 ImageLoad imageLoadImpl = new GlideImageLoad();

Adapter.config(new Adapter.Builder().setImageLoad(imageLoadImpl));

  
}
 
}

???????

public class YourXXX implements ImageLoad {

@Override public void load(Context context, ImageView imageView, String imageUrl) {

 //??Glide????

Glide.with(context).load(imageUrl).into(imageView);

 or

 //??Picasso????

Picasso.with(context).load(url).into(imageView);

 or

 Fresco

Android-Universal-Image-Loader

???

...
  
}
 
}

???????

//CommonAdapter?CommonRecyclerAdapter????IData?????????????????  void add(T elem);
  void addAll(List<T> elem);
  void set(T oldElem, T newElem);
  void set(int index, T elem);
  void remove(T elem);
  void remove(int index);
  void replaceAll(List<T> elem);
  boolean contains(T elem);
  void clear();
 

??

base-adapter-helper

??

License

Copyright 2015 classic  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

Resources

It's a cool way to show share widget.

java-object-diff is a simple, yet powerful library to find differences between Java objects. It takes two objects and generates a tree structure that represents any differences between the objects and their children. This tree can then be traversed to extract more information or apply changes to the underlying data structures.

Did you used sqlite to save your data on Android? If you did, you may be puzzled for the complexity of mechanism. Now the Andoird ORM (Aorm) coming which armed to make it simple for the developers.

Sample project showing usage of AutoValue library.

This project holds adapters for combining Realm Java with Android UI components and framework classes.

Another sliding menu base on DrawerLayout.

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