Back to project page palhike.
The source code is released under:
GNU General Public License
If you think the Android project palhike listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.palhike.android; //from w w w . j a v a2s. co m import java.util.ArrayList; import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Typeface; import android.os.Bundle; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.text.TextUtils.TruncateAt; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class TreeTabs extends Activity implements ActionBar.TabListener { /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a {@link FragmentPagerAdapter} * derivative, which will keep every loaded fragment in memory. If this * becomes too memory intensive, it may be best to switch to a * {@link android.support.v13.app.FragmentStatePagerAdapter}. */ SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tree_tabs); Bundle extras = getIntent().getExtras(); if (extras == null) { // no tree found // TODO: add this after finish debug // finish(); // startActivity(new Intent(this, WelcomeActivity.class)); // return; SharedPreferences prefs = getSharedPreferences("com.example.hike", Context.MODE_PRIVATE); prefs.edit().putString("resultIdString","1").commit(); prefs.edit().putString("language","en").commit(); }else { SharedPreferences prefs = getSharedPreferences("com.example.hike", Context.MODE_PRIVATE); prefs.edit().putString("resultIdString",extras.getString("resultIdString")).commit(); prefs.edit().putString("language",extras.getString("language")).commit(); } // Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); // When swiping between different sections, select the corresponding // tab. We can also use ActionBar.Tab#select() to do this if we have // a reference to the Tab. mViewPager .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { // Create a tab with text corresponding to the page title defined by // the adapter. Also specify this Activity object, which implements // the TabListener interface, as the callback (listener) for when // this tab is selected. actionBar.addTab(actionBar.newTab() .setText(mSectionsPagerAdapter.getPageTitle(i)) .setTabListener(this) ); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.tree_tabs, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in // the ViewPager. mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { private ArrayList<FragmentInfo> _fragmentsInfo = new ArrayList<FragmentInfo>(); private String[] _tabTitles; public SectionsPagerAdapter(FragmentManager fm) { super(fm); _tabTitles = getResources().getStringArray(R.array.tabs_titles); for (String tabTitle : _tabTitles ) { _fragmentsInfo.add(new FragmentInfo(_fragmentsInfo.size(), tabTitle, getLayout(tabTitle))); } } private int getLayout(String tabTitle) { int resource = getResources().getIdentifier("tab_" + tabTitle, "layout",getPackageName()); return (resource == 0) ? R.layout.tab_missing : resource; } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class // below). return PlaceholderFragment .newInstance(_fragmentsInfo.get(position)); } @Override public int getCount() { return _fragmentsInfo.size(); } @Override public CharSequence getPageTitle(int position) { return _fragmentsInfo.get(position).get_title(); } } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; private static final String ARG_CHILD_VIEW_LAYOUT = "child_layout"; /** * Returns a new instance of this fragment for the given section number. */ public static PlaceholderFragment newInstance(FragmentInfo fragmentInfo) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, fragmentInfo.get_sectionNumber()); args.putInt(ARG_CHILD_VIEW_LAYOUT, fragmentInfo.get_childViewLayout()); fragment.setArguments(args); return fragment; } public PlaceholderFragment() { } private int current_image = 0; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { SharedPreferences shared = container.getContext().getSharedPreferences("com.example.hike", MODE_PRIVATE); String resultIdString = shared.getString("resultIdString", ""); String language = shared.getString("language", ""); ObjectModel treeData; // handle tree qr // try { TestAdapter database = new TestAdapter(container.getContext()); database = database.open(); treeData = database.getObject(resultIdString, "minor", language); // if (data == null) { // throw new Exception("noData"); // } // } catch (SQLException e) { // throw e; // } catch (Exception ex) { // throw ex; // } this.getActivity().setTitle(treeData._title); View rootView = inflater.inflate(R.layout.header_tree_tabs, container, false); // set child view View childView = inflater.inflate( getArguments().getInt(ARG_CHILD_VIEW_LAYOUT), container, false); final TextView title = (TextView) childView.findViewById(R.id.treeTitle); if (title != null ) { title.setText(treeData._title); } final TextView treeDescription = (TextView) childView.findViewById(R.id.treeDescription); if (treeDescription != null ) { treeDescription.setText(treeData._description); } // handle images handleImage(treeData, childView); // handle scientific and did you know final LinearLayout dataLayout = (LinearLayout) childView.findViewById(R.id.treeInfo); if (dataLayout != null ) { String[] entries = treeData.get_scientific().split("\\|"); for(String entry : entries){ LinearLayout ll = new LinearLayout(container.getContext()); ll.setOrientation(LinearLayout.HORIZONTAL); ll.setGravity(Gravity.CENTER); String[] row = entry.split("\\\\\\\\def"); if (row.length > 0) { TextView keyString = new TextView(container.getContext()); keyString.setText(row[0] + " "); keyString.setTextIsSelectable(true); keyString.setTypeface(null, Typeface.BOLD); keyString.setTextColor(container.getContext().getResources().getColor(R.color.hike_brown)); ll.addView(keyString); } if (row.length > 1) { TextView valueString = new TextView(container.getContext()); valueString.setText(row[1]); valueString.setTextIsSelectable(true); valueString.setEllipsize(TruncateAt.MIDDLE); valueString.setTextColor(container.getContext().getResources().getColor(R.color.hike_gray)); ll.addView(valueString); } dataLayout.addView(ll); } } final TextView extras = (TextView) childView.findViewById(R.id.treeExtras); if (extras != null ) { extras.setText(treeData.get_didyouknow().replace("\n\r", " ")); } // append child view from args ((LinearLayout) rootView.findViewById(R.id.section_layout)) .addView(childView); return rootView; } // private HashMap<String,String> getScientific(String data){ // HashMap<String,String> resultHashMap; // String[] entries = data.split("|"); // for(String entry : entries){ // String[] row = entry.split("\\def"); // String key = row[0]; // String value = row[1]; // resultHashMap.add(key,value); // } // return resultHashMap; // } /** * @param treeData * @param childView */ private void handleImage(ObjectModel treeData, View childView) { final ImageView image = (ImageView) childView.findViewById(R.id.treeImages); if (image !=null) { // String num_images_string = treeData._num_images; // // if (num_images_string == null) { // num_images_string = "3"; // } // final int num_images = Integer.parseInt(num_images_string); // final ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>(); if (treeData._image1 != null) {Bitmap image1bm = BitmapFactory.decodeByteArray(treeData._image1, 0, treeData._image1.length); bitmapArray.add(image1bm); } if (treeData._image2 != null) { Bitmap image2bm = BitmapFactory.decodeByteArray(treeData._image2, 0, treeData._image2.length); bitmapArray.add(image2bm); } if (treeData._image3 != null) { Bitmap image3bm = BitmapFactory.decodeByteArray(treeData._image3, 0, treeData._image3.length); bitmapArray.add(image3bm); } if (treeData._image4 != null) { Bitmap image4bm = BitmapFactory.decodeByteArray(treeData._image4, 0, treeData._image4.length); bitmapArray.add(image4bm); } // if (treeData._image5 != null) // { // Bitmap image5bm = BitmapFactory.decodeByteArray(treeData._image5, 0, // treeData._image5.length); // bitmapArray.add(image5bm); // } image.setImageBitmap(bitmapArray.get(0)); image.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (current_image != (bitmapArray.size() - 1)) { image.setImageBitmap(bitmapArray.get(current_image + 1)); current_image = current_image + 1; } else { image.setImageBitmap(bitmapArray.get(0)); current_image = 0; } } }); } } } }