从其他片段(如导航链接)更改片段
Posted
技术标签:
【中文标题】从其他片段(如导航链接)更改片段【英文标题】:Change Fragment from other Fragment like Navigation links 【发布时间】:2015-10-18 21:45:20 【问题描述】:我有一个导航抽屉(所以我使用片段):
MainActivity (FragmentActivity) 片段 A(片段 B、C 和 D 的带有图片链接的片段) 片段 B(片段) 片段 C(片段) 片段 D(片段)我的导航抽屉工作正常,但我希望我的片段 A 做菜单之类的链接。现在,在 Fragment A 中,我这样称呼其他 Fragment:
ImageView contact = (ImageView) getView().findViewById(R.id.secondBlock);
contact.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
Fragment fragment = new ContactezNous();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.accueil, fragment);
fragmentTransaction.commit();
);
但我不想使用这种方式,因为我在查找其他 Fragment 的标签和 id 时遇到问题。
我想知道如何通过点击链接从片段 A 更改片段,就像我点击主活动的导航抽屉链接一样。
主要活动:
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener
private static String TAG = MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
//Initialisation de l activite avec les donnees necessaires
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// Affichage de la navigation
displayView(0);
@Override
public boolean onCreateOptionsMenu(Menu menu)
//Ajout des items
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
@Override
public void onDrawerItemSelected(View view, int position)
displayView(position);
private void displayView(int position)
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position)
case 0:
fragment = new Accueil();
title = getString(R.string.title_accueil);
break;
case 1:
fragment = new NosOffres();
title = getString(R.string.title_nosoffres);
break;
case 2:
fragment = new DemandeGratuite();
title = getString(R.string.title_demandegratuite);
break;
case 3:
fragment = new ContactezNous();
title = getString(R.string.title_contact);
break;
case 4:
fragment = new Actualites();
title = getString(R.string.title_actu);
break;
case 5:
fragment = new MentionsLegales();
title = getString(R.string.title_mentions);
break;
default:
break;
if (fragment != null)
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
//fragmentTransaction.addToBackStack("name");
fragmentTransaction.commit();
// libelle du toolbar
TextView titlet;
titlet = (TextView) findViewById(R.id.main_toolbar_title);
titlet.setText(title);
titlet.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/GothamBook.ttf"));
【问题讨论】:
我有一个问题要澄清您的问题。您是说您想从您的Accueil
片段导航到另一个片段,但您不想在Accueil
中包含片段事务代码?相反,您希望它回调到您的 MainActivity
,并让 它 进行交易?
@AutonomousApps 是的,因为通过导航链接我可以返回accueil
,但是通过单击 accueil 中的链接,它会关闭应用程序,因为这是一个事务,我不使用 backstack或者别的什么...
【参考方案1】:
您应该使用以下常见的 android 片段模式:
public class Accueil extends Fragment
// Your code here...
public interface OnFragmentInteractionListener
void onInteraction(int id);
private OnFragmentInteractionListener mListener;
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
try
mListener = (OnFragmentInteractionListener) activity;
catch (ClassCastException e)
throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
@Override
public void onDetach()
super.onDetach();
mListener = null;
// ...more code...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
// ...initialize your View...
ImageView contact = (ImageView) getView().findViewById(R.id.secondBlock);
contact.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// This is how you use the listener
mListener.onInteraction(3);
);
// ...more initialization...
MainActivity.java
public class MainActivity AppCompatActivity
implements FragmentDrawer.FragmentDrawerListener, OnFragmentInteractionListener
// Your code here...
// Implement OnFragmentInteractionListener
@Override
public void onInteraction(int id)
// 'id' would be '3' for your 'ContactezNous' fragment
displayView(id);
【讨论】:
但是我在哪里指定 Accueil Fragment 中图像的 OnClickListener ?这四个块是图片,我希望它们像导航中一样打开片段 太完美了!!谢谢!以上是关于从其他片段(如导航链接)更改片段的主要内容,如果未能解决你的问题,请参考以下文章