Teach you to write the news client step by step.
News client 1
:
Generally, the news client uses SlidingMenu + Fragment + ViewPage, which makes the interface effect more elegant and elegant.
Well, we will implement the above effect step by step. First, we will slide the menu effect. Here we introduce a framework library dedicated to the development library for implementing sliding menus. Through this library, we can easily create various sliding effects.
1. Create a new project and introduce the SlidingMenu-master database to our project. The introduction method is shown in.
Note: after the introduction of this library to the original project to delete the android-support-v4.jar shelf package otherwise the compilation will be wrong, because the introduced library already has the shelf package, if the original project is not deleted, a conflict may occur.
2. inherit SlidingFragmentActivity from MainActivity
<Span style = "font-size: 18px;"> public class MainActivity extends SlidingFragmentActivity {private SlidingMenu sm; @ Overridepublic void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setBehindContentView (R. layout. menu); setContentView (R. layout. content); // Fragment1 f = new Fragment1 (); // getSupportFragmentManager (). beginTransaction (). replace (R. id. content, f )//. commit (); sm = getSlidingMenu (); // 2. Set whether the sliding menu is displayed on the LEFT or on the RIGHT. // you can set the LEFT or RIGHT parameters, you can also set left and right LEFT_RIGHTsm.setMode (SlidingMenu. LEFT); // 3 set the remaining width of sm displayed on the content page after the sliding menu is displayed. setBehindOffsetRes (R. dimen. slidingmenu_offset); // 4. Set the shadow of the sliding menu. The shadow must be particularly dark at the beginning and gradually fade down sm. setShadowDrawable (R. drawable. shadow); // 5 sets the shadow width sm. setShadowWidth (R. dimen. shadow_width); // 6. Set the sliding menu range // The first parameter SlidingMenu. TOUCHMODE_FULLSCREEN enables full screen sliding // The second parameter SlidingMenu. TOUCHMODE_MARGIN can only slide at the edge // The third parameter SlidingMenu. TOUCHMODE_NONE cannot slide sm. setTouchModeAbove (sregistringmenu. TOUCHMODE_FULLSCREEN); // create fragmentMenuFragment menuFragment = new MenuFragment (); // obtain the getsuppfrfragmentmanager () of the fragment manager and // start the transaction. beginTransaction () // replace. replace (R. id. menu_layout, menuFragment, "Menu") // submit. commit (); // slide to the right // sm. setSecondaryMenu (R. layout. right_view); // sm. setSecondaryShadowDrawable (R. drawable. shadowright); // RightMenuFragment rightMenuFragment = new RightMenuFragment (); // getSupportFragmentManager (). beginTransaction (). replace (R. id. right_view, // rightMenuFragment ). commit (); HomeFragment homeFragment = new HomeFragment (); getSupportFragmentManager (). beginTransaction (). replace (R. id. content, homeFragment, "home "). commit ();} // method callback public void switchFragment (Fragment f) {getSupportFragmentManager (). beginTransaction (). replace (R. id. content, f ). commit (); // automatically switches sm. toggle () ;}}</span>
MenuFragment. java, which is used to switch the interface
<span style="font-size:18px;">@SuppressLint("NewApi")public class MenuFragment extends Fragment implements OnItemClickListener {private View view;private static final String tag = "MenuFragment";@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);Log.i(tag, "oncreate");}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {Log.i(tag, "onCreateView");view = inflater.inflate(R.layout.list_view, null);return view;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onActivityCreated(savedInstanceState);Log.i(tag, "onActivityCreated");ListView list = (ListView) view.findViewById(R.id.list_view);ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, android.R.id.text1,initdata());list.setAdapter(adapter);list.setOnItemClickListener(this);}public List<String> initdata() {List<String> listdata = new ArrayList<String>();listdata.add("Fragment1");listdata.add("fragment2");listdata.add("Fragment3");return listdata;}@Overridepublic void onItemClick(AdapterView<?> parent, View view, int postion,long id) {Fragment f = null;switch (postion) {case 0:f = new Fragment1();break;case 1:f = new Fragment2();break;case 2:f = new Fragment3();break;default:break;}switchFragment(f);}public void switchFragment(Fragment f) {if (f != null) {if (getActivity() instanceof MainActivity) {((MainActivity) getActivity()).switchFragment(f);}}}}</span>
<span style="font-size:18px;">public class Fragment1 extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubTextView tv = new TextView(getActivity());tv.setText(Fragment1.class.getSimpleName());return tv;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onActivityCreated(savedInstanceState);}}</span>
Interface menu
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/menu_layout"> </FrameLayout></span>
Content
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/content" android:orientation="vertical" > </FrameLayout></span>