public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
BaseAdapter adapter1,adapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = $(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
adapter1 = new BaseAdapter(Source.get("left"));
adapter2 = new BaseAdapter(Source.get("right"));
Button button1 = $(R.id.button1);
Button button2 = $(R.id.button2);
recyclerView.setAdapter(adapter1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recyclerView.swapAdapter(adapter1,false);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recyclerView.swapAdapter(adapter2,false);
}
});
}
@SuppressWarnings("unchecked")
public <T> T $(int resId) {
return (T) findViewById(resId);
}
class BaseAdapter extends RecyclerView.Adapter{
List<String> data;
public BaseAdapter(List<String> data) {
super();
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new BaseViewHolder(new TextView(parent.getContext()));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((BaseViewHolder) holder).render(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
class BaseViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public BaseViewHolder(View itemView) {
super(itemView);
textView = ((TextView) itemView);
}
public void render(String name){
textView.setText(name);
}
}
}
static class Source{
public static List<String> get(String name){
List<String> list = new ArrayList<>();
int size = (int) (10 + Math.random() * 100);
for (int i = 0; i < size; i++) {
list.add(name + i);
}
return list;
}
}
}
demo for swapAdapter
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- DetectingFaces Demo 传送门相关书籍推荐instant-opencv-for-iosiOS Ap...
- 配置 STEP 1 如果使用数据库存储任务队列,在Gemfile中加入 然后执行 STEP 2 生成delayed...
- 起因 项目中要用到webview进行上传文件, 由于Android自身的原因,4.4.X的版本,都无法实现上传功能...
- 要让Express在ES6下跑起来就不得不用转码器Babel了。首先新建一个在某目录下新建一个项目。然后跳转到这个...