public class NewFragment extends Fragment{ private PullToRefreshListView refreshListView; private String param; private int page_num=1; private List<NewBean.ResultsBean> list = new ArrayList<>(); private MyAdapter myAdapter;; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.newfragment,container,false); refreshListView = (PullToRefreshListView) view.findViewById(R.id.refresh_list_view); refreshListView.setMode(PullToRefreshBase.Mode.BOTH); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle bundle = getArguments(); param = bundle.getString("name", "fuli"); //请求数据,,,设置适配器 getData(); ILoadingLayout startLabels = refreshListView.getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新"); startLabels.setRefreshingLabel("正在刷新..."); startLabels.setReleaseLabel("放开刷新"); ILoadingLayout endLabels = refreshListView.getLoadingLayoutProxy( false, true); endLabels.setPullLabel("上拉刷新"); endLabels.setRefreshingLabel("正在载入..."); endLabels.setReleaseLabel("放开刷新..."); //设置监听 refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { page_num=1; getData(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { //上拉加载的时候 page_num加一 page_num++; getData(); } }); } private void getData() { AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { try { String path = "http://gank.io/api/data/"+ URLEncoder.encode(param,"UTF-8")+"/10/1"; URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); //获取 int responseCode = connection.getResponseCode(); if (responseCode == 200){ InputStream inputStream = connection.getInputStream(); String json = streamToString(inputStream,"utf-8"); return json; } } catch (Exception e) { e.printStackTrace(); } return ""; } @Override protected void onPostExecute(String s) { Log.i("你好",s); //解析 Gson gson = new Gson(); NewBean newBean = gson.fromJson(s, NewBean.class); /** * 判断数据非空 */ if (newBean != null && newBean.getResults() != null){ //数据添加 list.addAll(0,newBean.getResults()); //设置适配器 setAdapter(); //停止刷新 refreshListView.onRefreshComplete(); } } }; asyncTask.execute(); } private void setAdapter() { if (myAdapter == null){ myAdapter = new MyAdapter(getActivity(),list); refreshListView.setAdapter(myAdapter); }else { myAdapter.notifyDataSetChanged(); } } private String streamToString(InputStream inputStream, String charset) { try { InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String s = null; StringBuilder builder = new StringBuilder(); while ((s = bufferedReader.readLine()) != null){ builder.append(s); } Log.i("json",builder.toString()); bufferedReader.close(); return builder.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }public class Fragment04 extends Fragment { private TabLayout tabLayout; private ViewPager pager; private List<String> list = new ArrayList<>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment04, container, false); tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); pager = (ViewPager) view.findViewById(R.id.view_pager_shou); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); list.add("福利"); list.add("Android"); list.add("IOS"); list.add("休息视频"); list.add("拓展资源"); list.add("前端"); list.add("all"); //解决tablayout的空数据问题 pager.setOffscreenPageLimit(list.size()); pager.setAdapter(new FragmentPagerAdapter(getFragmentManager()) { @Override public CharSequence getPageTitle(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } @Override public Fragment getItem(int position) { NewFragment newsFragment = new NewFragment(); Bundle bundle = new Bundle(); if (list.get(position).equals("福利")) { bundle.putString("name", "fuli"); } else if (list.get(position).equals("Android")) { bundle.putString("name", "Android"); } else if (list.get(position).equals("IOS")) { bundle.putString("name", "IOS"); } else if (list.get(position).equals("休息视频")) { bundle.putString("name", "xiuxi"); } else if (list.get(position).equals("拓展资源")) { bundle.putString("name", "tuozhan"); } else if (list.get(position).equals("前端")) { bundle.putString("name", "qianduan"); } else if (list.get(position).equals("基金")) { bundle.putString("name", "jijin"); } else if (list.get(position).equals("all")) { bundle.putString("name", "all"); } //把当前位置的标题传递过去 bundle.putString("name",list.get(position)); newsFragment.setArguments(bundle); return newsFragment; } }); tabLayout.setupWithViewPager(pager); } }<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/draw_layout"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relative_layout"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/frame_layout" android:layout_above="@+id/group" ></FrameLayout> <RadioGroup android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/group" android:orientation="horizontal" > <RadioButton android:checked="true" android:padding="20dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/radio01" android:text="首页" android:button="@null" android:gravity="center"/> <RadioButton android:padding="20dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/radio02" android:text="自选" android:button="@null" android:gravity="center"/> <RadioButton android:padding="20dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/radio04" android:text="资讯" android:button="@null" android:gravity="center"/> <RadioButton android:padding="20dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/radio05" android:text="交易" android:button="@null" android:gravity="center"/> </RadioGroup> </RelativeLayout> <LinearLayout android:layout_width="200dp" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="left" android:id="@+id/linear_layout" android:background="#ffffff"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/line1" android:orientation="vertical" android:layout_gravity="center"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/image01" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text01" android:text="V8.36"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/line2" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image02" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text02" android:text="V8.36"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/line5" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image05" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text05" android:text="V8.36"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/line3" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image03" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text03" android:text="V8.36"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/line4" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image04" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text04" android:text="V8.36"/> </LinearLayout> </LinearLayout> </android.support.v4.widget.DrawerLayout>public class MainActivity extends AppCompatActivity { private FrameLayout frameLayout; private RadioGroup group; private FragmentManager manager; private Fragment01 fragment01; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); frameLayout = (FrameLayout) findViewById(R.id.frame_layout); group = (RadioGroup) findViewById(R.id.group); getMess(); } private void getMess() { manager = getSupportFragmentManager(); fragment01 = new Fragment01(); manager.beginTransaction().replace(R.id.frame_layout,fragment01).commit(); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { FragmentTransaction transaction = manager.beginTransaction(); switch (checkedId){ case R.id.radio01: getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,fragment01).commit(); break; case R.id.radio02: getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new Fragment02()).commit(); break; case R.id.radio04: getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new Fragment04()).commit(); break; case R.id.radio05: getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new Fragment05()).commit(); break; } } }); } }public class MyAdapter extends BaseAdapter{ Context context; List<NewBean.ResultsBean> list; public MyAdapter(Context context, List<NewBean.ResultsBean> list) { this.list=list; this.context=context; ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context)); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.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(context, R.layout.item_layout,null); holder = new ViewHolder(); holder.imageView = (ImageView) convertView.findViewById(R.id.image_view); holder.textView = (TextView) convertView.findViewById(R.id.text_title); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } //设置 holder.textView.setText(list.get(position).getDesc()); /** * 加载图片之前需要判断一下是否有图片的地址 */ if (list.get(position).getImages() != null){ ImageLoader.getInstance().displayImage(list.get(position%list.size()).getImages().get(0),holder.imageView,ImageLoaderUtil.getDefaultOption()); } return convertView; } private class ViewHolder{ ImageView imageView; TextView textView; } }
zonghe
最新推荐文章于 2021-05-25 16:16:09 发布