【发布时间】:2018-08-14 20:49:09
【问题描述】:
在设备上加载我的程序进行测试时,当我使用底部导航打开不同的片段时,什么都没有发生。如果您需要我添加更多代码,请说出来。我已经为 Main Activity 发布了我的 XML 和 Java,并且 XML 包含我的布局的 mainFrame。
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.shareFrame:
transaction.replace(R.id.mainFrame, new shareFragment()).commit();
mTextMessage.setText(R.string.title_share);
return true;
case R.id.settingsFrame:
transaction.replace(R.id.mainFrame, new settingsFragment()).commit();
mTextMessage.setText(R.string.title_settings);
return true;
case R.id.helpFrame:
transaction.replace(R.id.mainFrame, new helpFragment()).commit();
mTextMessage.setText(R.string.title_help);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.mainFrame, new shareFragment()).commit();
}
}
这是我的 shareFragment.java 的 Java
public class shareFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public shareFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment shareFragment.
*/
// TODO: Rename and change types and number of parameters
public static shareFragment newInstance(String param1, String param2) {
shareFragment fragment = new shareFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_share, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
Toast.makeText(context, "Share", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
这里是 XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/shareFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.swapdrop.swapdrop.shareFragment">
</FrameLayout>
【问题讨论】:
-
发布您的布局
-
仅用于我的 MainActivity?我还有 3 个其他片段
-
以包含主框架布局的为准
-
完成,抱歉,耽搁了一段时间
-
你能只发布你的片段代码吗?
标签: android android-studio android-fragments