我在另一个片段中有3个片段,我在点击按钮时在片段之间切换 我的问题是,当我按下后退按钮时,片段被隐藏,我需要再次按后退按钮退出片段 如何解决这个问题?
这是我的片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.barber_descrption_layout_fragment, container, false);
} catch (InflateException e) {
}
context = getActivity();
linearLayoutMain = (LinearLayout) rootView.findViewById(R.id.mainfrag);
myTextView_appointment = (MyTextView) rootView.findViewById(R.id.text_appi);
myTextView_map = (MyTextView) rootView.findViewById(R.id.textView2);
myTextView_images = (MyTextView) rootView.findViewById(R.id.img);
rate_btn = (LinearLayout) rootView.findViewById(R.id.rate_button);
rate_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowRateDialog(context);
}
});
Fragment fragment = new Appointment_Fragment();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
linearLayoutMain.setBackground(getResources().getDrawable(R.drawable.blue_boarder_map));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayoutMain.setLayoutParams(params);
//////////////////////////////////
myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.appi_color));
myTextView_appointment.setTextColor(getResources().getColor(R.color.white));
/////////////////////////
myTextView_appointment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.appi_color));
myTextView_appointment.setTextColor(getResources().getColor(R.color.white));
myTextView_map.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_map.setBackground(getResources().getDrawable(R.drawable.map_color_white));
myTextView_images.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_images.setBackground(getResources().getDrawable(R.drawable.blue__white));
linearLayoutMain.setBackgroundColor(getResources().getColor(R.color.white));
Fragment fragment = new Appointment_Fragment();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
linearLayoutMain.setLayoutParams(params);
}
});
myTextView_map.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myTextView_map.setBackground(getResources().getDrawable(R.drawable.map_color));
myTextView_map.setTextColor(getResources().getColor(R.color.white));
myTextView_appointment.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.apii_color_white));
myTextView_images.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_images.setBackground(getResources().getDrawable(R.drawable.blue__white));
linearLayoutMain.setBackground(getResources().getDrawable(R.drawable.blue_boarder_map));
Fragment fragment = new MapFragment_Barber();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(40, 40, 40, 40);
linearLayoutMain.setLayoutParams(params);
}
});
myTextView_images.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myTextView_images.setBackgroundColor(getResources().getColor(R.color.blue_color));
myTextView_images.setTextColor(getResources().getColor(R.color.white));
myTextView_map.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_map.setBackground(getResources().getDrawable(R.drawable.map_color_white));
myTextView_appointment.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.apii_color_white));
linearLayoutMain.setBackgroundColor(getResources().getColor(R.color.white));
Fragment fragment = new BarberImage();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(40, 40, 40, 0);
linearLayoutMain.setLayoutParams(params);
}
});
return rootView;
}
我需要这样当我按下后退按钮时,我会在第一次点击时退出片段而不需要多次点击。
答案 0 :(得分:0)
按照这个。使用onBackPressed()
在Fragment
上实施Abstract Class
功能的绝佳示例。
1)创建一个抽象类 -
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
public abstract class BackableFragment extends Fragment implements View.OnKeyListener {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(this);
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackButtonPressed();
return true;
}
}
return false;
}
public abstract void onBackButtonPressed();
}
2)将Abstract Class
实施到Fragment
-
public class Appointment_Fragment extends BackableFragment {
public static final String CLASS_NAME = "Appointment_Fragment";
public FragmentChangeListener fragmentChangeListener;
...
@Override
public void onBackButtonPressed() {
fragmentChangeListener.OnFragmentChange(CLASS_NAME);
}
...
}
3)制作一个名为interface
的{{1}}。
FragmentChangeListener
4)在interface FragmentChangeListener{
public void onFragmentChange(String fragmentName);
}
-
activity
多数民众赞成。 Reference
答案 1 :(得分:0)
如果您在片段中,请转到childFragmentManager
android.support.v4.app.FragmentManager fm = getChildFragmentManager();
onBackPress上的只会杀死父片段