- ????????
- ?????
- ???????
- ??????
- 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 ????
- ??
CommonRecyclerAdapter
?OnItemLongClickListener
????????boolean
. 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();
??
??
- Blog: http://blog.csdn.net/qy1387
- Email: [email protected]
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.