English 中文(简体)
Android - 基于同一类的多碎片
原标题:Android - Multiple fragments based on the same class

我正在开发一种安卓应用程序,其中我需要在 Fradition Affority 内有基于同一等级的多个碎片。 目前,我的代码主要基于官方文档中的http:// developmenter.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/ap/fragmentTabsPager.html' rel=“nofollown noreferrrr” > 。 这个示例是 < a >, 除了我对每个碎片都立即将相同的类别划为:

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

    setContentView(R.layout.fragment_tabs_pager);
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mViewPager = (ViewPager)findViewById(R.id.pager);

    mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

    mTabsAdapter.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1"), MyFragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2"), MyFragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3"), MyFragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("tab4").setIndicator("Tab 4"), MyFragment.class, null);

    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
    }
}

MyFrashment 很简单 :

public class MyFragment extends Fragment {

    View v;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (container == null) {
                return null;
        }

        v = inflater.inflate(R.layout.test, container, false);
        return v;
    }
}

我简化了分层类所膨胀的布局, 改成简单的文字视图:

<?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:orientation="vertical">

<TextView
    android:id="@+id/test_textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/some_default_value"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

So, the thing is, I would like to ba able to modify the value of test_textView1 separately in each fragment, after they have been instantiated. My problem is that I don t manage to access to the elements of the layout of a fragment. I cannot directly use the findViewById() method since it would affect all the fragments. Inside the TabsAdapter class, I tried to access a single fragment, and then access its View by doing the following:

getItem(number).getView();

但它返回了 null ( getThomatter () 返回了正确的碎片, 但 getView () 返回了空号 ) 。

我还读了>>这个解决方案 ,但如果我在我的 Frashment 中宣布文本View,则添加一种方法 setTextView(Strings) ,并从 Fradition Affactivity 中称之为它,它产生一个NullpokerException。

那么,有没有具体的方法可以 单独访问我碎片中的布局项目?谢谢!

最佳回答

我终于又重新开始了, 并以为基础,

问题回答

如果您有碎片 : frag1, frag2, frag3, 等。 您可以使用 frag1. getView (). findViewById () 来将范围限制在仅此碎片上, 因为每个碎片都延伸同一类 。

示例:

TextView frag1text = (TextView)frag1.getView().findViewById();

在“ Create View of your fits” 中, 请记住要将指针保存到视图中。 然后您总是可以说出我的Pointer. finddViewById () ; 以便获得您视图的子项 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签