用双片段屏幕替换带有图像的列表视图

时间:2013-06-13 02:52:59

标签: android image android-fragments

我在屏幕上有两个片段(顶部和底部)。我在底部有一个列表视图,我想换掉片段并显示图像。当我点击其中一个列表项时,我收到以下错误:

  06-13 02:14:30.502: E/AndroidRuntime(8457): java.lang.ClassCastException:
  com.ssowens.improvisation.ChapterListActivity cannot be cast to 
  com.ssowens.improvisation.ChapterFragment$Callbacks
  06-13 02:14:30.502: E/AndroidRuntime(8457): at 
  com.ssowens.improvisation.ChapterFragment.onAttach(ChapterFragment.java:51)

我尝试不替换片段并调用另一个活动并将其显示在片段中。这有效,但它将图像放在顶部片段而不是底部。

以下是MainActivity的代码。我调用替换方法“onChapterSelected”中的片段。

    public class ChapterListActivity extends SingleFragmentActivity 
    implements ChapterListFragment.Callbacks, CompatActionBarNavListener {

private static final boolean VERBOSE = true;
private static final String TAG = "ChapterListActivity";
private int mKeySelected;   
boolean mIsDualPane = true; // True for now, may add code later to modify.

private static final int    BFLAT = 1,
                    EFLAT = 2,
                BASS = 3,
                CONCERT = 4,
                VIDEO = 5,
                PRACTICE = 6,   
                SCALES = 7,
                CHAPTERS = 8;

// The key options and chapter index currently being displayed
int mKeyIndex = 0;
int mChapterIndex = 0;

// List of Music Menu Options
final String CATEGORIES[] = { "Chapters", "Scales", "B Flat", "E Flat", 
        "Concert", "Bass", "Video", "Practice" };   

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_twopane);

    // Set up the Action Bar
    int keyIndex = savedInstanceState == null ? 0: 
      savedInstanceState.getInt("keyIndex", 0);
    setUpActionBar(mIsDualPane, keyIndex);

}

@Override
protected Fragment createFragment() {
    if (VERBOSE) Log.v(TAG, "+++ createFragment +++");
        return new ChapterListFragment();
}   

@Override
protected int getLayoutResId() {
     if (VERBOSE) Log.v(TAG, "+++ getLayoutResId +++");
     // This should be flexible for different side screens
     //return R.layout.activity_masterdetail; // But not working
        return R.layout.activity_twopane;
}

public void onChapterSelected(Chapters chapter, int mKeySelected) {
    if (VERBOSE) Log.v(TAG, "+++ onChapterSelected +++");
    View bottomView = findViewById(R.id.fragmentContainer);
    if (bottomView == null && bottomView.getVisibility() == View.VISIBLE) {
        //if (findViewById(R.id.fragmentContainer) != null) {
        if (VERBOSE) Log.v(TAG, "+++ onChapterSelected (findViewById)  +++");
            // Start an instance of ImprovisationPagerActivity
        Intent  i = new Intent(this, ImprovisationPagerActivity.class);         
            i.putExtra(ChapterFragment.EXTRA_CHAPTER_ID, chapter.getId());
            i.putExtra(ChapterFragment.EXTRA_KEYSELECTED, mKeySelected);
            startActivity(i);   
    } else {
        if (VERBOSE) Log.v(TAG, "+++ onChapterSelected (else) +++");
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        Fragment oldDetail = fm.findFragmentById(R.id.fragmentContainer);
        Fragment newDetail = ChapterFragment.newInstance(chapter.getId());

        if (oldDetail != null) {
            ft.remove(oldDetail);
        }   

        ft.add(R.id.fragmentContainer, newDetail);
        ft.commit();            
    } 
}   // end - onChapterSelected


Here is ChapterFragment.newInstance
        public static ChapterFragment newInstance(UUID chapterID) {
    /* This method will attach arguments to a fragment. This must
      * be done after the fragment is created, but before it is 
      * added to an activity. 
      */
    if (VERBOSE) Log.v(TAG, "+++ newInstance +++");

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CHAPTER_ID, chapterID);
    args.putInt(EXTRA_KEYSELECTED, mKeySelected);

    ChapterFragment fragment = new ChapterFragment();
    fragment.setArguments(args);

    return fragment;
} // ChapterFragment

这是activity_twopane.XML文件:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" 
  android:showDividers="middle">

  <FrameLayout  
            android:id="@+id/videoFragmentContainer"
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
            android:layout_weight="2" >       
 </FrameLayout>   

 <FrameLayout   
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

 </FrameLayout>

</LinearLayout>

这是imagefile的activity_framement.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:cropToPadding="true"
    android:background="@android:color/darker_gray"
    android:contentDescription="@string/description"
    />

</LinearLayout>

这是#51行,显示错误发生的位置:

    @Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mCallbacks = (Callbacks)activity;
}

1 个答案:

答案 0 :(得分:1)

您的班级ChapterListActivity实施com.ssowens.improvisation.ChapterListFragment.Callbacks,您希望将其投放到com.ssowens.improvisation.ChapterFragment.Callbacks。这是两个不同的接口。