tablayout
来源:互联网 发布:2016黑马java百度网盘 编辑:程序博客网 时间:2024/06/07 06:55
Main3Activity
package com.example.zhaoukaolainxi;
import android.os.Bundle;import android.support.annotation.Nullable;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import java.util.ArrayList;import java.util.List;public class Main3Activity extends Fragment implements AdapterView.OnItemClickListener { private TabLayout tabLayout; private ViewPager viewPager; private List<String> list = new ArrayList<String>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_main3, null); tabLayout = (TabLayout)view.findViewById(R.id.tablayout); viewPager = (ViewPager) view.findViewById(R.id.vp); list.add("社会新闻"); list.add("国内新闻"); list.add("国际新闻"); list.add("娱乐新闻"); list.add("国内体育行业"); list.add("NBA动态"); list.add("国足资讯"); //tabLayout.setTabMode(TabLayout.MODE_FIXED); tabLayout.setupWithViewPager(viewPager); viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) { @Override public CharSequence getPageTitle(int position) { return list.get(position); } @Override public Fragment getItem(int position) { NewsFragment newsFragments = new NewsFragment(); Bundle bundle = new Bundle(); if(list.get(position).equals("社会新闻")){ bundle.putString("name","social"); }else if (list.get(position).equals("国内新闻")){ bundle.putString("name","guonei"); }else if (list.get(position).equals("国际新闻")){ bundle.putString("name","world"); }else if (list.get(position).equals("娱乐新闻")){ bundle.putString("name","huabian"); }else if (list.get(position).equals("国内体育行业")){ bundle.putString("name","tiyu"); }else if (list.get(position).equals("NBA动态")){ bundle.putString("name","nba"); }else if (list.get(position).equals("国足资讯")){ bundle.putString("name","football"); } newsFragments.setArguments(bundle); return newsFragments; } @Override public int getCount() { return list.size(); } }); return view; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { }}
<android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="40dp" app:tabGravity="center" app:tabIndicatorColor="#ff0000" app:tabMode="scrollable" app:tabSelectedTextColor="#ff0000" app:tabTextColor="#00ff00"></android.support.design.widget.TabLayout><android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/tablayout"></android.support.v4.view.ViewPager>
NewsFragment
package com.example.zhaoukaolainxi;import android.os.AsyncTask;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ListView;import com.google.gson.Gson;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import java.util.List;public class NewsFragment extends Fragment { private PullToRefreshListView pu; private String pagesize="20"; private int page=1; private String mstrings; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.news, null); pu = (PullToRefreshListView) inflate.findViewById(R.id.list_view); pu.getRefreshableView(); pu.setMode(PullToRefreshBase.Mode.BOTH); pu.setScrollingWhileRefreshingEnabled(true); pu.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { // //完成下拉刷新操作 @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { refreshView.postDelayed(new Runnable() { @Override public void run() { page=1; getDataFromNet(mstrings,pagesize,page); pu.onRefreshComplete(); } }, 2000); } //完成上拉刷新操作 @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { page++; refreshView.postDelayed(new Runnable() { @Override public void run() { if(mstrings!=null&&pagesize!=null){ getDataFromNet(mstrings,pagesize,page); } pu.onRefreshComplete(); } }, 2000); } }); return inflate; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle bundle = getArguments(); mstrings = bundle.getString("name","social"); getDataFromNet(mstrings,pagesize,page); } private void getDataFromNet(final String string, final String pagesize, final int page) { AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { // http://api.tianapi.com/wxnew/?key=71e58b5b2f930eaf1f937407acde08fe&num=10&page=8 //http://api.kkmh.com/v1/topic_new/discovery_module_list/210?sa_event=eyJwcm9qZWN0Ijoia3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg3OTE2MTcwNjE5LCJwcm9wZXJ0aWVzIjp7IiRvc192ZXJzaW9uIjoiNC4yLjIiLCJHZW5kZXJUeXBlIjoi5peg5rOV6I635Y-WIiwiVHJpZ2dlckl0ZW0iOjE3LCJWaXNpdFBhZ2VOYW1lIjoi5paw5L2c5LiK5p62IiwiJGxpYl92ZXJzaW9uIjoiMS42LjM0IiwiJG5ldHdvcmtfdHlwZSI6IldJRkkiLCIkd2lmaSI6dHJ1ZSwiJG1hbnVmYWN0dXJlciI6InNhbXN1bmciLCJUcmlnZ2VySXRlbU5hbWUiOiLmlrDkvZzkuIrmnrYiLCIkc2NyZWVuX2hlaWdodCI6NTc2LCJMaXN0VHlwZSI6IuaXp-eJiCIsIlByb3BlcnR5RXZlbnQiOiJSZWFkTGlzdCIsIlRyaWdnZXJPcmRlck51bWJlciI6MCwiRmluZFRhYk5hbWUiOiLmjqjojZAiLCJhYnRlc3RfZ3JvdXAiOjQ2LCIkc2NyZWVuX3dpZHRoIjoxMDI0LCIkb3MiOiJBbmRyb2lkIiwiVHJpZ2dlckl0ZW1UeXBlIjowLCJUcmlnZ2VyUGFnZSI6IkZpbmRQYWdlIiwiJGNhcnJpZXIiOiJDTUNDIiwiJG1vZGVsIjoiR1QtUDUyMTAiLCIkYXBwX3ZlcnNpb24iOiIzLjguMSJ9LCJ0eXBlIjoidHJhY2siLCJkaXN0aW5jdF9pZCI6IkE6OTA1MTA0Mjc2Mzc1NTEwOSIsIm9yaWdpbmFsX2lkIjoiQTo5MDUxMDQyNzYzNzU1MTA5IiwiZXZlbnQiOiJSZWFkTGlzdCJ9&style=2&offset=0&limit=3 String path = "http://api.tianapi.com/"+ URLEncoder.encode(string)+"/?key=71e58b5b2f930eaf1937407acde08fe&num="+pagesize+"&page="+page+""; Log.e("wzq", "doInBackground: "+path); try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); if ((connection.getResponseCode() == 200)) { InputStream inputStream = connection.getInputStream(); String json = streamToString(inputStream,"utf-8"); return json; } } catch (java.io.IOException e) { e.printStackTrace(); } return ""; } @Override protected void onPostExecute(String json) { Gson gson = new Gson(); Bea bean = gson.fromJson(json, Bea.class); List<Bea.NewslistBean> list = bean.getNewslist(); if (list != null) { MyAdapter1 myAdapter = new MyAdapter1(getActivity(), list); pu.setAdapter(myAdapter); } } }; asyncTask.execute(); } 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); } bufferedReader.close(); return builder.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }}
<com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" ></com.handmark.pulltorefresh.library.PullToRefreshListView>
Bean
MyAdapter1
package com.example.zhaoukaolainxi;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.List;/** * Created by 呵呵 on 2017/10/26. */public class MyAdapter1 extends BaseAdapter{ Context context; private List<Bea.NewslistBean> list; public MyAdapter1(Context context, List<Bea.NewslistBean> list) { this.context = context; this.list = list; } //视图数量 @Override public int getCount() { return list.size(); } //视图 @Override public Object getItem(int i) { return list.get(i); } //视图id @Override public long getItemId(int i) { return i; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView == null){ holder = new ViewHolder(); //获得布局 convertView = View.inflate(context,R.layout.item,null); //初始化控件 holder.tv = (TextView) convertView.findViewById(R.id.tv1); holder.tv1 = (TextView) convertView.findViewById(R.id.tv2); holder.img = (ImageView) convertView.findViewById(R.id.img); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } //显示获得的数据显示在listview中 holder.tv.setText(list.get(position).getTitle()); holder.tv1.setText(list.get(position).getCtime()); //显示图片在listview中 ImageLoader.getInstance().displayImage(list.get(position).getPicUrl(),holder.img); return convertView; } //优化 class ViewHolder{ TextView tv,tv1; ImageView img; }
<ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="100dp" /><RelativeLayout android:layout_width="match_parent" android:layout_height="100dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/tv1"> <TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv2" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /></RelativeLayout>
上一个掉
<ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher"/><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好"/> <TextView android:id="@+id/tv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我不好" android:layout_marginTop="8dp"/></LinearLayout>
阅读全文
0 0
- TabLayout
- tablayout
- Tablayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- tablayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- TabLayout
- 在Ubuntu16.04LTS编译LAPACK3.7.1教程,以及遇到的问题
- 第9周【项目3
- MoveWindow() SetWindowPos()的区别于联系
- populating-next-right-pointers-in-each-node Java code
- 【第九周项目1】--二叉树算法库
- tablayout
- 第九周 【项目2
- java Mysql 存储emoji表情解决方案
- RecycleView实现多条目
- web 视频播放插件兼容ie
- scrapy处理个中文本格式HTML,XML,CSV
- MUI H5+常见控件的使用之单选按钮
- 第9周项目2 二叉树遍历的递归算法
- 【Codeforces837G】Functions On The Segments