8

尝试使用facebook 实现动态 UI,例如滑动菜单actionbarsherlock 。首先, 我查看了引入片段来处理动态按钮的 android 文档。但是没有运气和一周的时间,我仍然无法让它工作,我想是我对 android 概念的误解。滑动条和 actionbarsherlock 工作没有任何问题。

我有一个 HomeScreen.java ,其中包含我所有的菜单和预设阶段,到目前为止,我已经创建了一个扩展 FragmentPagerAdapter 的 pagerAdapter1.java 和三个处理我的工作的示例片段类,即 task1.java、task2.java、task3.java足够简单

这是我的代码 HomeScreen.java 的一部分

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();
    }

}

和三个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);
        }

我认为最好用图片来解释我的问题

一个主屏幕,是一个预设阶段,每当用户点击菜单时,这个页面就会变成他想要的页面。

主屏幕

这是我的菜单

菜单框架

我的问题是如何将这 3 个片段包含到我的主屏幕中?我尝试了很多教程,但在我的情况下不起作用。大多数教程都是用代码创建片段,我只想将我的 3 个任务包含在其中

4

3 回答 3

14

我将尝试解释此示例代码,并根据您的需要使用。

这是您的BehindContent (SlidingMenu) 的ListFragment:

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);
        }
    }


}

这是您的 BaseActivity 类:

它没有滑动,我可以理解,你不需要这个。

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();
    }

}

好的,所以如果您想将 ColorFragment 更改为其他任何内容,请执行以下操作:

首先,选择您要使用的项目:

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

至:

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

我只是做了一个arraylist,它只是一个简单的例子,你可以做很多事情,然后你可以阅读Fragment来学习如何做不同的事情。

    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();

        }

    }

好吧,如果您误解了什么,请告诉我。

于 2013-01-16T14:39:41.290 回答
2

My problem is how do i include this 3 fragment into my homescreen ?

It really depends on how do you want them to behave.

Do you want them to appear only one at a time without allowing swipeing between them? If yes then add/insert a container layout(for example a simple FrameLayout) in your Activity on which you'll add the Fragments. I didn't worked with the SlidingMenu library but it should have a callback called when you click one of the items in the menu. In that callback you'll attach the proper fragment to the container layout(the FrameLayout) I mention earlier.

Do you want to show only one Fragment but you want to allow the user to swipe between them? If yes use a ViewPager in the activity layout and in the callback triggered by the SlidingMenu library's menu selection set the current page of the ViewPager with the setCurrentItem() method.

If you want something different then this provide more details.

Most tutorial are creating fragment with code, i just want to include my 3 task into it

This, I don't quite understand. If you want to "include" your task fragments directly in your xml layout, you can but you'll be limited on what you can do with them(not to mention that all the fragments will be on one single screen) and I would avoid it. If you want something else provide more details.

于 2013-01-11T18:55:29.653 回答
1

我认为它不会像 Fragments 那样工作,我也在寻找解决方案并最终手动添加了 Fragments。

我正在做类似的事情,但对我来说,也有打开WebViews指定 URL 的情况。因此,“上方”屏幕总是会在任何点击时更新。

为了控制它的行为,我创建了一个MenuItemResource对象,它基本上包含属性,如图标的 ID、菜单项的名称和 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;
    }
}

该行为由一个OnItemClickListenerwhich 用一个开关检查哪些值在MenuItemResource被点击的那个中处理。因为WebView它非常简单:

            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

startFragment 方法只是使用FragmentManagerandFragmentTransaction来替换当前的Fragment. 这对于其他MenuItemResources启动常规片段的情况相同。

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

我没有提到MenuItemResource(还)中的片段,但它对 URL 和 WebView 非常有效。value片段是基于MenuItemResource 我不知道你会如何引用你在评论中所做的片段(Task1.java 等),因为你不以Intentslike开头Activities。此外,我不确定为什么要为 Fragments 动态执行此操作(我可以想象这种情况对于 WebViews 是动态的),因为它们无论如何都需要编译,所以这就是我的菜单项是手动添加的原因。

于 2013-01-14T09:35:54.187 回答