我正在执行android项目,实现片段并遇到麻烦。 这是麻烦的故事:
我创建了Main活动,该活动使用Fragment Layout保存片段。我这样做是因为我想使用抽屉菜单实现导航菜单。所有这些步骤都可以正常工作。
代码如下:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private Toolbar toolbar;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.layout_drawer);
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
toggle = new ActionBarDrawerToggle(this,drawer,toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Main_fragment()).commit();
navigationView.setCheckedItem(R.id.home);
}
}
直到这里都可以正常工作。
然后在片段活动中,我触发了Textview并调用新活动。我这样做是因为我想为这些片段制作一个新的占位符,并放置不同的“抽屉菜单”。
她是代码:
public class Main_fragment extends Fragment{
TextView t1,t2,t3,t4,t5,t6,t7;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main_fragment,container,false);
//define buttons
t1 = (TextView) v.findViewById(R.id.txt1);
t2 = (TextView) v.findViewById(R.id.txt2);
t3 = (TextView) v.findViewById(R.id.txt3);
t4 = (TextView) v.findViewById(R.id.txt4);
t5 = (TextView) v.findViewById(R.id.txt5);
t6 = (TextView) v.findViewById(R.id.txt6);
t7 = (TextView) v.findViewById(R.id.txt7);
t1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//intent to the second activity
Intent i = new Intent(getActivity(), Social_Container.class);
startActivity(i);
}
});
t2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), Social_Container.class);
startActivity(i);
}
});
t3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), Social_Container.class);
startActivity(i);
}
});
t4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), Social_Container.class);
startActivity(i);
/**
//this code just for trial fragment
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.fragment_container, new Fragment_Pengurusan());
fr.commit();
**/
}
});
t5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), Social_Container.class);
startActivity(i);
/**
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.fragment_container, new Fragment_Politic());
fr.commit();
**/
}
});
t6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), Social_Container.class);
startActivity(i);
/**
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.fragment_container, new Fragment_Industri());
fr.commit();
**/
}
});
t7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), Social_Container.class);
startActivity(i);
/**
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.fragment_container, new Fragment_Awam());
fr.commit();
**/
}
});
return v;
}
}
问题是,如何基于我在主活动内最后一个Fragment活动中附加的Textview上设置的CLick触发在第二类占位符(FrameLayout)上设置片段活动?< / p>
注意:我还没有在第二堂课中设置任何东西。我只是想知道如何从那里的TextView调用基于ClickListener的Second Class内部的片段。