导入依赖库:
compile 'com.android.support:design:25.3.1'
1.fg_content_demo2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="123" android:gravity="center" android:id="@+id/fg_content_demo2_textview"/> </LinearLayout>
2.Demo2Fragment
import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.GridView; import android.widget.TextView; import com.example.mac.mainapplication.R; import com.example.mac.mainapplication.adapter.MainAdapter; import org.w3c.dom.Text; /** * Created by mac on 17/6/16. */ public class Demo2Fragment extends Fragment { private TextView textView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fg_content_demo2, container, false); // tv_content = (TextView) view.findViewById(R.id.tv_content); // String text = getArguments().getString("text"); // tv_content.setText(text); textView = (TextView)view.findViewById(R.id.fg_content_demo2_textview); String text = getArguments().getString("text"); textView.setText(text); return view; } // fg_content_demo2_textview }
Demo2Adaper代码
public class Demo2Adapter extends FragmentPagerAdapter { private final String[] titles; private Context context; private List<Fragment> fragments; public Demo2Adapter(List<Fragment> fragments, String[] titles, FragmentManager fm, Context context) { super(fm); this.context = context; this.fragments = fragments; this.titles = titles; } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return titles.length; } @Override public CharSequence getPageTitle(int position) { return titles[position]; } }
3.main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#d33c3c" android:gravity="center_vertical" android:orientation="horizontal"> <android.support.design.widget.TabLayout android:id="@+id/demo2_tablayout" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" app:tabIndicatorColor="@color/colorPrimary" app:tabMode="scrollable"> </android.support.design.widget.TabLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" android:src="@drawable/ic_launcher"/> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/demo2_viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </android.support.v4.view.ViewPager> </LinearLayout>
4.main代码:
private LinearLayout backLayout; private String[]titles = {"项目1","项目2","项目4","项目5","项目6","项目7","项目8","项目9","项目10","项目11"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo2); backLayout = (LinearLayout)findViewById(R.id.default_nav_left_layout); backLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onBackPressed(); } }); ViewPager viewPager = (ViewPager) findViewById(R.id.demo2_viewPager); List<Fragment> fragments = new ArrayList<>(); for (int i = 0; i < titles.length; i++) { Fragment fragment = new Demo2Fragment(); Bundle bundle = new Bundle(); bundle.putString("text",titles[i]); fragment.setArguments(bundle); fragments.add(fragment); } viewPager.setAdapter(new Demo2Adapter(fragments, titles, getSupportFragmentManager(), this)); // 初始化 TabLayout tablayout = (TabLayout) findViewById(R.id.demo2_tablayout); // 将ViewPager和TabLayout绑定 tablayout.setupWithViewPager(viewPager); // 设置tab文本的没有选中(第一个参数)和选中(第二个参数)的颜色 tablayout.setTabTextColors(getResources().getColor(R.color.colorPrimaryDark), Color.WHITE); }