好吧,这是我的问题:
我想用片段实现chrisbanes actionbar pulltorefresh库,以便能够与navigationdrawer一起使用。
https://github.com/chrisbanes/ActionBar-PullToRefresh#fragments
.
克瑞斯班斯说这是为了和碎片一起使用:
需要注意的一点是pulltorefreshatcher需要
在活动的oncreate()阶段创建。如果你打算使用
这个带有片段的库的最佳实践是
创建PullToRefreshAttacher的活动,然后
片段从活动中检索它。
fragment&tabs示例中提供了一个示例。
.
.
****问题来了:
我在我的活动中创建了pulltorefreshatcher,但是我怎么能把pulltorefreshatcher传递给我的片段:s***
我读了很多关于bundle和getArguments()以及putserializable和parcelable的内容:
Passing an Object from an Activity to a Fragment
我也读过这篇文章,其中使用了像这样的东西。
Call Activity Method From Fragment
但没什么我真正理解/工作。:(
.
.
这里是导航活动和一个示例片段。我必须说我对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;
}

}

如果有人能帮我的话,我会非常感谢的,我已经困在这里好几天了:)
抱歉,可能是因为我的语言不好,我不是以英语为母语的人;)

最佳答案

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示例开始。

09-12 03:21