CymChad / BaseRecyclerViewAdapterHelper
- вторник, 31 мая 2016 г. в 03:12:11
Java
Powerful and flexible RecyclerAdapter, www.recyclerview.org
Powerful and flexible RecyclerAdapter
Please feel free to use this.(Love can be a Star)
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Add the dependency
dependencies {
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.7.4'
}
public class QuickAdapter extends BaseQuickAdapter<Status> {
public QuickAdapter(Context context) {
super(context, R.layout.tweet, DataServer.getSampleData());
}
@Override
protected void convert(BaseViewHolder helper, Status item) {
helper.setText(R.id.tweetName, item.getUserName())
.setText(R.id.tweetText, item.getText())
.setText(R.id.tweetDate, item.getCreatedAt())
.setImageUrl(R.id.tweetAvatar, item.getUserAvatar())
.setVisible(R.id.tweetRT, item.isRetweet())
.linkify(R.id.tweetText);
}
}
mQuickAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//..
}
});
// Turn animation
quickAdapter.openLoadAnimation();
or
// Turn animation and set animate
quickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
or
// Turn animation and set custom animate
quickAdapter.openLoadAnimation(new BaseAnimation() {
@Override
public Animator[] getAnimators(View view) {
return new Animator[]{
ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1),
ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)
};
}
});
public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<MultipleItem> {
public MultipleItemQuickAdapter(Context context, List data) {
super(context, data);
addItmeType(MultipleItem.TEXT, R.layout.text_view);
addItmeType(MultipleItem.IMG, R.layout.image_view);
}
@Override
protected void convert(BaseViewHolder helper, MultipleItem item) {
switch (helper.getItemViewType()) {
case MultipleItem.TEXT:
helper.setImageUrl(R.id.tv, item.getContent());
break;
case MultipleItem.IMG:
helper.setImageUrl(R.id.iv, item.getContent());
break;
}
}
}
mQuickAdapter.addHeaderView(getView());
mQuickAdapter.addFooterView(getView());
setOnLoadMoreListener
mQuickAdapter.setOnLoadMoreListener(this);
Override onLoadMoreRequested()
@Override
public void onLoadMoreRequested() {
mRecyclerView.post(new Runnable() {
@Override
public void run() {
if (mCurrentCounter >= TOTAL_COUNTER) {
mQuickAdapter.notifyDataChangedAfterLoadMore(false);
}
} else {
mQuickAdapter.notifyDataChangedAfterLoadMore(DataServer.getSampleData(PAGE_SIZE),true);
mCurrentCounter = mQuickAdapter.getItemCount();
}});
}
mQuickAdapter.setLoadingView(customView);
public class SectionAdapter extends BaseSectionQuickAdapter<MySection> {
public SectionAdapter(Context context, int layoutResId, int sectionHeadResId, List data) {
super(context, layoutResId, sectionHeadResId, data);
}
@Override
protected void convert(BaseViewHolder helper, MySection item) {
helper.setImageUrl(R.id.iv, (String) item.t);
}
@Override
protected void convertHead(BaseViewHolder helper,final MySection item) {
helper.setText(R.id.header, item.header);
if(!item.isMroe)helper.setVisible(R.id.more,false);
else
helper.setOnClickListener(R.id.more, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,item.header+"more..",Toast.LENGTH_LONG).show();
}
});
}
mQuickAdapter.setEmptyView(getView());
Adapter
protected void convert(BaseViewHolder helper, Status item) {
helper.setOnClickListener(R.id.tweetAvatar, new OnItemChildClickListener())
.setOnClickListener(R.id.tweetName, new OnItemChildClickListener());
}
Activity
mQuickAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
String content = null;
Status status = (Status) adapter.getItem(position);
switch (view.getId()) {
case R.id.tweetAvatar:
content = "img:" + status.getUserAvatar();
break;
case R.id.tweetName:
content = "name:" + status.getUserName();
break;
}
Toast.makeText(AnimationUseActivity.this, content, Toast.LENGTH_LONG).show();
}
});
JoanZapata / base-adapter-helper
Copyright 2016 陈宇明
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.