Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adapt chrisbanes ActionBar-PullToRefresh to Fragments(NavigationDrawer)

Okay here is my problem:

I want to implement Chrisbanes ActionBar-PullToRefresh library with fragments to be able to use it with Navigationdrawer.

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

.

Chrisbanes says this for the use with fragments:

One thing to note is that the PullToRefreshAttacher needs to be created in the onCreate() phase of the Activity. If you plan on using this library with Fragments then the best practice is for your Activity to create the PullToRefreshAttacher, and then have your fragments retrieve it from the Activity.

An example is provided in the Fragment & Tabs sample.

.

.

****Here comes the question: I created the PullToRefreshAttacher in my activity but how the hell could I pass the PullToRefreshAttacher to my fragments :S****

I have read much about bundles and getArguments() with putSerializable and Parcelable :

Passing an Object from an Activity to a Fragment

and I also read this article in which sth. like this ((MyActivity ) getActivity()).getClassX() ; is used.

Call Activity Method From Fragment

But nothing I really understood/worked. :(

.

.

Here are the NavigationActivity and one example fragment.I have to say that I am new to 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;
}


}

And a fragment

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

}

It would be very kind if somebody could help me ,i am stuck at this point for days :)

P.S. sorry for the probably bad language , i am not a native speaker ;)

like image 937
Florian Braun Avatar asked Oct 22 '22 03:10

Florian Braun


1 Answers

There is demo code on the GitHub page:

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

Add this to your Activity:

public PullToRefreshAttacher getPullToRefreshAttacher() {
    return mPullToRefreshAttacher;
}

And this to the onCreateView in your Fragment:

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

A better approach would be to use an interface but I'd recommend starting with the GitHub example.

like image 71
blackcj Avatar answered Oct 27 '22 09:10

blackcj