2015-03-04 9 views
8

मेरे पास एक गतिविधि है और मैं इस गतिविधि पर framelayout पर कुछ टुकड़े दिखा सकते हैं। मैं फ्रैगमेंट एक्टिविटी के बजाय एक टुकड़े के अंदर टुकड़ों से बना एक व्यूपर रखना चाहता हूं। यह कैसे किया जा सकता है?एंड्रॉइड में खंड के अंदर व्यूपर को कैसे रखा जाए?

उत्तर

31

ViewPager एक टुकड़े में होने के लिए, और ViewPager में खंड होने के लिए, आपको नेस्टेड टुकड़ों का उपयोग करने की आवश्यकता है। यदि आपका minSdkVersion 17 या उच्चतम है, तो टुकड़ों का मूल कार्यान्वयन नेस्टेड टुकड़ों का समर्थन करता है। अन्यथा, आपको support-v13 (या support-v4) टुकड़ों के बैकपोर्ट का उपयोग करने की आवश्यकता होगी।

उसके बाद, आप बस को getChildFragmentManager() देने की आवश्यकता अपने FragmentPagerAdapter या FragmentStatePagerAdapter:

/*** 
    Copyright (c) 2012 CommonsWare, LLC 
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License. You may obtain a copy 
    of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. 

    From _The Busy Coder's Guide to Android Development_ 
    http://commonsware.com/Android 
*/ 

package com.commonsware.android.pagernested; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class PagerFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, 
          Bundle savedInstanceState) { 
    View result=inflater.inflate(R.layout.pager, container, false); 
    ViewPager pager=(ViewPager)result.findViewById(R.id.pager); 

    pager.setAdapter(buildAdapter()); 

    return(result); 
    } 

    private PagerAdapter buildAdapter() { 
    return(new SampleAdapter(getActivity(), getChildFragmentManager())); 
    } 
} 

(this sample project से)

+0

धन्यवाद बहुत, कि उपयोगी है। – Sayed

+0

पूरी तरह से मेरे लिए काम किया, धन्यवाद !! – josher932

+0

पहले मैंने 'getFragmentManager() 'का उपयोग किया था। फिर 'getChildFragmentManager() 'द्वारा बदला गया। एक जादू की तरह काम किया! –

संबंधित मुद्दे