Activity中
public class SanActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&";
private RecyclerView mRecyclerView;
private List<Super.SongListBean> mDatas;
private HomeAdapter mAdapter;
private SwipeRefreshLayout swipeRefreshLayout;//下拉刷新
private int count=20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_san);
boolean netWorkAvailable = NetWorkUtils.isNetWorkAvailable(this);
if (!netWorkAvailable) {
Toast.makeText(SanActivity.this, "联网:" + netWorkAvailable, Toast.LENGTH_SHORT).show();
}
mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(this);
getData();
}
private void getData() {
OkHttp3Utils.getInstance().doGet(path+"type=1&size="+count+"&offset=0", new GsonObjectCallback<Super>() {
@Override
public void onUi(Super aSuper) {
mDatas = aSuper.getSong_list();
mAdapter=new HomeAdapter();
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(SanActivity.this));//list
}
@Override
public void onFailed(Call call, IOException e) {
}
});
}
@Override
public void onRefresh() {//刷新
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
count++;
mAdapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}
},3000);
}
//RecyclerView
class HomeAdapter extends RecyclerView.Adapter {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
/*MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
SanActivity.this).inflate(R.layout.item, parent,
false));*/
View view = null;
RecyclerView.ViewHolder holder = null;
switch (viewType){
case 0:
view = LayoutInflater.from(SanActivity.this).inflate(R.layout.item, parent, false);
holder = new MyViewHolder(view);
break;
case 1:
view = LayoutInflater.from(SanActivity.this).inflate(R.layout.item1, parent, false);
holder = new MyViewHolder1(view);
break;
}
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)){
case 0:
MyViewHolder holder1= (MyViewHolder) holder;
holder1.tv1.setText(mDatas.get(position).getTitle());
//ImageLoader imageLoader=ImageLoader.getInstance();
ImageLoader.getInstance().displayImage(mDatas.get(position).getPic_small(), holder1.imageView1);
break;
case 1:
MyViewHolder1 holder2= (MyViewHolder1) holder;
holder2.tv2.setText(mDatas.get(position).getSi_proxycompany());
break;
}
}
/* @Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.tv1.setText(mDatas.get(position).getTitle());
//ImageLoader imageLoader=ImageLoader.getInstance();
ImageLoader.getInstance().displayImage(mDatas.get(position).getPic_small(), holder.imageView1);
}*/
@Override
public int getItemCount() {
return mDatas.size();
}
@Override
public int getItemViewType(int position) {
if (position%2 == 0){
return 0;
} else {
return 1;
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv1;
ImageView imageView1;
public MyViewHolder(View view) {
super(view);
tv1 = (TextView) view.findViewById(R.id.item_text);
imageView1 = (ImageView) view.findViewById(R.id.item_img);
}
}
class MyViewHolder1 extends RecyclerView.ViewHolder {
TextView tv2;
public MyViewHolder1(View view) {
super(view);
tv2 = (TextView) view.findViewById(R.id.item1_text);
}
}
}
}
xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_san"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bwie.disanzhoumoni.SanActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>