将chrisbanes ActionBar-PullToRefresh调整为片段(NavigationDrawer)

时间:2013-08-15 14:35:32

标签: android android-fragments navigation-drawer pull-to-refresh

好的,这是我的问题:

我想用片段实现Chrisbanes ActionBar-PullToRefresh库,以便能够与Navigationdrawer一起使用。

  

https://github.com/chrisbanes/ActionBar-PullToRefresh#fragments

Chrisbanes说这是用于片段的:

  

需要注意的一点是PullToRefreshAttacher需要   在Activity的onCreate()阶段创建。如果你打算使用   这个带有Fragments的库然后最好的做法是为你的   创建PullToRefreshAttacher的Activity,然后有你的   片段从Activity中检索它。

     

片段和片段中提供了一个例子。标签示例。

****问题出现了: 我在我的活动中创建了PullToRefreshAttacher,但是我怎么能将PullToRefreshAttacher传递给我的片段:S ****

我已经阅读了很多关于使用putSerializable和Parcelable的bundle和getArguments():

  

Passing an Object from an Activity to a Fragment

我也读过这篇文章。像这样使用((MyActivity ) getActivity()).getClassX() ;

  

Call Activity Method From Fragment

但我真的不理解/工作。 :(

这是NavigationActivity和一个示例片段。我不得不说我是android / Java新手:)

    final String[] menuEntries = {"Start","Datum","Website","Kunden"};
final String[] fragments = {
        "com.blabla.MainFragment",
        "com.blabla.OneFragment",
        "com.blabla.TwoFragment",
        "com.blabla.KundenFragment",
};

private ActionBarDrawerToggle drawerToggle;
private DrawerLayout drawerAdapter;
private ListView navListAdapter;




private PullToRefreshAttacher mPullToRefreshAttacher;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.navigation_layout);

    mPullToRefreshAttacher = PullToRefreshAttacher.get(this);



    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, menuEntries);

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.refresh_navwiev);
    final ListView navList = (ListView) findViewById(R.id.drawerMenu);

    drawerAdapter=drawer;
    navListAdapter=navList;

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);


   drawerToggle = new ActionBarDrawerToggle(
            this, 
            drawer,
            R.drawable.navicon,
            R.string.drawer_open,
            R.string.drawer_close
    ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {

        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {

        }


    };

    drawer.setDrawerListener(drawerToggle);



    navList.setAdapter(adapter);
    navList.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
            drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                @Override
                public void onDrawerClosed(View drawerView){
                    super.onDrawerClosed(drawerView);
                    //Runs On completly Closed
                }
            });

            //Runs Onclick if not same fragment
            if(getActionBar().getTitle()!= menuEntries[pos])
            {

            Bundle bundle=new Bundle();
            bundle.putString("message", "From Activity");

            //Fragment zusammenbauen
            Fragment myFragment=new Fragment();
            myFragment = Fragment.instantiate(NavigationActivity.this, fragments[pos]);
            myFragment.setArguments(bundle);


                FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                tx.setCustomAnimations(R.anim.fragmentfadein, R.anim.fragmentfadeout);
                tx.replace(R.id.navigationScreen, myFragment);

                tx.commit();
                getActionBar().setTitle(menuEntries[pos]);
                drawer.closeDrawer(navList);
            }
        }
    });

    Bundle bundle=new Bundle();
 //     bundle.putInt(PullToRefreshAttacher., position);

    //Fragment zusammenbauen
    Fragment myFragment=new Fragment();
    myFragment = Fragment.instantiate(NavigationActivity.this, fragments[0]);
    myFragment.setArguments(bundle);


        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.setCustomAnimations(R.anim.fragmentfadein, R.anim.fragmentfadeout);
        tx.replace(R.id.navigationScreen, myFragment);

        tx.commit();

}

public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {

        Log.i("FlosTemplate", "Menu Taste Gedrückt");

            if(drawerAdapter.isDrawerOpen(navListAdapter))
            {
            drawerAdapter.closeDrawer(navListAdapter);
            }
            else
            {
            drawerAdapter.openDrawer(navListAdapter);
            }

        return true;
    }
    return super.onKeyUp(keyCode, event);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


}

和片段

public class MainFragment extends Fragment  {

public static Fragment newInstance(Context context) {
    MainFragment f = new MainFragment();
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);



    return rootView;
}

}

如果有人可以帮助我,那将是非常友好的,我在这一天被困住了几天:)

P.S。抱歉可能是坏语言,我不是母语;)

1 个答案:

答案 0 :(得分:3)

GitHub页面上有演示代码:

https://github.com/chrisbanes/ActionBar-PullToRefresh/blob/master/samples/actionbarcompat/src/java/uk/co/senab/actionbarpulltorefresh/samples/actionbarcompat/FragmentTabsActivity.java

将此添加到您的活动:

public PullToRefreshAttacher getPullToRefreshAttacher() {
    return mPullToRefreshAttacher;
}

这是你片段中的onCreateView:

PullToRefreshAttacher mPullToRefreshAttacher = ((NavigationActivity) getActivity()).getPullToRefreshAttacher();

更好的方法是使用接口,但我建议从GitHub示例开始。