Dynamic interface with sliding menu and action bar

An attempt to achieve a dynamic interface with facebook as a sliding menu and actionbarsherlock . First, I look at the Android documentation, which introduces a fragment for processing a dynamic button. But without luck and a week, I still canโ€™t get it to work anyway, I think this is my misunderstanding in the Android concept. The sliding panel and actionbarsherlock work without problems.

I have a HomeScreen.java that contains all of my menus and a pre-configuration step and so far I have created pagerAdapter1.java that extends FragmentPagerAdapter, and three examples of the fragment class that handle my work, which is task1.java, task2.java task3.java is quite simple

here is part of my HomeScreen.java code

import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import com.slidingmenu.lib.SlidingMenu; import com.slidingmenu.lib.app.SlidingFragmentActivity; public class HomeScreen extends SlidingFragmentActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home_screen); setBehindContentView(R.layout.menu_frame); } 

PagerAdapter1.java

 public class PagerAdapter1 extends FragmentPagerAdapter { private List<Fragment> fragments; public PagerAdapter1(FragmentManager fm, List<Fragment> fragments) { super(fm); this.fragments = fragments; } public Fragment getItem(int position) { return this.fragments.get(position); } public int getCount() { return this.fragments.size(); } } 

and three task1.java, 2,3

  import android.support.v4.app.Fragment; public class Tab1Fragment extends Fragment{ onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { return null; } return (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout, container, false); } 

I think itโ€™s better to explain my image problem

The home screen, which is the stage of presetting, whenever the user clicks on the menu, this page will change to the desired page.

homescreen

and this is my menu

menu_frame

My problem is how to include this 3 fragment in my desktop? I tried so many tutorials, but this does not work in my situation. Most tutorials create a code snippet, I just want to include my 3 tasks in it

+6
source share
3 answers

I will try to explain this sample code and you will use it for your needs.

This is the ListFragment of your BehindContent (SlidingMenu):

 public class ColorMenuFragment extends ListFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.list, null); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); String[] colors = getResources().getStringArray(R.array.color_names); ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, colors); setListAdapter(colorAdapter); //This array is only to fill SlidingMenu with a Simple String Color. //I used MergeAdapter from Commonsware to create a very nice SlidingMenu. } @Override public void onListItemClick(ListView lv, View v, int position, long id) { //This switch case is a listener to select wish item user have been selected, so it Call //ColorFragment, you can change to Task1Fragment, Task2Fragment, Task3Fragment. Fragment newContent = null; switch (position) { case 0: newContent = new ColorFragment(R.color.red); break; case 1: newContent = new ColorFragment(R.color.green); break; case 2: newContent = new ColorFragment(R.color.blue); break; case 3: newContent = new ColorFragment(android.R.color.white); break; case 4: newContent = new ColorFragment(android.R.color.black); break; } if (newContent != null) switchFragment(newContent); } // the meat of switching the above fragment private void switchFragment(Fragment fragment) { if (getActivity() == null) return; if (getActivity() instanceof FragmentChangeActivity) { FragmentChangeActivity fca = (FragmentChangeActivity) getActivity(); fca.switchContent(fragment); } else if (getActivity() instanceof ResponsiveUIActivity) { ResponsiveUIActivity ra = (ResponsiveUIActivity) getActivity(); ra.switchContent(fragment); } } } 

Here is your BaseActivity class:

