标签:
1.activity_main_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="mydemo.mycom.demo2.FragmentMain"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tab1" android:text="社会新闻" android:layout_width="0dip" android:layout_weight="1" android:gravity="center" android:layout_height="wrap_content" /> <TextView android:id="@+id/tab2" android:text="生活新闻" android:layout_width="0dip" android:layout_weight="1" android:gravity="center" android:layout_height="wrap_content" /> <TextView android:id="@+id/tab3" android:text="军事新闻" android:layout_width="0dip" android:layout_weight="1" android:gravity="center" android:layout_height="wrap_content" /> <TextView android:id="@+id/tab4" android:text="娱乐新闻" android:layout_width="0dip" android:layout_weight="1" android:gravity="center" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/lv" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout> </LinearLayout>
2.fragment1.xml
<?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"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="社会新闻"/> </LinearLayout>
3.fragment2.xml
<?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"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="生活新闻"/> </LinearLayout>
4.fragment3.xml
<?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"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="军事新闻"/> </LinearLayout>
5.fragment4.xml
<?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"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="娱乐新闻"/> </LinearLayout>
6.FragmentMain.java
package mydemo.mycom.demo2; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class FragmentMain extends FragmentActivity implements View.OnClickListener { private TextView tab1; private TextView tab2; private TextView tab3; private TextView tab4; private FragmentManager fm; private FragmentTransaction ft; private List<Fragment> list = new ArrayList<Fragment>(); private List<TextView> tvList = new ArrayList<TextView>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_main); tab1 = (TextView)findViewById(R.id.tab1); tab2 = (TextView)findViewById(R.id.tab2); tab3 = (TextView)findViewById(R.id.tab3); tab4 = (TextView)findViewById(R.id.tab4); tvList.add(tab1); tvList.add(tab2); tvList.add(tab3); tvList.add(tab4); tab1.setOnClickListener(this); tab2.setOnClickListener(this); tab3.setOnClickListener(this); tab4.setOnClickListener(this); list.add(new Fragment1()); list.add(new Fragment2()); list.add(new Fragment3()); list.add(new Fragment4()); fm = getSupportFragmentManager(); ft = fm.beginTransaction(); tvList.get(0).setTextColor(getResources().getColor(R.color.detail_background)); tvList.get(0).setBackgroundColor(getResources().getColor(R.color.default_background)); ft.replace(R.id.lv,new Fragment1()); ft.commit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_fragment_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View view) { ft = fm.beginTransaction(); switch (view.getId()) { case R.id.tab1: //ft.hide(list.get(0)).hide(list.get(1)).hide(list.get(2)).show(list.get(0)); ft.replace(R.id.lv,new Fragment1()); setStyle(0); break; case R.id.tab2: //ft.hide(list.get(0)).hide(list.get(1)).hide(list.get(2)).show(list.get(1)); ft.replace(R.id.lv,new Fragment2()); setStyle(1); break; case R.id.tab3: //ft.hide(list.get(0)).hide(list.get(1)).hide(list.get(2)).show(list.get(3)); ft.replace(R.id.lv,new Fragment3()); setStyle(2); break; case R.id.tab4: //ft.hide(list.get(0)).hide(list.get(1)).hide(list.get(2)).show(list.get(4)); ft.replace(R.id.lv,new Fragment4()); setStyle(3); break; } ft.commit(); } private void setStyle(int i) { for (TextView tv:tvList) { tv.setBackgroundColor(getResources().getColor(R.color.default_color)); tv.setTextColor(getResources().getColor(R.color.black)); } tvList.get(i).setTextColor(getResources().getColor(R.color.detail_background)); tvList.get(i).setBackgroundColor(getResources().getColor(R.color.default_background)); } }
7.Fragment1.java
package mydemo.mycom.demo2; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2015/5/25. */ public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1,null); } }
8.Fragment2.java
package mydemo.mycom.demo2; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2015/5/25. */ public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2,null); } }
9.Fragment3.java
package mydemo.mycom.demo2; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2015/5/25. */ public class Fragment3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment3,null); } }
10.Fragment4.java
package mydemo.mycom.demo2; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2015/5/25. */ public class Fragment4 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment4,null); } }
标签:
原文地址:http://www.cnblogs.com/zoro-zero/p/4682112.html