How to Call FrameLayout slider in Different Activities in Android

前端 未结 1 1993
不思量自难忘°
不思量自难忘° 2020-12-22 12:08

I have 3 activity say first_activity,second_activity,third_activity.I have used FrameLayout to open a slider .!

Now what I wan

相关标签:
1条回答
  • 2020-12-22 12:23

    You can't open a drawer in a different activity unless you have a drawer for that activity.

    I would suggest you to use fragments.

    Further before deciding on your design i suggest you take a look at this link first

    http://developer.android.com/design/patterns/navigation.html

    In oncreate

    if (savedInstanceState == null){
                getUrl(0);
            }
    

    In onItemClick call getUrl(position)

    Then in getUrl

      protected String getUrl(int position) {
    
        FragmentTransaction fragTran = getFragmentManager()
                                     .beginTransaction();
    
        switch (position) {
        case 0:
                // make one a Fragment
                fragTran.replace(R.id.content_frame, fragment1);
            break;
        case 1:
               // second fragment
               fragTran.replace(R.id.content_frame, fragment2);
    
            break
        case 2:
              // third fragment
              fragTran.replace(R.id.content_frame, fragment3);  
            break;
        }
        // Commit the transaction and close the drawer
        fragTran.commit();  
        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(drawerList);
    
     }  
    

    Example:

    public class Fragment1 extends Fragment {
        String[] titles={"A","B","C"};
        @Override
        public View onCreateView(
                LayoutInflater inflater, 
                ViewGroup container, 
                Bundle savedInstanceState){
            View rootView = inflater.inflate(R.layout.fragment1, container, false);
            return rootView;
        }   
    }
    

    MainActivity.java

    public class MainActivity extends Activity {
    
        // Fields -----------------------------------------------------------------
        private DrawerLayout drawerLayout;
        private ListView drawerList;
        private ActionBarDrawerToggle drawerToggle;
        private MenuListAdapter menuAdapter;
        private int[] icons;
        private Fragment fragment1;
        private Fragment fragment2;
        private Fragment fragment3;
        private CharSequence drawerTitle;
        private CharSequence title;
        private final String[] titles = new String[]{
                "Title Fragment #1",
                "Title Fragment #2",
                "Title Fragment #3"
        };
        private final String[] subtitles = new String[]{
                "Subtitle Fragment #1",
                "Subtitle Fragment #2",
                "Subtitle Fragment #3"
        };
    
        // Lifecycle Callbacks ----------------------------------------------------
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            // Base implemenation
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Instantiate the fragments
            fragment1 = new Fragment1();
            fragment2 = new Fragment2();
            fragment3 = new Fragment3();
    
            // Get the title from this activity
            title = drawerTitle = getTitle();
    
            // Get the icons from the drawables folder
            icons = new int[]{
                    R.drawable.action_about,
                    R.drawable.action_settings,
                    R.drawable.collections_cloud
            };
    
            // Get the drawer layout from the XML file and the ListView inside it
            drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
            drawerList = (ListView)findViewById(R.id.listview_drawer);
    
            // Set a custom shadow over that overlays the main content
            // when the drawer opens
            drawerLayout.setDrawerShadow(
                    R.drawable.drawer_shadow, GravityCompat.START);
    
            // Pass the string arrays to the MenuListAdapter, set the drawer
            // list adapter to it and set up its click listener
            menuAdapter = new MenuListAdapter(
                    MainActivity.this, titles, subtitles, icons);
            drawerList.setAdapter(menuAdapter);
            drawerList.setOnItemClickListener(new DrawerItemClickListener());
    
            // Enable the action bar to have up navigation
            getActionBar().setHomeButtonEnabled(true);
            //getActionBar().setDisplayHomeAsUpEnabled(true);
    
            // Allow the the action bar to toggle the drawer
            drawerToggle = new ActionBarDrawerToggle(
                    this,
                    drawerLayout,
                    R.drawable.ic_drawer,
                    R.string.drawer_open,
                    R.string.drawer_close){
    
                public void onDrawerClosed(View view){
                    super.onDrawerClosed(view);
                }
                public void onDrawerOpened(View view){
                    getSupportActionBar().setTitle(drawerTitle);
                    super.onDrawerOpened(view);
                }
            };
            drawerLayout.setDrawerListener(drawerToggle);
    
            // If this is the first time opening this activity,
            // start with loading fragment #1
            if (savedInstanceState == null){
                selectItem(0);
            }       
    
        }
    
        // Methods ----------------------------------------------------------------
        @Override
        public boolean onOptionsItemSelected(MenuItem item){
    
            // If the user has pressed the action bar icon
            if (item.getItemId() == android.R.id.home){
    
                // If the drawer is open, close it; vice versa
                if (drawerLayout.isDrawerOpen(drawerList)){
                    drawerLayout.closeDrawer(drawerList);
                } else {
                    drawerLayout.openDrawer(drawerList);
                }
            }
    
            // Finish by letting the super class do the rest
            return super.onOptionsItemSelected(item);
    
        }
        @Override
        protected void onPostCreate(Bundle savedInstanceState){
    
            // Call the super implementation and synchronize the drawer
            super.onPostCreate(savedInstanceState);
            drawerToggle.syncState();
    
        }
        @Override
        public void onConfigurationChanged(Configuration newConfig){
    
            // Call the super implemenation on this activity
            // and the drawer toggle object
            super.onConfigurationChanged(newConfig);
            drawerToggle.onConfigurationChanged(newConfig);
    
        }
        private void selectItem(int position){
    
            // Create a new fragment transaction and start it
            FragmentTransaction fragTran = getFragmentManager()
                                           .beginTransaction();
    
            // Locate the position selected replace the content view
            // with the fragment of the number selected
            switch (position){
                case 0:{
                    fragTran.replace(R.id.content_frame, fragment1);
                    break;
                }
                case 1:{
                    fragTran.replace(R.id.content_frame, fragment2);
                    break;
                }
                case 2:{
                    fragTran.replace(R.id.content_frame, fragment3);
                    break;
                }
            }
    
            // Commit the transaction and close the drawer
            fragTran.commit();
            drawerList.setItemChecked(position, true);
            drawerLayout.closeDrawer(drawerList);
    
        }
        public void setTitle(CharSequence title){
    
            // Save the passed in title and set the action bar title
            this.title = title;
            getActionBar().setTitle(title);
    
        }
    
        // Classes ----------------------------------------------------------------
        private class DrawerItemClickListener 
        implements ListView.OnItemClickListener{
    
            @Override
            public void onItemClick(
                    AdapterView<?> parent, 
                    View view, 
                    int position,
                    long id) {
    
                // When clicked, select open the appropriate fragment
                selectItem(position);
    
            }
    
        }
    
    }
    

    Fragment2.java

    public class Fragment2 extends Fragment {
        @Override
        public View onCreateView(
                LayoutInflater inflater,
                ViewGroup container,
                Bundle savedInstanceState){
            View rootView = inflater.inflate(R.layout.fragment2, container, false);
            return rootView;
        }
    }
    

    Fragment3.java

    public class Fragment3 extends Fragment {
        @Override
        public View onCreateView(
                LayoutInflater inflater,
                ViewGroup container,
                Bundle savedInstanceState){
            View rootView = inflater.inflate(R.layout.fragment3, container, false);
            return rootView;
        }
    }
    

    activity_main.xml

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout 
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <ListView 
            android:id="@+id/listview_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"/>
    
    </android.support.v4.widget.DrawerLayout>
    

    drawer_list.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:background="@android:color/darker_gray"
        android:orientation="horizontal"
        style="?attr/dropdownListPreferredItemHeight" >
    
        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"/>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical|left"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                style="?attr/spinnerDropDownItemStyle"/>
            <TextView 
                android:id="@+id/subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                style="?attr/spinnerDropDownItemStyle"/>
    
        </LinearLayout>    
    
    </LinearLayout>
    

    Have framen1.xml,framgent2.xml and fragment3.xml and customize the way you want.

    0 讨论(0)
提交回复
热议问题