自用,Android POST
package com.example.hasee.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentLive extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = View.inflate(getActivity(),R.layout.activty_live, null);
return view;
}
}
package com.example.hasee.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentPhoto extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = View.inflate(getActivity(),R.layout.activty_photo, null);
return view;
}
}
package com.example.hasee.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentPost extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = View.inflate(getActivity(),R.layout.activity_post,null);
return view;
}
}
package com.example.hasee.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentVote extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = View.inflate(getActivity(),R.layout.activty_vote, null);
return view;
}
}
package com.example.hasee.myapplication;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
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 {
private Spinner spinner;
private FragmentPost fragmentPost;
private FragmentVote fragmentVote;
private FragmentLive fragmentLive;
private FragmentPhoto fragmentPhoto;
private int ENTRY_CODE=0; //入口,显示不同发帖
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) {
}
});
}
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_post.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<EditText
android:id="@+id/postTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGray1"
android:hint="输入帖子标题..."
android:padding="8dp"
android:textSize="22dp" />
<EditText
android:id="@+id/postText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorWhite"
android:gravity="left"
android:hint="输入正文内容..."
android:padding="8dp"
android:textSize="22dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<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/colorBlue" />
</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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorGray2">
<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:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@mipmap/ic_launcher" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>