好的,这是我的问题:
我想用片段实现 Chrisbanes ActionBar-PullToRefresh 库,以便能够将它与 Navigationdrawer 一起使用。
https://github.com/chrisbanes/ActionBar-PullToRefresh#fragments
.
Chrisbanes 说这是为了与片段一起使用:
需要注意的一点是,PullToRefreshAttacher 需要在 Activity 的 onCreate() 阶段创建。如果您打算将此库与 Fragments 一起使用,那么最佳实践是让您的 Activity 创建 PullToRefreshAttacher,然后让您的 Fragment 从 Activity 中检索它。
Fragment & Tabs 示例中提供了一个示例。
.
.
****问题来了:我在我的活动中创建了 PullToRefreshAttacher,但是我怎么能将 PullToRefreshAttacher 传递给我的片段:S****
我已经阅读了很多关于 bundles 和 getArguments() 与 putSerializable 和 Parcelable :
而且我也读到了这篇文章里面的…… 像这样((MyActivity ) getActivity()).getClassX() ;
使用。
但我没有真正理解/工作。:(
.
.
这是 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;
}
}
如果有人可以帮助我,那就太好了,我在这一点上被困了好几天:)
PS 抱歉,可能语言不好,我不是母语人士;)