一共有4个fragment,分别是contact(联系人),friends(朋友),search(查找),more(更多)。使用的都是同一个布局,每个fragment中都有四个内部按钮,可以切换到其他的3个fragment中。
现在只考虑在contact中,实现点4个(其实有效的是3个),切换到其他3个fragment中。
现在的情况是,点击contactfragment中的按钮,toast会响应,但是fragment不会跳转。这是怎么回事,下面是两个类的代码。
这是contactfragment。
public class ContactFragment extends Fragment implements OnClickListener { MainActivity activity = (MainActivity) getActivity(); View view = null; Button btn_inner_contact; Button btn_inner_friends; Button btn_inner_search; Button btn_inner_more; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.frag_content, container, false); TextView textview_hint = (TextView) view .findViewById(R.id.textview_hint); textview_hint.setText("Contact"); initComponent(); return view; } private void initComponent() { btn_inner_contact = (Button) view .findViewById(R.id.button_inner_contact); btn_inner_friends = (Button) view .findViewById(R.id.button_inner_friends); btn_inner_search = (Button) view.findViewById(R.id.button_inner_search); btn_inner_more = (Button) view.findViewById(R.id.button_inner_more); btn_inner_contact.setOnClickListener(this); btn_inner_friends.setOnClickListener(this); btn_inner_search.setOnClickListener(this); btn_inner_more.setOnClickListener(this); } @Override public void onClick(View v) { FragmentManager fm = activity.getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); switch (v.getId()) { case R.id.button_inner_contact: Toast.makeText(getActivity(), "点击了内部contact按钮", Toast.LENGTH_SHORT) .show(); if (activity.contactFragment == null) { activity.contactFragment = new ContactFragment(); } ft.replace(R.id.framelayout_content, activity.contactFragment); break; case R.id.button_inner_friends: Toast.makeText(getActivity(), "点击了内部friends按钮", Toast.LENGTH_SHORT) if (activity.friendsFragment == null) { activity.friendsFragment = new FriendsFragment(); } ft.replace(R.id.framelayout_content, activity.friendsFragment); break; case R.id.button_inner_more: if (activity.moreFragment == null) { activity.moreFragment = new MoreFragment(); } ft.replace(R.id.framelayout_content, activity.moreFragment); break; case R.id.button_inner_search: if (activity.searchFragment == null) { activity.searchFragment = new SearchFragment(); } ft.replace(R.id.framelayout_content, activity.searchFragment); break; default: break; } ft.commit(); } }
这是mainactivity类,mainactivity中的那四个按钮及其响应时间是fragment外部的四个按钮,与内部的inner按钮没关系
public class MainActivity extends Activity implements OnClickListener { public ContactFragment contactFragment; public FriendsFragment friendsFragment; public MoreFragment moreFragment; public SearchFragment searchFragment; private Button btn_contact; private Button btn_friends; private Button btn_search; private Button btn_more; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initcomponet(); initview(); } private void initview() { contactFragment = new ContactFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.add(R.id.framelayout_content, contactFragment); ft.commit(); } private void initcomponet() { btn_contact = (Button) findViewById(R.id.button_contact); btn_friends = (Button) findViewById(R.id.button_friends); btn_more = (Button) findViewById(R.id.button_more); btn_search = (Button) findViewById(R.id.button_search); btn_contact.setOnClickListener(this); btn_friends.setOnClickListener(this); btn_more.setOnClickListener(this); btn_search.setOnClickListener(this); } @Override public void onClick(View v) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); switch (v.getId()) { case R.id.button_contact: if (contactFragment == null) { contactFragment = new ContactFragment(); } ft.replace(R.id.framelayout_content, contactFragment); break; case R.id.button_friends: if (friendsFragment == null) { friendsFragment = new FriendsFragment(); } ft.replace(R.id.framelayout_content, friendsFragment); break; case R.id.button_more: if (moreFragment == null) { moreFragment = new MoreFragment(); } ft.replace(R.id.framelayout_content, moreFragment); break; case R.id.button_search: if (searchFragment == null) { searchFragment = new SearchFragment(); } ft.replace(R.id.framelayout_content, searchFragment); break; default: break; } ft.commit(); } }
我试了一下,把MainActivity activity=(MainActivity)getActivity();这行代码放到onclick方法了,就可以了!
时间: 2024-10-13 07:31:16