It does not have a scroll, as I could understand, you do not need it.

 public class FragmentChangeActivity extends BaseActivity { private Fragment mContent; public FragmentChangeActivity() { super(R.string.changing_fragments); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set the Above View if (savedInstanceState != null) mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent"); if (mContent == null) mContent = new ColorFragment(R.color.red); // set the Above View //This will be the first AboveView setContentView(R.layout.content_frame); getSupportFragmentManager() .beginTransaction() .replace(R.id.content_frame, mContent) .commit(); // set the Behind View //This is the SlidingMenu setBehindContentView(R.layout.menu_frame); getSupportFragmentManager() .beginTransaction() .replace(R.id.menu_frame, new ColorMenuFragment()) .commit(); // customize the SlidingMenu //This is opcional getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); getSupportFragmentManager().putFragment(outState, "mContent", mContent); } public void switchContent(Fragment fragment) { // the meat of switching fragment mContent = fragment; getSupportFragmentManager() .beginTransaction() .replace(R.id.content_frame, fragment) .commit(); getSlidingMenu().showContent(); } } 

So, if you want to change the ColorFragment to something else, do the following:

First select the item you want to use:

 case 0: newContent = new ColorFragment(R.color.red); break; 

in

 case 0: newContent = new ArrayListFragment(); break; 

I did only an arraist, this is just a simple example, you can do a lot, then you can read about Fragment to learn how to do different things.

  public class ArrayListFragment extends ListFragment { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Listnames.TITLES)); //Listnames is a class with String[] TITLES; } @Override public void onListItemClick(ListView l, View v, int position, long id) { Log.i("FragmentList2", "Item clicked: " + id); String item = (String) getListAdapter().getItem(position); Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show(); } } 

Well, if you misunderstood something, just tell me.

+14
source

My problem is how to include this 3 snippet in my desktop?

It really depends on how you want them to behave.

Do you want them to appear only one at a time, not allowing them to scroll between them? If so, add / paste a container layout (e.g. a simple FrameLayout ) into your Activity , into which you will add Fragments . I did not work with the SlidingMenu library, but when calling one of the items in the menu, it should have a callback. In this callback, you add the correct fragment to the container layout ( FrameLayout ) that I mentioned earlier.

You want to show only one fragment, but you want to allow the user to carry between them? If so, use the ViewPager in the action layout and in the callback caused by the menu selection of the SlidingMenu library, set the current ViewPager page using the setCurrentItem() method.

If you need something else, then this will give more detailed information.

Most tutorials create a code snippet, I just want to include my task 3

This, I do not quite understand. If you want to โ€œincludeโ€ your task fragments directly in your xml layout, you can, but you will be limited by what you can do with them (not to mention that all fragments will be on the same screen), and I would avoid It. If you want something else to provide more details.

+2
source

I donโ€™t think that this will work with Fragments, I also looked for a solution and as a result I added fragments manually.

I am working on something similar, but for me there has also been a case of opening WebViews for assigned URLs. Thus, the screen "above" will always be updated with each press.

To control the behavior of this, I created a MenuItemResource object that basically contains properties such as the icon identifier, menu item name, and URL.

 public class MenuItemResource { private int aValue; private int aUrl; private int aIconIdle; private int aIconActive; public MenuItemResource(int value, int url, int iconIdle, int iconActive) { aValue = value; aUrl = url; aIconIdle = iconIdle; aIconActive = iconActive; } } 

The behavior is handled using the OnItemClickListener , which checks using a switch whose values โ€‹โ€‹are in the MenuItemResource that is clicked. For WebView this is pretty simple:

  newFragment = new WebViewFragment(); final Bundle arguments = new Bundle(); arguments.putString(Constants.KEY_URL, getString(item.getUrl())); newFragment.setArguments(arguments); startFragment(newFragment, false); // boolean is used to add the fragment to the backstack 

The startFragment method uses the FragmentManager and FragmentTransaction to replace the current Fragment . This works the same for other MenuItemResources that run regular snippets.

  newFragment = new Task1Fragment(); startFragment(newFragment, false); 

I don't refer to snippets in MenuItemResource (yet), but it works very well for URLs and WebView. Fragments start based on value in MenuItemResource I am not sure how you feel about fragments, as in comments (Task1.java, etc.), since you do not start them with Intents , like Activities . Also, I'm not sure why you would like to do this dynamically for Fragments (I can imagine that this case is dynamic for WebViews), since they still have to be compiled, so my menu items are added manually.

+1
source

All Articles