打造万能的适配器,超级简单使用,省时省力。
項目地址鏈接
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.fingerth:FingerthCommonAdapter:1.0.1'
}
ListView lv;
ArrayList list = new ArrayList<>();
//list.add("ViewPager Demo");
lv.setAdapter(new CommonAdapter(this, list, android.R.layout.simple_list_item_1) {
@Override
public void convert(ViewHolder helper, String item) {
helper.setText(android.R.id.text1, item);
}
});
ViewPager vp;
ArrayList arrayList = new ArrayList<>();
for (int i = 1; i < 6; i++) {
arrayList.add("pager : " + i);
}
vp.setAdapter(new CommonPagerAdapter(this, arrayList, R.layout.pager_item_view) {
@Override
public void convert(PagerHolder holder, String item) {
holder.setText(R.id.tv, item);
}
});
> 要支持无限滑怎办,不要担心,用这个UnlimitedSlidePagerAdapter
ViewPager vp;
ArrayList arrayList = new ArrayList<>();
for (int i = 1; i < 4; i++) {
arrayList.add("unlimited pager : " + i);
}
UnlimitedSlidePagerAdapter unlimitedSlidePagerAdapter = new UnlimitedSlidePagerAdapter(this, arrayList, R.layout.pager_item_view) {
@Override
public void convert(PagerHolder holder, String item) {
holder.setText(R.id.tv, item);
}
};
vp.setAdapter(unlimitedSlidePagerAdapter);
vp.setCurrentItem(unlimitedSlidePagerAdapter.getMidPosition(), false);
正常使用
RecyclerView rv;
ArrayList arrayList = new ArrayList<>();
for (int i = 0; i < 50; i++) {
arrayList.add("item:" + i);
}
GridLayoutManager manager = new GridLayoutManager(this, 2);
rv.setLayoutManager(manager);
rv.setAdapter(new CommonRecyclerAdapter(this, arrayList) {
@Override
public int setLayoutId(int viewType) {
return android.R.layout.simple_list_item_1;
}
@Override
public void onBind(Holder holder, int RealPosition, String data) {
holder.setText(android.R.id.text1, data);
}
});
有头部,有尾部
RecyclerView rv;
ArrayList arrayList = new ArrayList<>();
for (int i = 0; i < 50; i++) {
arrayList.add("item:" + i);
}
CommonRecyclerAdapter adapter = new CommonRecyclerAdapter(this, arrayList) {
@Override
public int setLayoutId(int viewType) {
return android.R.layout.simple_list_item_1;
}
@Override
public void onBind(Holder holder, int RealPosition, String data) {
holder.setText(android.R.id.text1, data);
}
};
View mHeaderView = View.inflate(this, R.layout.view_header, null);
adapter.setHeaderView(mHeaderView);
View mFootView = View.inflate(this, R.layout.view_foot, null);
adapter.setFootView(mFootView);
GridLayoutManager manager = new GridLayoutManager(this, 2);
rv.setLayoutManager(manager);
rv.setAdapter(adapter);
不同Item的使用
RecyclerView rv;
ArrayList arrayList = new ArrayList<>();
for (int i = 0; i < 50; i++) {
arrayList.add("item:" + i);
}
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(manager);
rv.setAdapter(new CommonRecyclerAdapter(this, arrayList) {
@Override
public int itemViewType(int position) {
if (position % 3 == 1) {
return 0x88;
}
return super.itemViewType(position);
}
@Override
public int setLayoutId(int viewType) {
switch (viewType) {
case 0x88:
return R.layout.view_foot;
default:
return android.R.layout.simple_list_item_1;
}
}
@Override
public void onBind(Holder holder, int RealPosition, String data) {
switch (holder.getItemViewType()) {
case 0x88:
holder.setText(R.id.tv, data);
break;
default:
holder.setText(android.R.id.text1, data);
break;
}
}
});