ViewPage+Fragment+TabLayout<综合技能>
欢迎页面布局
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPage"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.viewpager.widget.ViewPager>
<RadioGroup
android:id="@+id/radioGroup"
android:orientation="horizontal"
android:layout_marginBottom="80dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal">
<RadioButton
android:id="@+id/one"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="○"
android:textColor="@drawable/layout_item_button"
android:textSize="30dp"
android:gravity="center" />
<RadioButton
android:id="@+id/two"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="○"
android:textColor="@drawable/layout_item_button"
android:textSize="30dp"
android:gravity="center"/>
<RadioButton
android:id="@+id/three"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="○"
android:textColor="@drawable/layout_item_button"
android:textSize="30dp"
android:gravity="center"/>
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="立即跳转"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:visibility="gone"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="倒计时"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:visibility="gone"
/>
功能页布局
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout_zhu"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
app:tabTextColor="#B4B4B4"
app:tabSelectedTextColor="#000"
app:tabIndicatorColor="#FF0000"
app:tabIndicatorHeight="5dp"
app:tabMode="fixed">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPage_zhu"
android:layout_weight="6"
android:layout_width="match_parent"
android:layout_height="0dp">
</androidx.viewpager.widget.ViewPager>
<RadioGroup
android:orientation="horizontal"
android:id="@+id/radioGroup_zhu"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<RadioButton
android:id="@+id/Button1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@drawable/layout_item_button2"
android:text="联系人"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center"
android:button="@null"
/>
<RadioButton
android:id="@+id/Button2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@drawable/layout_item_button2"
android:text="动态"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center"
android:button="@null"
/>
<RadioButton
android:id="@+id/Button3"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@drawable/layout_item_button2"
android:text="设置"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center"
android:button="@null"
/>
<RadioButton
android:id="@+id/Button4"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@drawable/layout_item_button2"
android:text="我的"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center"
android:button="@null"
/>
</RadioGroup>
</LinearLayout>
ListView 页面布局
<ImageView
android:id="@+id/icon_id"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/title_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textSize="20sp"
android:layout_toRightOf="@+id/icon_id"
android:layout_marginTop="40dp"
android:layout_marginLeft="20dp"/>
Fragment 主页布局
<LinearLayout
android:id="@+id/line"
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="@+id/buttonWrite"
android:layout_weight="1"
android:text="存入SD卡数据"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonRead"
android:layout_weight="1"
android:text="读取SD卡数据"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonTime"
android:layout_weight="1"
android:text="获取当前日期"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity 引导页
private RadioGroup radioGroup;
private RadioButton one;
private RadioButton two;
private RadioButton three;
private ViewPager viewPage;
private Button button;
private TextView textView;
private Timer timer;
private Timer timer1;
private List<Fragment> list = new ArrayList<>();
private int index = 0;
private int num = 5;
private Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
viewPage.setCurrentItem(index);
index++;
if (index == list.size()) {
index = 0;
timer1 = new Timer();
timer1.schedule(new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 2;
handler.sendMessage(message);
}
}, 0, 1000);
}
break;
case 2:
textView.setText("倒计时:"+(num--));
if (num == 0){
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
timer1.cancel();
SharedPreferences flag = getSharedPreferences("flag", MODE_PRIVATE);
SharedPreferences.Editor edit = flag.edit();
edit.putBoolean("isCome",true);
edit.commit();
finish();
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences flag = getSharedPreferences("flag", MODE_PRIVATE);
boolean isCome = flag.getBoolean("isCome", false);
if (isCome){
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
this.finish();
}
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
viewPage = (ViewPager) findViewById(R.id.viewPage);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(this);
one = (RadioButton) findViewById(R.id.one);
one.setChecked(true);
one.setOnClickListener(this);
two = (RadioButton) findViewById(R.id.two);
two.setOnClickListener(this);
three = (RadioButton) findViewById(R.id.three);
three.setOnClickListener(this);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnClickListener(this);
for (int i = 1; i < 4; i++) {
ContentFragment contentFragment = new ContentFragment();
Bundle bundle = new Bundle();
bundle.putString("key", i + "");
contentFragment.setArguments(bundle);
list.add(contentFragment);
}
viewPage.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return list.size();
}
@NonNull
@Override
public Fragment getItem(int position) {
return list.get(position);
}
});
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
}, 0, 1000);
viewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0){
one.setChecked(true);
}else if (position == 1){
two.setChecked(true);
}else {
three.setChecked(true);
}
if (position == list.size() - 1) {
button.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
timer.cancel();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
timer1.cancel();
startActivity(intent);
SharedPreferences flag = getSharedPreferences("flag", MODE_PRIVATE);
SharedPreferences.Editor edit = flag.edit();
edit.putBoolean("isCome",true);
edit.commit();
this.finish();
break;
}
}
Main2Activity 主页功能
private ViewPager viewPage_zhu;
private RadioGroup radioGroup_zhu;
private List<Fragment> list = new ArrayList<>();
private List<String> listTitle = new ArrayList<>();
private RadioButton Button1;
private RadioButton Button2;
private RadioButton Button3;
private RadioButton Button4;
private TabLayout tabLayout_zhu;
private Fragment_item fragment_item;
private Fragment_item2 fragment_item2;;
private Fragment_item3 fragment_item3;;
private Fragment_item4 fragment_item4;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
}
private void initView() {
Button1 = (RadioButton) findViewById(R.id.Button1);
Button1.setChecked(true);
Button2 = (RadioButton) findViewById(R.id.Button2);
Button3 = (RadioButton) findViewById(R.id.Button3);
Button4 = (RadioButton) findViewById(R.id.Button4);
viewPage_zhu = (ViewPager) findViewById(R.id.viewPage_zhu);
radioGroup_zhu = (RadioGroup) findViewById(R.id.radioGroup_zhu);
tabLayout_zhu = (TabLayout) findViewById(R.id.tabLayout_zhu);
tabLayout_zhu.setupWithViewPager(viewPage_zhu);
fragment_item = new Fragment_item();
fragment_item2 = new Fragment_item2();
fragment_item3 = new Fragment_item3();
fragment_item4 = new Fragment_item4();
list.add(fragment_item);
list.add(fragment_item2);
list.add(fragment_item3);
list.add(fragment_item4);
for (int i = 0; i < 4; i++) {
listTitle.add("标题" + (i + 1));
}
viewPage_zhu.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@NonNull
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return listTitle.get(position);
}
});
viewPage_zhu.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
Button1.setChecked(true);
} else if (position == 1) {
Button2.setChecked(true);
} else if (position == 2) {
Button3.setChecked(true);
} else {
Button4.setChecked(true);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
ContentFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_content, container, false);
textView = inflate.findViewById(R.id.textView_Fragment);
Bundle bundle = getArguments();
String key = bundle.getString("key");
textView.setText(key);
return inflate;
}
Fragment_item
public class Fragment_item extends Fragment {
private Button buttonWrite;
private Button buttonRead;
private Button buttonTime;
SharedPreferences.Editor editor;
SharedPreferences sharedPreferences;
String s;
public Fragment_item() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_fragment_item, container, false);
buttonRead = inflate.findViewById(R.id.buttonRead);
buttonWrite = inflate.findViewById(R.id.buttonWrite);
buttonTime = inflate.findViewById(R.id.buttonTime);
buttonWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OkGo.<String>get("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&act=column&ctype=4").execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
Toast.makeText(getActivity(), response.body(), Toast.LENGTH_SHORT).show();
sharedPreferences = getActivity().getSharedPreferences("one", MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putString("json", response.body());
editor.commit();
}
});
}
});
buttonRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = sharedPreferences.getString("json","");
Toast.makeText(getActivity(), string, Toast.LENGTH_SHORT).show();
}
});
}
Fragment_item2
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_fragment_item2, container, false);
listView = inflate.findViewById(R.id.listView_item2);
new MyAsyncTask(getActivity(),listView).execute("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&act=column&ctype=4");
return inflate;
}