在选项卡布局的片段内部访问自动完成

问题描述 投票:0回答:1

以下是我的onCreate代码:

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

        Log.i(TAG, "Inside Base Drawer...");

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);

        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager){

            @Override
            public void onTabSelected(TabLayout.Tab tab){
                tabPosition = tab.getPosition();
            }
        });

    }

以下是我的FragmentPageAdapter,用于滑动选项卡布局

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                Tab1Fragment tab1 = new Tab1Fragment();
                return tab1;
            case 1:
                Tab1Fragment tab2 = new Tab2Fragment();
                return tab2;
            case 2:
                Tab1Fragment tab3 = new Tab3Fragment();
                return tab3;
            default:
                return null;
        }

    }

    @Override
    public int getCount() {
        return 3;
    }
}

以下是tabFragment代码:

public class Tab1Fragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState){
        return inflater.inflate(R.layout.tab_activity_sheet_details_tab1, viewGroup, false);
    }
}

我在选项卡布局内有一个自定义的autocompleteTextView(tab_activity_sheet_details_tab1)以下是相同的代码:

<com.example.user.package.TabLayoutActivity.AutoCompleteAdapter.HistoryAutoCompleteTextView
            android:id="@+id/history_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textCapCharacters"
            android:imeOptions="flagNoExtractUi|actionSearch"/>

但是,我无法从TabLayoutActivity访问此元素。放大片段视图后,访问此内容的最佳方法是什么?并请注意,所有三个选项卡布局中都有相似的项目。是否需要一个单独的适配器来容纳所有三个片段布局并进行访问?如果是这样,我可以从哪个生命周期元素访问它们?附言:我正在寻找对片段的非适配器访问(如果可能)(可能通过“布局ID”)?

android android-fragments autocompletetextview android-tabs
1个回答
0
投票

这样的事情会使视图暴露于外界:

public class Tab1Fragment extends Fragment {
    View mView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState){
        mView = inflater.inflate(R.layout.tab_activity_sheet_details_tab1, viewGroup, false);
        return mView;
    }

    public HistoryAutoCompleteTextView getTextView() {
      return (HistoryAutoCompleteTextView)mView.findViewById(R.id. history_text);
    }
}

然后您可以从片段中获取它:

   textView = ((Tab1Fragment)fragment).getTextView()

但是从软件设计的角度来看,您可能不应该这样做,而应考虑完全在Fragment中管理此视图,而只公开收集的数据。

© www.soinside.com 2019 - 2024. All rights reserved.