我的项目目录如下:
直接上核心代码
MainActivity
import android.support.v4.app.FragmentTransaction;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.fragmentlayout.Fragment.MainFragment;
import com.example.fragmentlayout.Fragment.MineFragment;
import com.example.fragmentlayout.Fragment.MoreFragment;
public class MainActivity extends FragmentActivity implements View.OnClickListener {
private TextView main_textview,more_textview,mine_textview;
private ImageView main_imageview,more_imageview,mine_imageview;
private RelativeLayout main_layout,more_layout,mine_layout;
private Fragment mainFragment,moreFragment,mineFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//删除自带标题栏(如果继承AppCompatActivity,不起效果,所以继承FragmentActivity)
setContentView(R.layout.activity_main);
initView();
setSelect(0);//显示第一个Fragment (0代表mainFragment,1代表moreFragment,2代表mineFragment);
}
private void initView() {
main_imageview=(ImageView)findViewById(R.id.main_iamgeview);
more_imageview=(ImageView)findViewById(R.id.more_iamgeview);
mine_imageview=(ImageView)findViewById(R.id.mine_iamgeview);
main_textview=(TextView)findViewById(R.id.main_textview);
more_textview=(TextView)findViewById(R.id.more_textview);
mine_textview=(TextView)findViewById(R.id.mine_textview);
main_layout=(RelativeLayout)findViewById(R.id.main_layout);
more_layout=(RelativeLayout)findViewById(R.id.more_layout);
mine_layout=(RelativeLayout)findViewById(R.id.mine_layout);
main_layout.setOnClickListener(this);
more_layout.setOnClickListener(this);
mine_layout.setOnClickListener(this);
}
private void setSelect(int i) {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction transaction=fm.beginTransaction();
hideFragment(transaction);
switch (i){
case 0:
if(mainFragment==null){
mainFragment=new MainFragment();
transaction.add(R.id.fragment,mainFragment);
}else {
transaction.show(mainFragment);
}
break;
case 1:
if(moreFragment==null){
moreFragment=new MoreFragment();
transaction.add(R.id.fragment,moreFragment);
}else {
transaction.show(moreFragment);
}
break;
case 2:
if(mineFragment==null){
mineFragment=new MineFragment();
transaction.add(R.id.fragment,mineFragment);
}else {
transaction.show(mineFragment);
}
break;
}
transaction.commit();//提交事务
}
private void hideFragment(FragmentTransaction transaction) {
if(mainFragment!=null){
transaction.hide(mainFragment);
}
if(moreFragment!=null){
transaction.hide(moreFragment);
}
if(mineFragment!=null){
transaction.hide(mineFragment);
}
}
@Override
public void onClick(View v) {
initImageText();
switch (v.getId()){
case R.id.main_layout:
setSelect(0);
main_imageview.setImageResource(R.drawable.main2);
main_textview.setTextColor(Color.parseColor("#707070"));
break;
case R.id.more_layout:
setSelect(1);
more_imageview.setImageResource(R.drawable.more2);
more_textview.setTextColor(Color.parseColor("#707070"));
break;
case R.id.mine_layout:
setSelect(2);
mine_imageview.setImageResource(R.drawable.my2);
mine_textview.setTextColor(Color.parseColor("#707070"));
break;
}
}
//初始化图片和文字(未点击自定义的Tab下样式)
private void initImageText() {
main_imageview.setImageResource(R.drawable.main1);
more_imageview.setImageResource(R.drawable.more1);
mine_imageview.setImageResource(R.drawable.my1);
main_textview.setTextColor(Color.parseColor("#dfdfdf"));
more_textview.setTextColor(Color.parseColor("#dfdfdf"));
mine_textview.setTextColor(Color.parseColor("#dfdfdf"));
}
}
MoreFragment
重点获取Fragmment子管理器用到getChildFragment()
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.fragmentlayout.ChildFragment.OneChild;
import com.example.fragmentlayout.ChildFragment.TwoChild;
import com.example.fragmentlayout.R;
import java.util.ArrayList;
import java.util.List;
public class MoreFragment extends Fragment implements View.OnClickListener {
private ViewPager viewPager;
private FragmentPagerAdapter fragmentPagerAdapter;
private RelativeLayout one_layout,two_layout;
private Fragment oneChildFragment,twoChildFrament;
private List mFragmentList;
private TextView oneTextView,twoTextView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.more_fragment, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
setSelect(0);//0代表第一Fragemnt,1代表第二个Fragment
}
private void initView() {
viewPager=(ViewPager)getActivity().findViewById(R.id.viewpager);
one_layout=(RelativeLayout)getActivity().findViewById(R.id.one_layout);
two_layout=(RelativeLayout)getActivity().findViewById(R.id.two_layout);
oneTextView=(TextView)getActivity().findViewById(R.id.one_textView);
twoTextView=(TextView)getActivity().findViewById(R.id.two_textView);
one_layout.setOnClickListener(this);
two_layout.setOnClickListener(this);
mFragmentList=new ArrayList();
oneChildFragment=new OneChild();
twoChildFrament=new TwoChild();
mFragmentList.add(oneChildFragment);
mFragmentList.add(twoChildFrament);
fragmentPagerAdapter=new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
};
viewPager.setAdapter(fragmentPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
setLayout();
switch (position){
case 0:
one_layout.setBackgroundColor(Color.parseColor("#ffffff"));
oneTextView.setTextColor(Color.parseColor("#000000"));
setSelect(0);
break;
case 1:
two_layout.setBackgroundColor(Color.parseColor("#ffffff"));
twoTextView.setTextColor(Color.parseColor("#000000"));
setSelect(1);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.one_layout:
oneTextView.setTextColor(Color.parseColor("#000000"));
setSelect(0);
break;
case R.id.two_layout:
setSelect(1);
twoTextView.setTextColor(Color.parseColor("#000000"));
break;
}
}
private void setSelect(int i) {
switch (i){
case 0:
viewPager.setCurrentItem(0);
break;
case 1:
viewPager.setCurrentItem(1);
break;
}
}
//初始化Layout和文字
private void setLayout(){
oneTextView.setTextColor(Color.parseColor("#dfdfdf"));
twoTextView.setTextColor(Color.parseColor("#dfdfdf"));
}
}
项目地址:[http://download.csdn.net/download/qq_37238649/10169652
效果图