将框架布局转换为ViewPager

时间:2014-12-10 21:01:52

标签: android android-fragments android-viewpager

在Eclipse Android应用程序中,我有一个页面,在上半部分显示一个片段,下面有一个“ViewPager”。

我想切换位置,所以我在片段上面有ViewPager:在我的XML中,我将“ViewPager”代码块移到“Frame-Layout”代码块之上。

它不起作用。我得到错误:java.lang.ClassCastException:android.widget.FrameLayout无法强制转换为android.support.v4.view.ViewPager 关于如何解决这个问题的任何建议都将不胜感激。

此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" >

        <FrameLayout
           android:id="@+id/fragment_container_mental"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" 
           android:background="#ffff76"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">       
        </android.support.v4.view.ViewPager>

</LinearLayout>

此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.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">       
        </android.support.v4.view.ViewPager>

        <FrameLayout
           android:id="@+id/fragment_container_mental"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" 
           android:background="#ffff76"/>

</LinearLayout>

这是Java代码:

package com.winid.mentalapp;

import java.util.ArrayList;
import java.util.List;

import com.winid.mentalapp.Patient_Fragment_Comments;
import com.winid.mentalapp.Patient_Fragment_Dental;
import com.winid.mentalapp.Patient_Fragment_GFX1;
import com.winid.mentalapp.Patient_Fragment_GFX2;
import com.winid.mentalapp.Patient_Fragment_GFX3;
import com.winid.mentalapp.Patient_Fragment_GFX4;
import com.winid.mentalapp.Patient_Fragment_GFX5;
import com.winid.mentalapp.Patient_Fragment_PEDG1;
import com.winid.mentalapp.Patient_Fragment_PhotoGPS;
import com.winid.mentalapp.Patient_Fragment_UserDef;
import com.winid.mentalapp.MyPageAdapter;
import com.winid.mentalapp.R;
import com.winid.mentalapp.adapter.Patient_TabsPagerAdapter;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import  android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class AntRestAddDetail extends FragmentActivity implements OnClickListener, ActionBar.TabListener {

    private ViewPager viewPager;
    private Patient_TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles

    private String[] tabs = { "Aa", "Bb", "Cc", "Dd", "Ee", "Ff", "Gg", "Hh", "Ii"}
    public Intent i;

    public String t1="", t2="", t3="", t4="", t5="", t6="", t7="", t8="", t9="", t10="";
    public String t11="", t12="", t13="", t14="", t15="", t16="", t17="", t18="", t19="", t20="";
    public String t21="", t22="", t23="", t24="", t25="", t26="", t27="", t28="", t29="", t30="";
    public String t31="", t32="";

    MyPageAdapter pageAdapter;

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.antrestadddetail);

        i = getIntent();

        List<Fragment> fragments = getFragments();
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        actionBar = getActionBar();
        mAdapter = new Patient_TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));            
        }

        //String RecNumb = i.getExtras().getString("id");

        // Create an instance of ExampleFragment
        Patient_Fragment_Mental firstFragment = new Patient_Fragment_Mental();

        firstFragment.setArguments(getIntent().getExtras());

        firstFragment.setArguments(getIntent().getExtras());
        // Add the fragment to the 'fragment_container' FrameLayout
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container_mental, firstFragment).commit();

         // on swiping the viewpager make respective tab selected
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

            public void onTabReselected(Tab tab, FragmentTransaction ft) {
            }

            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                // on tab selected
                // show respected fragment view
                viewPager.setCurrentItem(tab.getPosition());
            }

            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub      
    }
    /*
    private List<Fragment> getFragments(){      
        List<Fragment> fList = new ArrayList<Fragment>();       
        //fList.add(Treatment_Fragment_AnteriorResto.newInstance("Fragment 1"));
        fList.add(MyListFragment.newInstance("Fragment 1"));
        fList.add(Treatment_Fragment_Crown.newInstance("Fragment 2"));  
        fList.add(Treatment_Fragment_Extraction.newInstance("Fragment 3"));
        fList.add(Treatment_Fragment_PostResto.newInstance("Fragment 4"));
        fList.add(Treatment_Fragment_Options.newInstance("Fragment 5"));    
        fList.add(Patient_Fragment_PhotoGPS.newInstance("Fragment 6"));
        fList.add(Patient_Fragment_PEDG1.newInstance("Fragment 7"));
        fList.add(Patient_Fragment_Comments.newInstance("Fragment 8")); 
        fList.add(Patient_Fragment_UserDef.newInstance("Fragment 9"));  
        return fList;
    }
    */

    private List<Fragment> getFragments(){      
        List<Fragment> fList = new ArrayList<Fragment>();       
        fList.add(Patient_Fragment_PEDG1.newInstance("Fragment 1"));
        fList.add(Patient_Fragment_Comments.newInstance("Fragment 2"));
        fList.add(Treatment_Fragment_Options.newInstance("Fragment 3"));    
        fList.add(Patient_Fragment_PhotoGPS.newInstance("Fragment 4"));
        fList.add(Patient_Fragment_UserDef.newInstance("Fragment 5"));  
        return fList;
    }

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());        
    }

    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
        // TODO Auto-generated method stub      
    }

1 个答案:

答案 0 :(得分:0)

我发现有时当我在XML中反转某些视图的顺序时, id 不会更新以匹配。尝试进行干净的构建,重新生成所有 id ,然后再次运行项目。