MainPost.java 20181119
package com.example.hasee.myapplication;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* setType 0:post
* 1:vote
* 2:live
* 3:photo
*/
public class MainActivity extends FragmentActivity {
DrawerLayout mainlayout;
private RadioButton black;
private Spinner spinner;
private Button back;
private FragmentPost fragmentPost;
private FragmentVote fragmentVote;
private FragmentLive fragmentLive;
private FragmentPhoto fragmentPhoto;
private TextView topic;
private int ENTRY_CODE=0; //入口,显示不同发帖
private EditText ed;
android.support.v4.app.FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activty_main);
spinner = findViewById(R.id.postType);
final List<String> list = getDatas();
fm=getSupportFragmentManager();
if(0==ENTRY_CODE) {
setType(0);
} else {
setType(1);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
spinner.setAdapter(adapter);
spinner.setSelection(0, true);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String data = (String) spinner.getItemAtPosition(position);
if (0 == position) {
setType(0);
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
} else if(1==position){
setType(1);
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}else if(2==position){
setType(2);
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}else if(3==position){
setType(3);
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
/**
* 点击侧滑效果
*/
mainlayout=findViewById(R.id.mainlayout);
topic=findViewById(R.id.topic);
topic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mainlayout.openDrawer(GravityCompat.END);
}
});
back=findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mainlayout.closeDrawer(GravityCompat.END);
}
});
}
@Override
protected void onStart() {
super.onStart();
ed=findViewById(R.id.postText);
black=findViewById(R.id.black);
black.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.setTextColor(getResources().getColor(R.color.b));
}
});
}
public List<String> getDatas() {
List<String> list = new ArrayList<>();
list.add("发布帖子");
list.add("发布投票");
list.add("发布直播");
list.add("发布图片");
return list;
}
private void setType(int index){
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
hideFragment(ft);
switch (index) {
case 0:
if(fragmentPost==null){
fragmentPost = new FragmentPost();
ft.add(R.id.fragment_container, fragmentPost);
}else{
ft.show(fragmentPost);
}
break;
case 1:
if(fragmentVote==null){
fragmentVote = new FragmentVote();
ft.add(R.id.fragment_container, fragmentVote);
}
ft.show(fragmentVote);
break;
case 2:
if(fragmentLive==null){
fragmentLive = new FragmentLive();
ft.add(R.id.fragment_container, fragmentLive);
}
ft.show(fragmentLive);
break;
case 3:
if(fragmentPhoto==null){
fragmentPhoto = new FragmentPhoto();
ft.add(R.id.fragment_container, fragmentPhoto);
}
ft.show(fragmentPhoto);
break;
}
ft.commit();
}
private void hideFragment(FragmentTransaction ft){
if(fragmentPost!=null){
ft.hide(fragmentPost);
}
if(fragmentVote!=null){
ft.hide(fragmentVote);
}
if(fragmentLive!=null){
ft.hide(fragmentLive);
}
if(fragmentPhoto!=null){
ft.hide(fragmentPhoto);
}
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/headBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"> <!--头部-->
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="@mipmap/ic_launcher" />
<Spinner
android:id="@+id/postType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"></Spinner>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="8dp"
android:background="@color/c" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/headBar"
android:orientation="vertical">
<TextView
android:id="@+id/topic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="选择模块>"
android:textSize="22dp" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/topic"></FrameLayout>
</RelativeLayout>
<LinearLayout
android:background="#383737"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/foot_bar">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="@+id/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:button="@drawable/radio_selector"
android:layout_weight="1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/foot_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/f">
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@mipmap/ic_launcher" />
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@mipmap/ic_launcher" />
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@mipmap/ic_launcher" />
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@mipmap/ic_launcher" />
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@mipmap/ic_launcher" />
<ImageButton
android:id="@+id/color_select"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@mipmap/ic_launcher" />
</LinearLayout>
</RelativeLayout>
<include
android:id="@+id/slide"
layout="@layout/slide" />
</android.support.v4.widget.DrawerLayout>
slide
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#ffffff"
android:orientation="vertical">
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:text="hhh"
android:textSize="200dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:text="hhh"
android:textSize="200dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:text="hhh"
android:textSize="200dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>