package com.example.day04_viewpageindicator;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Window;
import com.viewpagerindicator.TabPageIndicator;
public class MainActivity extends FragmentActivity {
/**
* Tab标题
*/
private static final String[] TITLE = new String[] { "头条", "房产", "另一面",
"女人", "财经", "数码", "情感", "科技" };
/**
* 装fragment的集合
*/
private List<MyFragment> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
TabPageIndicator tabPageIndicator = (TabPageIndicator) findViewById(R.id.tabPageIndicator);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
// 准备fragment
addFragment();
viewPager.setAdapter(new MyFragmentPagerAdapter(
getSupportFragmentManager(), list,TITLE));
// 把viewPager设置给指示器
tabPageIndicator.setViewPager(viewPager);
}
private void addFragment() {
list = new ArrayList<MyFragment>();
for (int i = 0; i < TITLE.length; i++) {
MyFragment myFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("path", TITLE[i] + "的接口地址");
myFragment.setArguments(bundle);
list.add(myFragment);
}}
}
/
package com.example.day04_viewpageindicator;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private List<MyFragment> list;
private String[] title;
public MyFragmentPagerAdapter(FragmentManager fm, List<MyFragment> list, String[] title) {
super(fm);
this.list = list;
this.title = title;
}
@Override
public Fragment getItem(int arg0) {
return list.get(arg0);
}
@Override
public int getCount() {
return list.size();
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
///
package com.example.day04_viewpageindicator;
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.TextView;
public class MyFragment extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = View.inflate(getActivity(), R.layout.fragment, null);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
Bundle bundle = getArguments();
String path = bundle.getString("path");
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(path);
super.onActivityCreated(savedInstanceState);
}
}