布局
<com.jcodecraeer.xrecyclerview.XRecyclerView android:layout_width="match_parent" android:id="@+id/show_recycler" android:layout_height="match_parent"> </com.jcodecraeer.xrecyclerview.XRecyclerView>
适配器:
public class ShowRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { Context context; ArrayList<SuperClass.NewslistBean> list; public ShowRecyclerAdapter(Context context, ArrayList<SuperClass.NewslistBean> list) { this.context = context; this.list = list; } private enum Type{ Type_one,Type_two } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType == Type.Type_one.ordinal()){ View view = LayoutInflater.from(context).inflate(R.layout.item_recyclera,null); MyShowViewHolderA viewholder = new MyShowViewHolderA(view); return viewholder; }else if(viewType == Type.Type_two.ordinal()){ View view = LayoutInflater.from(context).inflate(R.layout.item_recyclerb,null); MyShowViewHolderB viewholder = new MyShowViewHolderB(view); return viewholder; } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if(holder instanceof MyShowViewHolderA){ ((MyShowViewHolderA)holder).texta.setText(list.get(position).getTitle()); }else if(holder instanceof MyShowViewHolderB){ String bitUrl = list.get(position).getPicUrl(); ImageLoader loader = ImageLoader.getInstance(); loader.displayImage(bitUrl, ((MyShowViewHolderB)holder).head); ((MyShowViewHolderB)holder).textb.setText(list.get(position).getTitle()); } } @Override public int getItemCount() { return list.size(); } @Override public int getItemViewType(int position) { if(position%2==0) { return Type.Type_one.ordinal(); }else{ return Type.Type_two.ordinal(); } } class MyShowViewHolderA extends RecyclerView.ViewHolder{ TextView texta; public MyShowViewHolderA(View itemView) { super(itemView); texta = (TextView) itemView.findViewById(R.id.recyclera_text); } } class MyShowViewHolderB extends RecyclerView.ViewHolder{ TextView textb; ImageView head; public MyShowViewHolderB(View itemView) { super(itemView); textb = (TextView) itemView.findViewById(R.id.recyclerb_text); head = (ImageView) itemView.findViewById(R.id.recyclerb_head); } } }
Activity代码如下:
public class ShowActivity extends AppCompatActivity { XRecyclerView recyclerView; ShowRecyclerAdapter adapter; ArrayList<SuperClass.NewslistBean> list; ArrayList<SuperClass.NewslistBean> newlist; String url = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=20&page=1"; String url1 = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=20&page ="; int page =1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); recyclerView = (XRecyclerView) findViewById(R.id.show_recycler); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setPullRefreshEnabled(true); recyclerView.setLoadingMoreEnabled(true); recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { Toast.makeText(ShowActivity.this,"刷新....",Toast.LENGTH_SHORT).show(); list.clear(); page=1; inData(); recyclerView.refreshComplete(); } @Override public void onLoadMore() { Toast.makeText(ShowActivity.this,"加载....",Toast.LENGTH_SHORT).show(); addData(); // ArrayList<SuperClass.NewslistBean> mylist = addData(); // Toast.makeText(ShowActivity.this,mylist.toString(),Toast.LENGTH_SHORT).show(); // list.addAll(mylist); // adapter.notifyDataSetChanged(); recyclerView.refreshComplete(); } }); inData(); } public void addData(){ // Toast.makeText(ShowActivity.this, url1+page, Toast.LENGTH_SHORT).show(); page++; String url3 = url1+page; newlist = new ArrayList<SuperClass.NewslistBean>(); OkHttp3Utils.getInstance().doGet(url3, new GsonObjectCallback<SuperClass>() { @Override public void onUi(SuperClass superClass) { //Toast.makeText(ShowActivity.this, superClass.getNewslist().toString(), Toast.LENGTH_SHORT).show(); newlist = (ArrayList<SuperClass.NewslistBean>) superClass.getNewslist(); list.addAll(superClass.getNewslist()); adapter.notifyDataSetChanged(); } @Override public void onFailed(Call call, IOException e) { } }); // return newlist; } public void inData(){ list = new ArrayList<>(); OkHttp3Utils.getInstance().doGet(url, new GsonObjectCallback<SuperClass>() { @Override public void onUi(SuperClass superClass) { list = (ArrayList<SuperClass.NewslistBean>) superClass.getNewslist(); //Toast.makeText(getActivity(), list.toString(), Toast.LENGTH_SHORT).show(); adapter = new ShowRecyclerAdapter(ShowActivity.this,list); recyclerView.setAdapter(adapter); } @Override public void onFailed(Call call, IOException e) { } }); } }