В моем основном макете активности у меня есть кнопка изображения.
При нажатии на нее появляется всплывающее окно. Макет всплывающего окна выглядит следующим образом.
test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100px"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/black">
<Button
android:padding="5dp"
android:id="@+id/b_help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Help"
android:textColor="@android:color/white"
android:background="@android:color/black"/>
<Button
android:padding="5dp"
android:id="@+id/b_pref"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Preferences"
android:textColor="@android:color/white"
android:background="@android:color/black"/>
<Button
android:padding="5dp"
android:id="@+id/b_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About Us"
android:textColor="@android:color/white"
android:background="@android:color/black"/>
</LinearLayout>
Всплывающее окно отображается следующим кодом из MainActivity.java
btnOpenPopup = (ImageButton)findViewById(R.id.menu_button);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
popupWindow = new PopupWindow(getBaseContext());
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
}});
Класс всплывающего окна выглядит следующим образом: PopupWindow.java
public class PopupWindow extends android.widget.PopupWindow implements View.OnClickListener{
Context ctx;
View popupView;
Button help,pref,about;
public PopupWindow(Context context)
{
super(context);
ctx = context;
popupView = LayoutInflater.from(context).inflate(R.layout.test, null);
setContentView(popupView);
setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
// Closes the popup window when touch outside of it - when looses focus
setOutsideTouchable(true);
setFocusable(true);
// Removes default black background
setBackgroundDrawable(new BitmapDrawable());
pref = (Button)popupView.findViewById(R.id.b_pref);
pref.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.b_pref:break;
case R.id.b_help:break;
case R.id.b_about:break;
}
}
}
Теперь, нажав кнопку настроек во всплывающем окне, мне нужно отобразить фрагмент настроек.
Я пробовал это для setOnClickListener для кнопки предпочтений, но он не работает, поскольку класс popupwindow не может расширять активность фрагментов, а getFragmentManager() не происходит.
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager
.beginTransaction();
Prefs mPrefsFragment = new Prefs();
//this line..
setContentView(R.layout.activity_prefs);
mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
mFragmentTransaction.addToBackStack(null);
setContentView(R.layout.activity_main);
mFragmentTransaction.commit();
Как я могу вызвать префрагмент с помощью кнопки из всплывающего окна?? Любая помощь будет высоко оценена. Потому что я сильно застрял в этом.