我遇到了從另一個片段中打開一個片段的問題。我的主要活動實現了最新的導航欄抽屜,導航欄列表中的每個選項都會打開一個新的片段。這一點已經實施並且運作良好。我遇到的問題是下一層碎片。我有一個imageButton的事件片段。點擊時,我需要導航到這個子事件的新片段。我試圖在Event Fragment中創建一個onclicklistener打開子事件頁面,但我得到以下錯誤。從片段打開片段imageButton Click?
未發現ID 0x7f08000a視圖(com.ikimuhendis.ldrawer.sample:ID/imagefrag),用於片段imageFragment1 {348db088#0的id = 0x7f08000a}
public class EventFragment extends Fragment
{
ImageButton test;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_event, container, false);
SliderLayout sliderShow = (SliderLayout) rootView.findViewById(R.id.slider);
sliderShow.setDuration(8000);
sliderShow.setPresetTransformer(SliderLayout.Transformer.ZoomOut);
//get the button view
test = (ImageButton) rootView.findViewById(R.id.imageButton);
test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Fragment videoFragment = new imageFragment1();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.imagefrag, videoFragment)
.commit();
}
});
HashMap<String, Integer> file_maps = new HashMap<String, Integer>();
file_maps.put("Munster Vs Leinster", R.drawable.rugby);
file_maps.put("Ennio Morricone", R.drawable.ennio);
file_maps.put("Leinster Vs Harlequins", R.drawable.heino);
file_maps.put("WWE Live", R.drawable.wwe);
for (
String name
: file_maps.keySet())
{
TextSliderView textSliderView = new TextSliderView(getActivity());
textSliderView
.description(name)
.image(file_maps.get(name));
sliderShow.addSlider(textSliderView);
}
getActivity().setTitle("Events");
return rootView;
}
}
上面的代碼是指,打開該圖像片段
public class imageFragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.imagefragment1, container, false);
return rootView;
}
}
imageFragment1 XML代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@id/imagefrag" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="This is a test"
android:id="@+id/textView"
android:layout_marginTop="41dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
How to replace the activity's fragment from the fragment itself?
我很不確定片段事務應該發生在哪裏。在我的主要活動中,片段交易發生在導航抽屜的開關塊中。我讀過「請注意片段不應該直接替換它自己或其他片段,片段應該是單獨的實體,片段應該做的是通知它的父級活動發生了一些事件,但它不是一個片段工作來決定該怎麼做!它應該是活動,即決定即替換手機上的片段,而是在平板電腦上添加另一個片段。因此,您基本上是通過設計做錯了某些事情「
另一個海報提及「處理這種情況的更好方法是爲主要活動創建回調實現以處理請求,例如啓動新片段」我如何在我的主要活動中設置它以便它與我的子片段進行通信抽屜碎片。任何想法都非常感謝。
主要活動
public class SampleActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerArrowDrawable drawerArrow;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
CustomDrawerAdapter adapter;
List<DrawerItem> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
ActionBar ab = getActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
dataList = new ArrayList<DrawerItem>();
dataList.add(new DrawerItem("Home", R.drawable.home));
dataList.add(new DrawerItem("How It Works", R.drawable.howitworks));
dataList.add(new DrawerItem("Events", R.drawable.events));
dataList.add(new DrawerItem("Profile", R.drawable.signin));
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.navdrawer);
drawerArrow = new DrawerArrowDrawable(this)
{
@Override
public boolean isLayoutRtl() {
return false;
}
};
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
drawerArrow, R.string.drawer_open,R.string.drawer_close)
{
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
dataList);
/* ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, mNames); */
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
{
displayView(position);
// Running Fragment Transaction code here
}
}
// Method that updates content frame with different fragments
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
Bundle args = new Bundle();
switch (position) {
case 0:
fragment = new TitleFragment();
mDrawerToggle.setAnimateEnabled(true);
drawerArrow.setProgress(1f);
mDrawerToggle.syncState();
break;
case 1:
fragment = new HowFragment();
mDrawerToggle.setAnimateEnabled(true);
drawerArrow.setProgress(1f);
mDrawerToggle.syncState();
break;
case 2:
fragment = new EventFragment();
mDrawerToggle.setAnimateEnabled(true);
drawerArrow.setProgress(1f);
mDrawerToggle.syncState();
break;
case 3:
fragment = new ProfileFragment();
mDrawerToggle.setAnimateEnabled(true);
drawerArrow.setProgress(1f);
mDrawerToggle.syncState();
break;
}
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).addToBackStack(null).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(dataList.get(position).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);
}
});
}
@Override
public void setTitle(CharSequence title)
{
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
return false;
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
創建一個'接口',它有一個打開片段的方法,從片斷中創建一個實例併爲其生成setter和調用方法,在activity實現接口和'this'中設置片段實例,瘦句柄片段從活動 –
是否有可能看到代碼示例?我不確定如何實現這一點。 – TheNyquistLimit