RecycleView的多条目适配,有几种类型创建几种布局,根据值判断返回的类型。
1、适配器的创建:
1、适配器的创建:
/**
*
创建适配器
* Created by lenovo on 2017/4/24.
*/
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
//
定义常量
private final int TYPE_0=0;
private final int TYPE_1=1;
ArrayList<String> lists;
Context context;
public MyAdapter(Context context, ArrayList<String> lists) {
this.lists=lists;
this.context=context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType==TYPE_0){
View view=View.
inflate
(context,R.layout.
list0_layouts
,null);
return new MyViewHolder(view);
}else {
View view=View.
inflate
(context,R.layout.
list_layout
,null);
return new MyViewHolder1(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//
判断
if(holder instanceof MyViewHolder){
((MyViewHolder) holder).text_0.setText(lists.get(position));
((MyViewHolder) holder).text_0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.
makeText
(context, "
这是
"+position, Toast.
LENGTH_SHORT
).show();
}
});
}if(holder instanceof MyViewHolder1){
((MyViewHolder1) holder).text_1.setText(lists.get(position));
((MyViewHolder1) holder).text_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.
makeText
(context, "
这是
"+position, Toast.
LENGTH_SHORT
).show();
}
});
}
}
@Override
public int getItemCount() {
return lists.size();
}
//判断返回的类型:
@Override
public int getItemViewType(int position) {
if(position%3==0){
return TYPE_1;
}else {
return TYPE_0;
}
}
//根据不同的类型声明对应的ViewHolder
class MyViewHolder extends RecyclerView.ViewHolder{
private final TextView text_0;
public MyViewHolder(View itemView) {
super(itemView);
//
声明控件
text_0 = (TextView) itemView.findViewById(R.id.
text_0
);
//
设置点击事件
}
}
//根据不同的类型声明对应的ViewHolder
class MyViewHolder1 extends RecyclerView.ViewHolder{
private final TextView text_1;
public MyViewHolder1(View itemView) {
super(itemView);
text_1 = (TextView) itemView.findViewById(R.id.
text_1
);
}
}
}
2.在MainActivity中操作:
public class MainActivity extends AppCompatActivity {
private int num=0;
private ArrayList<String> lists;
private RecyclerView rv;
private MyAdapter adapter;
private ArrayList<String> arrayList=new ArrayList<>();
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
//
初始化控件
initViews();
}
private void initViews() {
rv = (RecyclerView) findViewById(R.id.
rv
);
pb = (ProgressBar) findViewById(R.id.
pb
);
//
设置布局管理者
rv.setLayoutManager(new LinearLayoutManager(this));
//
设置适配器
getAdapterDatas();
//
设置监听
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState==RecyclerView.
SCROLL_STATE_IDLE
){
RecyclerView.LayoutManager manager = rv.getLayoutManager();
if(manager instanceof LinearLayoutManager){
int position = ((LinearLayoutManager) manager).findLastVisibleItemPosition();
if(position==(arrayList.size()-1)){
num+=15;
getAdapterDatas();//
调用刷新方法
//设置进度条
pb.setVisibility(View.
VISIBLE
);
//进行发送handler,让进度条进行显示的时间
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
pb.setVisibility(View.
GONE
);
}
},3000);
}
}
}
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
//
建立一个刷新适配器的方法
private void getAdapterDatas(){
//
设置数据
lists = new ArrayList<>();
for (int i=0;i<num+15;i++){
lists.add("name "+i+"");
}
for (int i = num; i <lists.size() ; i++) {
arrayList.add(lists.get(i));
}
if(adapter==null){
adapter = new MyAdapter(MainActivity.this, arrayList);
rv.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();//
刷新
}
}
}