//main.xml布局
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwei.tablayoutdemo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/colorPrimary"
app:tabMode="scrollable"
app:tabSelectedTextColor="#cb0b0b"
app:tabTextAppearance="@android:style/TextAppearance.Holo.Small"
app:tabIndicatorHeight="0dp"
app:tabContentStart="5dp"
app:textAllCaps="true"
app:tabTextColor="#111" />
<android.support.v4.view.ViewPager
android:id="@+id/vp_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:id="@+id/lvLeft"
/>
</android.support.v4.widget.DrawerLayout>
//pager_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
ptr:ptrAnimationStyle="rotate"
ptr:ptrHeaderTextColor="#ffffff"
ptr:ptrHeaderSubTextColor="#00ffff"
ptr:ptrHeaderBackground="@null"
ptr:ptrDrawable="@mipmap/ic_launcher"/>
</LinearLayout>
//dot_layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:id="@+id/scrollView"
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_gravity="bottom"
android:background="@drawable/dot_normal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
//图片轮播跳转.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_success"
android:orientation="vertical"
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.bwei.tablayoutdemo.SuccessActivity">
<android.support.v4.view.ViewPager
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager" />
<LinearLayout
android:id="@+id/ll"
android:orientation="horizontal"
android:gravity="center"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="20dp"/>
<Button
android:onClick="btt"
android:id="@+id/bt1"
android:text="进入系统"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
//list_item02
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:id="@+id/imageView"
/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
自己写方法
//ChannelFragment上拉下拉
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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.util.ArrayList;
/**
* Created by Administrator on 2017/9/14.
*/
public class ChannelFragment extends Fragment{
private String name;
private String news_url;
private PullToRefreshListView pullToRefreshListView;
private MyPullToListViewAdapter adapter;
private ArrayList<Result> results;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
name = (String) bundle.get("name");
news_url = (String) bundle.get("url");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pager_item, null);
pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.pull_refresh_list);
//刚进来,网络请求数据,配置数据
refreshData();
//配置刷新,加载
pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
refreshData();
}
});
pullToRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
loadMoreDate();
}
});
return view;
}
public void refreshData(){
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
String json = new NetWorkUtils().getJsonByUrlConnection(news_url);
return json;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//解析json
SuperClass superClass = new Gson().fromJson(s, SuperClass.class);
results = superClass.getResults();
adapter = new MyPullToListViewAdapter(getActivity(), results);
pullToRefreshListView.setAdapter(adapter);
pullToRefreshListView.onRefreshComplete();
}
}.execute();
}
public void loadMoreDate(){
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
String json = new NetWorkUtils().getJsonByUrlConnection(news_url);
return json;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//解析json
SuperClass superClass = new Gson().fromJson(s, SuperClass.class);
results.addAll(superClass.getResults());
adapter.notifyDataSetChanged();
}
}.execute();
}
}
///////////MainActivity代码
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TabLayout mTablayout;
private ViewPager mViewPager;
private ListView lvLeft;
private DrawerLayout drawerLayout;
private String[] menu = {"收藏","设置","钱包","反馈"};
private String[] channels = {"推荐","热点","体育","娱乐","社会","汽车","教育","财经","科技","游戏"};
private String[] urlS = {
"http://gank.io/api/data/Android/10/1",
"http://gank.io/api/data/iOS/10/1",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1"
};
private List<String> mTitleList = new ArrayList<>();//页卡标题集合
// private ChannelFragment view1, view2, view3, view4, view5,view6, view7, view8, view9, view10;//页卡视图
private List<ChannelFragment> mViewList = new ArrayList<>();//页卡视图集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.activity_main);
mViewPager = (ViewPager) findViewById(R.id.vp_view);
mTablayout = (TabLayout) findViewById(R.id.tabs);
lvLeft = (ListView) findViewById(R.id.lvLeft);
for(int i=0;i<channels.length;i++){
//创建栏目的fragment
ChannelFragment fragment = new ChannelFragment();
Bundle b = new Bundle();
b.putString("name", channels[i]);//传递名字
b.putString("url", urlS[i]);
fragment.setArguments(b);
//收集fragment
mViewList.add(fragment);
//给tablayout添加tab选项卡
mTablayout.addTab(mTablayout.newTab().setText(channels[i]));//添加tab选项卡
}
FragmentManager fm = getSupportFragmentManager();
MyFragmentPagerAdapter mAdapter = new MyFragmentPagerAdapter(fm, mViewList);
mViewPager.setAdapter(mAdapter);//给ViewPager设fsf置适配器
mTablayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
mTablayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器
// int count = mTablayout.getTabCount();
//
// for (int j=0;j<count;j++){
// TabLayout.Tab tab= mTablayout.getTabAt(j);
// tab.setIcon(R.mipmap.ic_launcher);
// }
// mTablayout.addTab(mTablayout.newTab().setText(channels[0]).setIcon(R.mipmap.ic_launcher));
// mTablayout.getTabAt(3).select();
//给侧滑中的listview配置数据
initDataForListViewLeft();
}
private void initDataForListViewLeft() {
//
ArrayAdapter<String> lvLeftAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
lvLeft.setAdapter(lvLeftAdapter);
//添加监听
lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,menu[position],Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(lvLeft);
//做相关的业务,,比如:让viewpager联动滑动到对应的pager
// mViewPager.setCurrentItem(position);
}
});
}
class MyFragmentPagerAdapter extends FragmentPagerAdapter{
private List<ChannelFragment> mViewList;
public MyFragmentPagerAdapter(FragmentManager fm, List<ChannelFragment> mViewList) {
super(fm);
this.mViewList = mViewList;
}
@Override
public Fragment getItem(int position) {
return mViewList.get(position);
}
@Override
public int getCount() {
return mViewList.size();
}
@Override
public String getPageTitle(int position) {
return channels[position];
}
}
// //ViewPager适配器
// class MyPagerAdapter extends PagerAdapter {
// private List<ChannelFragment> mViewList;
//
// public MyPagerAdapter(List<ChannelFragment> mViewList) {
// this.mViewList = mViewList;
// }
//
// @Override
// public int getCount() {
// return mViewList.size();//页卡数
// }
//
// @Override
// public boolean isViewFromObject(View view, Object object) {
// return view == object;//官方推荐写法
// }
//
// @Override
// public Object instantiateItem(ViewGroup container, int position) {
// container.addView(mViewList.get(position));//添加页卡
// return mViewList.get(position);
// }
//
// @Override
// public void destroyItem(ViewGroup container, int position, Object object) {
// container.removeView(mViewList.get(position));//删除页卡
// }
//
// @Override
// public CharSequence getPageTitle(int position) {
// return mTitleList.get(position);//页卡标题
// }
//
// }
}
//MyPullToListViewAdapter适配器
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.ArrayList;
/**
* Created by Administrator on 2017/9/20.
*/
public class MyPullToListViewAdapter extends BaseAdapter {
private Context context;
private ArrayList<Result> results;
public MyPullToListViewAdapter(Context context, ArrayList<Result> results) {
this.context = context;
this.results = results;
}
@Override
public int getCount() {
return results.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
//多条目配置
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
Result result = results.get(position);
if(result.getUrl() !="" && result.getUrl()!= null){//有图片,用有图片的布局
return 1;
}else{//没有图片,用没有图片的布局
return 0;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder0 holder0;
ViewHolder1 holder1;
int i = getItemViewType(position);
if(i==0){
if(convertView == null){
holder0 = new ViewHolder0();
convertView = View.inflate(context, R.layout.list_item0,null);
holder0.who = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder0);
}else{
holder0 = (ViewHolder0) convertView.getTag();
}
Result r = results.get(position);
holder0.who.setText(r.getWho());
}else if(i==1){
if(convertView == null){
holder1 = new ViewHolder1();
convertView = View.inflate(context, R.layout.list_item1,null);
holder1.who = (TextView) convertView.findViewById(R.id.textView);
holder1.img = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder1);
}else{
holder1 = (ViewHolder1) convertView.getTag();
}
Result r = results.get(position);
holder1.who.setText(r.getWho());
ImageLoader.getInstance().displayImage(r.getUrl(), holder1.img);
}
return convertView;
}
class ViewHolder0{
TextView who;
}
class ViewHolder1{
TextView who;
ImageView img;
}
}
//图片轮播跳转SuccessActivity
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.os.Handler;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class SuccessActivity extends AppCompatActivity {
private Button bt1;
private ViewPager viewPager;
private Button bt;
private LinearLayout layout;
private final static String JSONURL = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10";
private List<ImageView> imgs = new ArrayList<ImageView>();//图片的集合
private List<View> dots = new ArrayList<View>(); //小圆点集合
private int i = 0;
private int dot = 0;
private List<SuperBean.NewslistBean> newslist;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
viewPager.setCurrentItem(i);
dots.get(dot).setBackgroundResource(R.drawable.dot_normal);
dots.get(i%imgs.size()).setBackgroundResource(R.drawable.dot_foucused);
dot=i%imgs.size();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_success);
bt1 = (Button) findViewById(R.id.bt1);
//异步网络请求
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
String jsonByUrlConnection = new NetWorkUtils().getJsonByUrlConnection(JSONURL);
return jsonByUrlConnection;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SuperBean superClass = new Gson().fromJson(s, SuperBean.class);
newslist = superClass.getNewslist();
init();//初始化方法
}
}.execute();
}
//初始化方法
private void init() {
viewPager = (ViewPager) findViewById(R.id.viewPager);
layout = (LinearLayout) findViewById(R.id.ll);
getImgs();//获取图片
getDots();//获取小圆点
//viewPager适配器
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int i = position % imgs.size();
ImageView imageView = imgs.get(i);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
});
//设置初始化位置
i = imgs.size()*10;
//得到位置
viewPager.setCurrentItem(i);
//设置第一个圆点为true
dots.get(0).setBackgroundResource(R.drawable.dot_foucused);
//timer自动轮播
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
i = viewPager.getCurrentItem() + 1;
handler.sendEmptyMessage(i);
}
},1,2000);
}
//图片的方法
private void getImgs() {
for(int i = 0;i<newslist.size();i++){
String picUrl = newslist.get(i).getPicUrl();
ImageView img = new ImageView(this);
img.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(picUrl,img);
imgs.add(img);
}
}
//小圆点的方法
private void getDots() {
for(int i=0;i<imgs.size();i++){
View view = LayoutInflater.from(this).inflate(R.layout.dot_layout,null);
View viewById = view.findViewById(R.id.scrollView);
dots.add(viewById);
layout.addView(view);
}
}
public void btt(View v){
Intent intent = new Intent(SuccessActivity.this,MainActivity .class);
startActivity(intent);
}
}