//首先要先导个包,xlistview的包 public class Fragment_stream extends Fragment { private View inflate; private XListView xlist_stream; private List<Video.V9LG4CHORBean> list_v9LG4CHOR; String[]st = {"V9LG4CHOR","V9LG4E6VR","00850FRB"}; int a=0; myadpter my1; List<Videopath> list_videopath=new ArrayList<>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { inflate = inflater.inflate(R.layout.fragment_stream, null); return inflate; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { xlist_stream = (XListView) inflate.findViewById(R.id.xlist_stream); getDatas(a); super.onActivityCreated(savedInstanceState); } public void xlistview(){ final Handler handler=new Handler(); //设置xlistview的属性: xlist_stream.setPullRefreshEnable(true);//刷新 xlist_stream.setPullLoadEnable(true);//加载 //监听 xlist_stream.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { //默认值 a=0; getDatas(a);//这是封装的一个解析的方法 onLoadTime(); Toast.makeText(getActivity(), "下拉刷新", Toast.LENGTH_SHORT).show(); //2秒后停止刷新 handler.postDelayed(new Runnable() { @Override public void run() { xlist_stream.stopRefresh(); } },5000); } //2秒后重新加载: @Override public void onLoadMore() { //这是加载数据 a++; getDatas(a); Toast.makeText(getActivity(), "上拉加载", Toast.LENGTH_SHORT).show(); handler.postDelayed(new Runnable() { @Override public void run() { xlist_stream.stopLoadMore(); } },2000); } }); if(my1==null) { my1=new myadpter(); xlist_stream.setAdapter(my1); }else{ my1.notifyDataSetChanged(); } } private void getDatas(int a){ String url="http://c.3g.163.com/nc/video/list/"+st[a]+"/n/10-10.html"; myAsnytask m1=new myAsnytask(); m1.execute(url); } class myAsnytask extends AsyncTask<String,Integer,String>{ @Override protected String doInBackground(String... params) { String url = params[0]; //创建网络请求对象 HttpClient client = new DefaultHttpClient(); //创建请求方式 HttpGet request = new HttpGet(url); try { HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == 200) { publishProgress(10); InputStream inputStream = response.getEntity().getContent(); ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); int len=0; byte[] b1=new byte[1024]; while((len=inputStream.read(b1))!=-1) { outputStream.write(b1,0,len); } outputStream.close(); inputStream.close(); String s = outputStream.toString(); Log.i("ssssss",s.toString()); return s; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { Gson g1=new Gson(); try { JSONObject obj=new JSONObject(s); //获得key键 Iterator<String> keys = obj.keys(); while(keys.hasNext()){ String next = keys.next();//每个key JSONArray jsonArray = obj.optJSONArray(next); for(int i=0;i<jsonArray.length();i++){ JSONObject object = jsonArray.optJSONObject(i); Videopath videopath = g1.fromJson(object.toString(), Videopath.class); list_videopath.add(videopath); } } } catch (Exception e) { e.printStackTrace(); } xlistview(); super.onPostExecute(s); } } class myadpter extends BaseAdapter{ @Override public int getCount() { return list_videopath.size(); } @Override public Object getItem(int position) { return list_videopath.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { viewholder holder; if(convertView==null) { convertView=View.inflate(getActivity(),R.layout.video_listview,null); holder=new viewholder(); holder.text_video_list= (TextView) convertView.findViewById(R.id.text_video_list); holder.player= (JCVideoPlayerStandard) convertView.findViewById(R.id.player_list_video); convertView.setTag(holder); } else{ holder= (viewholder) convertView.getTag(); } holder.text_video_list.setText(list_videopath.get(position).getTitle()); boolean setUp = holder.player.setUp(list_videopath.get(position).getMp4_url(), JCVideoPlayer.SCREEN_LAYOUT_LIST, ""); if (setUp) { Glide.with(getActivity()).load(list_videopath.get(position).getCover()).into(holder.player.thumbImageView); } return convertView; } class viewholder{ TextView text_video_list; JCVideoPlayerStandard player; } } //设置加载时间 private void onLoadTime() { //获取系统当前时间 long timeMillis = System.currentTimeMillis(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM--dd HH:mm:ss"); Date date = new Date(timeMillis); String time = format.format(date); xlist_stream.setRefreshTime(time); } }
xlistview和视频结合使用
最新推荐文章于 2017-03-17 18:05:50 发布