Source code introduction: Based on Baidu data, a very useful news client. Source code effect: Source code snippet: - package com.linpeng.baidunewssearch;
-
- import java.lang.reflect.Method;
-
- import com.linpeng.baidunewssearch.R;
- import com.linpeng.fragment.FragmentNews;
- import com.linpeng.fragment.FragmentPhoto;
- import com.linpeng.fragment.FragmentPostbar;
- import com.linpeng.fragment.FragmentQuestion;
-
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.Window;
- import android.app.Activity;
- import android.app.Fragment;
- import android.content.Intent;
- import android.os.Bundle;
-
-
- public class MainActivity extends Activity {
-
- private Fragment[] fragments = new Fragment[ 4 ];
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- fragments[ 0 ] = new FragmentNews();
- fragments[ 1 ] = new FragmentPostbar();
- fragments[ 2 ] = new FragmentQuestion();
- fragments[ 3 ] = new FragmentPhoto();
- getFragmentManager().beginTransaction().add(R.id.main_fragment,fragments[ 1 ])
- .add(R.id.main_fragment,fragments[ 2 ]).add(R.id.main_fragment,fragments[ 3 ]).add(R.id.main_fragment,fragments[ 0 ]).commit();
- newsClick( null );
- }
-
- public void newsClick(View view){
- getFragmentManager().beginTransaction().hide(fragments[ 1 ])
- .hide(fragments[ 2 ]).hide(fragments[ 3 ]).show(fragments[ 0 ]).commit();
-
- getActionBar().setTitle( "News" );
- }
-
- public void postbarClick(View view){
- getFragmentManager().beginTransaction().hide(fragments[ 0 ])
- .hide(fragments[ 2 ]).hide(fragments[ 3 ]).show(fragments[ 1 ]).commit();
-
- getActionBar().setTitle( "Tieba" );
- }
-
- public void questionClick(View view){
- getFragmentManager().beginTransaction().hide(fragments[ 0 ])
- .hide(fragments[ 1 ]).hide(fragments[ 3 ]).show(fragments[ 2 ]).commit();
- getActionBar().setTitle( "know" );
- }
-
- public void photoClick(View view){
- getFragmentManager().beginTransaction().hide(fragments[ 0 ])
- .hide(fragments[ 1 ]).hide(fragments[ 2 ]).show(fragments[ 3 ]).commit();
- getActionBar().setTitle( "Picture" );
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu, menu);
- return true ;
- }
- @Override
- public boolean onMenuItemSelected( int featureId, MenuItem item) {
- switch (item.getItemId()) {
- case R.id.menu_share:
- Intent intent = new Intent(Intent.ACTION_SEND);
- intent.setType( "text/plain" );
- intent.putExtra(Intent.EXTRA_TEXT, "This is a news app that can customize keywords, welcome to try it" );
- startActivity(Intent.createChooser(intent, getTitle()));
- break ;
-
- default :
- break ;
- }
- return super .onMenuItemSelected(featureId, item);
- }
-
- @Override
- public boolean onMenuOpened( int featureId, Menu menu) {
- if (featureId == Window.FEATURE_ACTION_BAR && menu != null ) {
- if (menu.getClass().getSimpleName().equals( "MenuBuilder" )) {
- try {
- Method m = menu.getClass().getDeclaredMethod( "setOptionalIconsVisible" , Boolean.TYPE);
- m.setAccessible( true );
- m.invoke(menu, true );
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- return super .onMenuOpened(featureId, menu);
- }
-
- }
Download address : http://download..com/data/2089511 |