动态UI滑动菜单和actionbarsherlock [英] Dynamic UI with sliding menu and actionbarsherlock

查看:122
本文介绍了动态UI滑动菜单和actionbarsherlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

努力实现一个动态UI与 Facebook的类似滑动菜单和的 actionbarsherlock 。首先我必须看看android的文档其中介绍片段来处理动态按钮。但没有运气和一个星期的时间,我仍然无法得到它的工作,无论如何,我想是我的误解在Android concept.The slidingbar和actionbarsherlock工作没有任何问题。

Trying to achieve a dynamic UI with facebook like sliding menu and actionbarsherlock .First i have look into android documentation which introduce fragment to handle dynamic button. But with no luck and a week time , i still can't get it to work anyhow , i guess is my misunderstand on android concept.The slidingbar and actionbarsherlock work without any problem.

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

I have a HomeScreen.java which contain all my menu and presetation stage and so far i have created a pagerAdapter1.java that extends FragmentPagerAdapter , and three example fragment class that handle my work which is task1.java,task2.java ,task3.java simple enough

下面是我的code部分 HomeScreen.java

here is part of my code 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

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

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 its better to explain my problem with picture

一个主屏幕是一个presetation阶段,在菜单中,每当用户点击,这个页面会变成他想要的页面

A homescreen which is a presetation stage , whenever user click on menu , this page will change to the page he want

这是我的菜单

我的问题是我怎么有这3片段插入我的主屏幕?我已经试了很多教程,但它不能在我的situation.Most教程,创建片段,code,我只是想包括我的3个任务到它

My problem is how do i include this 3 fragment into my homescreen ? i have tried so many tutorial but it doesn't work in my situation.Most tutorial are creating fragment with code, i just want to include my 3 task into it

推荐答案

我马上试图解释这个样本code和使用您的需求。

I´ll try to explain this sample code and you use for your need.

这是你BehindContent的ListFragment(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);
        }
    }


}

这是你的BaseActivity类:

它不具备刷卡,我能理解,你并不需要这个。

It dont have swipe, as I could understand, you don't need this.

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别的,做到这一点:

Ok, So If you want to change the ColorFragment to anything else, do this:

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

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

到:

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

我已经做只是一个ArrayList中,这只是一个简单的例子,你可以做很多事情,那么你可以阅读有关的Fragment ,了解如何做不同的事情。

I have made just a arraylist, it is just a simple example, you can do a lot of thing, 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.

这篇关于动态UI滑动菜单和actionbarsherlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