//加依赖compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.android.support:recyclerview-v7:25.3.1'
//主页面
public class MainActivity extends AppCompatActivity { private RecyclerView my_recycler; private String url="http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=2&size=20&offset=0"; private MyAdapter adapter; private SwipeRefreshLayout refreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); getData(); } private void initView() { my_recycler = (RecyclerView) findViewById(R.id.my_recycler); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); my_recycler.setLayoutManager(linearLayoutManager); adapter = new MyAdapter(MainActivity.this); my_recycler.setAdapter(adapter); } private void initRefreshLayout() { refreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light); } private void getData(){ OkHttpUtils instance = OkHttpUtils.getInstance(); instance.getDataByNet(url, new OkHttpUtils.CallBack() { @Override public void onResponse(String result) { if (result!=null){ DataBean bean = DataBean.objectFromData(result); adapter.addData(bean.song_list); adapter.addBean(bean.billboard); }else { Toast.makeText(MainActivity.this,"网络请求失败",Toast.LENGTH_SHORT).show(); } } }); }}
//OKHttpUtils类public class OkHttpUtils { //声明变量 private OkHttpClient okHttpClient; private volatile static OkHttpUtils okUtils; private final String TAG = OkHttpUtils.class.getSimpleName();//获得当前的类名 private Handler handler; private Request request; //提交json数据 private static final MediaType JSON = MediaType.parse("application/json;charset=utf-8"); //提交字符串 private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown;charset=utf-8"); private OkHttpUtils(){ okHttpClient = new OkHttpClient.Builder().addInterceptor(new LoggingInterceptor()).build(); handler = new Handler(Looper.getMainLooper()); } //采用单例模式获取对象 public static OkHttpUtils getInstance(){ OkHttpUtils instance = null; if (okUtils == null){ synchronized (OkHttpUtils.class){ if (instance == null){ instance = new OkHttpUtils(); okUtils = instance; } } } return instance; } /** * 网络请求 * @param url * @param callBack */ public void getDataByNet(String url, final CallBack callBack){ request = new Request.Builder().url(url).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(final Call call, final Response response) throws IOException { final String string = response.body().string(); handler.post(new Runnable() { @Override public void run() { if (call != null) { callBack.onResponse(string); } } }); } }); } /** * 回调接口 */ public interface CallBack{ void onResponse(String result); } }
//网络拦截器public class LoggingInterceptor implements Interceptor { private static final String UA = "User-Agent"; @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request() .newBuilder() .addHeader(UA, makeUA()) .build(); return chain.proceed(request); } private String makeUA() { String s = Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE; return Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE; } }
//MyAdapter类public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private List<DataBean.SongListBean> data=new ArrayList<>(); private Context context; private final LayoutInflater inflater; private final DisplayImageOptions options; private final ImageLoader loader; private DataBean.BillboardBean billboardBean; private final int TITLE_TYPE = 0,SONG_TYPE = 1; private ViewHolder1 holder1; private ViewHolder2 holder2; public MyAdapter(Context context) { this.context = context; inflater = LayoutInflater.from(context); ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(context); loader = ImageLoader.getInstance(); loader.init(configuration); // 加载中图片 // 没有图片资源时的默认图片 //加载失败的图片 options = new DisplayImageOptions.Builder() .cacheOnDisk(true) .cacheInMemory(true) .showImageOnLoading(R.drawable.loading) // 加载中图片 .showImageForEmptyUri(R.mipmap.ic_launcher) // 没有图片资源时的默认图片 .showImageOnFail(R.drawable.fail) //加载失败的图片 .build(); } public void addData(List<DataBean.SongListBean> list){ this.data.addAll(list); notifyDataSetChanged(); } public void addBean(DataBean.BillboardBean billboardBean){ this.billboardBean = billboardBean; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType==TITLE_TYPE){ View view = inflater.inflate(R.layout.title_layout, parent,false); holder1 = new ViewHolder1(view); return holder1; } View view = inflater.inflate(R.layout.context_layout, parent, false); holder2 = new ViewHolder2(view); return holder2; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof ViewHolder1){ loader.displayImage(billboardBean.pic_s192,((ViewHolder1)holder).title_pic,options); ((ViewHolder1) holder).title_name.setText(billboardBean.name); ((ViewHolder1) holder).update_time.setText("最近更新:"+billboardBean.update_date); ((ViewHolder1) holder).title_comment.setText(billboardBean.comment); } if (holder instanceof ViewHolder2){ DataBean.SongListBean bean = data.get(position); loader.displayImage(bean.pic_small,((ViewHolder2) holder).song_pic,options); ((ViewHolder2) holder).song_title.setText(bean.title); ((ViewHolder2) holder).song_author.setText(bean.author+"-"+bean.album_title); } } @Override public int getItemCount() { return data==null?0:data.size(); } @Override public int getItemViewType(int position) { if (position==0){ return TITLE_TYPE; } return SONG_TYPE; } public class ViewHolder1 extends RecyclerView.ViewHolder{ public ImageView title_pic; public TextView title_name,update_time,title_comment; public ViewHolder1(View itemView) { super(itemView); title_pic = (ImageView)itemView.findViewById(R.id.title_pic); title_name = (TextView)itemView.findViewById(R.id.title_name); update_time = (TextView)itemView.findViewById(R.id.update_time); title_comment = (TextView)itemView.findViewById(R.id.title_comment); } } public class ViewHolder2 extends RecyclerView.ViewHolder{ public ImageView song_pic; public TextView song_title,song_author; public ViewHolder2(View itemView) { super(itemView); song_pic = (ImageView)itemView.findViewById(R.id.song_pic); song_title = (TextView)itemView.findViewById(R.id.song_title); song_author = (TextView)itemView.findViewById(R.id.song_author); } } }
//主页面布局<android.support.v4.widget.SwipeRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/refreshLayout"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/my_recycler"></android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout>
//条目1布局<ImageView android:id="@+id/title_pic" android:src="@mipmap/ic_launcher" android:layout_width="200dp" android:layout_weight="0.5" android:layout_height="200dp" android:layout_margin="10dp" /> <LinearLayout android:orientation="vertical" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/title_name" android:text="热歌榜" android:padding="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/update_time" android:text="最近更新时间:" android:padding="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/title_comment" android:text="榜单介绍" android:padding="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
//条目2布局<ImageView android:id="@+id/song_pic" android:src="@mipmap/ic_launcher" android:layout_width="60dp" android:layout_height="60dp" android:layout_margin="10dp" /> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/song_title" android:text="城都" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" /> <TextView android:id="@+id/song_author" android:text="赵雷" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" /> </LinearLayout>
RecyclerView的多条目展示,使用OkHttp解析数据
最新推荐文章于 2019-01-03 15:01:58 发布