访问图形页面内片段

访问图形页面内片段

本文介绍了安卓:访问图形页面内片段,ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的浏览pager来显示三种观点。

I am Using View Pager to Display three Views.

"Filter","Streets","Around"

每个视图是由片段定义。

Each View is Defined by Fragment.

是我想要的:我想用图形页面里面我FilterFragment。

WHAT I WANT :I want to Use Mapview Inside My FilterFragment.

我做了什么:

我想用图形页面过滤器内片段,但可能无法成功运行。和获取日志美食错误:

I want to Use MapView Inside Filter Fragment but Could Not Able to Run Successfully. and Getting Log cate Error:

android.view.InflateException:二进制XML文件中的行#33:错误充气类com.google.android.maps.MapView

我还搜查网和#1这个问题,发现 LINK LINK 却苦于无法找到此链接相关的我的问题,因为我使用片段,ViewPager。

I have also Searched on Net and Stackoverflow about this Problem and Found the LINK and LINK but Could not able to Found this link Related to my Problem as i am using Fragment With ViewPager.

我已经把我的code,如下。

I have put my Code as Below.

下面是我的$ C $下MainViewPager.java:

Here is My Code for MainViewPager.java:

    package com.example.viewpageractivitydemo;

    import com.example.viewpageractivitydemo.viewpager.TitlePageIndicator;

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.ViewPager;
    import android.util.Log;

    public class MainViewPager extends FragmentActivity {

        TestFragmentAdapter adapter;
        ViewPager pager;

        @Override
        protected void onCreate(Bundle arg0) {
            // TODO Auto-generated method stub
            super.onCreate(arg0);
            Log.i("MainViewPager","Inside Oncreate");
            setContentView(R.layout.mainviewpager);
            pager = (ViewPager) findViewById(R.id.pager);

            adapter = new TestTitleFragmentAdapter(getSupportFragmentManager());
            pager.setAdapter(adapter);

            TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
            indicator.setViewPager(pager);
            pager.setCurrentItem(1);

        }

        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();

    //      pager.setCurrentItem(1);
        }

    }

下面是$ C $下TestFragmentAdapter.java

Here is Code for TestFragmentAdapter.java

package com.example.viewpageractivitydemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;

public class TestFragmentAdapter extends FragmentPagerAdapter {

    protected static final String[] CONTENT = new String[] { "Filter",
            "Streets", "Around" };
    public String TAG = "TestFragmentAdapter";
    private int mCount = CONTENT.length;

    public TestFragmentAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        Log.i(TAG, "Inside getItemMethod with position " + position);

        if (position == 0) {
            Log.i("TestFragmentAdapter","Inside if Condition");
            return FilterFragment.newInstance(position);
        } else if (position == 1) {
            return StreetsFragment.newInstance(position);
        } else if (position == 2) {
            return AroundFragment.newInstance(position);
        } else {
            return null;
        }
        // return MainFragment.newInstance(CONTENT[position % CONTENT.length]);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mCount;
    }

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }

}

下面是我的$ C $下FilterFragment.java:

Here is My Code for FilterFragment.java:

package com.example.viewpageractivitydemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FilterFragment extends Fragment {

    /*
     * @Override protected Class<? extends Activity> getActivityClass() { //
     * TODO Auto-generated method stub return MyFilterActivity.class; }
     */

    public static Fragment newInstance(int position) {

        Log.i("FilterFragment","Inside New Instasnce");
        FilterFragment filterFragment = new FilterFragment();
        return filterFragment;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.i("FilterFragment","Inside OcCreateView");
        View view = inflater.inflate(R.layout.filter, null);
        return view;
    }

}

下面是我的$ C $下filter.xml:

Here is My Code for filter.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#C6CDD4"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#93A4BA"
            android:gravity="center" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_margin="5dp"
                android:gravity="center"
                android:text="Where Am I"
                android:textColor="#ffffff"
                android:textSize="18dp"
                android:textStyle="bold" >
            </TextView>
        </LinearLayout>

        <com.google.android.maps.MapView
            android:id="@+id/whereami_mapview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:apiKey="0-PaH6rk7RKrZSMd_lqdrZi6FazYyIlX5r6YFyA"
            android:clickable="true" />

        <!--
             <com.google.android.maps.MapView
            android:id="@+id/whereami_mapview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:apiKey="0-PaH6rk7RKrZSMd_lqdrZi6FazYyIlX5r6YFyA" />



        -->
    </LinearLayout>

</LinearLayout>

请任何人都可以推荐我来回答解决我的问题。我花了在这几个小时。还是没有得到关于我的问题的想法。Thnks提前。

Please Can anyone Suggest Me Answer to Solve My Problem. I have Spent many Hours on this. Still not getting any Idea about my Question.Thnks in Advance.

推荐答案

您可以使用 LocalActivityManager 来举办一个活动的片段中。这是一个去precated类,但它提供了简单的解决方案。

You can use LocalActivityManager to host a Activity inside a fragment. It is a deprecated class, but it offers the simplest solution.

下面是$ C $下(在code MyFilterFragment )的片段。 YourMapActivity 是一个扩展 MapActivity

Here is the code for a fragment (MyFilterFragment in your code). YourMapActivity is a basic activity that extends MapActivity.

public class MapFragment extends Fragment {
    private static final String KEY_STATE_BUNDLE = "localActivityManagerState";

    private LocalActivityManager mLocalActivityManager;

    protected LocalActivityManager getLocalActivityManager() {
        return mLocalActivityManager;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle state = null;
        if (savedInstanceState != null) {
            state = savedInstanceState.getBundle(KEY_STATE_BUNDLE);
        }

        mLocalActivityManager = new LocalActivityManager(getActivity(), true);
        mLocalActivityManager.dispatchCreate(state);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            //This is where you specify you activity class
        Intent i = new Intent(getActivity(), YourMapActivity.class);
        Window w = mLocalActivityManager.startActivity("tag", i);
        View currentView=w.getDecorView();
        currentView.setVisibility(View.VISIBLE);
        currentView.setFocusableInTouchMode(true);
        ((ViewGroup) currentView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        return currentView;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle(KEY_STATE_BUNDLE,
                mLocalActivityManager.saveInstanceState());
    }

    @Override
    public void onResume() {
        super.onResume();
        mLocalActivityManager.dispatchResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mLocalActivityManager.dispatchPause(getActivity().isFinishing());
    }

    @Override
    public void onStop() {
        super.onStop();
        mLocalActivityManager.dispatchStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLocalActivityManager.dispatchDestroy(getActivity().isFinishing());
    }
}


编辑:自从这个答案被张贴,谷歌发布了谷歌地图Android的API与片段支持,正式的 MapFragment


Since this answer was posted, Google has released Google Maps Android API with fragment support with an official MapFragment class.

这篇关于安卓:访问图形页面内片段,ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:40