package com.example.myapplication.fragment;
import android.support.v4.app.Fragment;import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.myapplication.R;
/**
* Created by xia on 2017/7/27.
*/
public class girlFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.girllayout,container, false);
return view;
}
}package com.example.myapplication;
import android.support.v4.app.Fragment;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentPagerAdapter;
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.View;
import com.example.myapplication.fragment.ButlreFragment;
import com.example.myapplication.fragment.girlFragment;
import com.example.myapplication.fragment.userFragment;
import com.example.myapplication.fragment.weixinFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TabLayout mTabLayout;
private ViewPager mViewPager;
private List<String> tab;
private List<Fragment> fragments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
mTabLayout.setupWithViewPager(mViewPager);
}
private void initData() {
tab = new ArrayList<>();
tab.add("智能管家");
tab.add("微信精选");
tab.add("美女相册");
tab.add("个人中心");
fragments = new ArrayList<>();
fragments.add(new ButlreFragment());
fragments.add(new weixinFragment());
fragments.add(new girlFragment());
fragments.add(new userFragment());
}
private void initView() {
mTabLayout = (TabLayout) findViewById(R.id.mTabLayout);
mViewPager = (ViewPager) findViewById(R.id.mViewPager);
//预加载
mViewPager.setOffscreenPageLimit(fragments.size());
mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return 4;
}
//设置标题
@Override
public CharSequence getPageTitle(int position) {
return tab.get(position);
}
});
}
}<?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"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美女相册"/>
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/mTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextColor="@android:color/white"
/>
<android.support.v4.view.ViewPager
android:id="@+id/mViewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>找了好久没找到错误 求大神解答 不胜感激!!!
添加回答
举报
0/150
提交
取消