首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在公共类ItemThreeFragment扩展片段中使用findViewById

如何在公共类ItemThreeFragment扩展片段中使用findViewById
EN

Stack Overflow用户
提问于 2017-12-07 12:58:06
回答 2查看 140关注 0票数 0

我想在我的片段中显示另一个片段,当我运行它时,它会显示一个错误:(31,75) error:找不到符号方法findViewById(int),它们是某种我可以在片段类中使用findViewByID而不是AppCompatActivity的方式吗?

代码语言:javascript
运行
复制
ItemThreeFragment.java

public class ItemThreeFragment extends Fragment {


private BottomNavigationView bottomNavigationView;

public static ItemThreeFragment newInstance() {
    ItemThreeFragment fragment = new ItemThreeFragment();
    return fragment;
}

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

    BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.NavGroup);

    bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener(){

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment1 = null;
                    switch (item.getItemId()){
                        case R.id.groupJoined:
                            selectedFragment1 = GroupOne.newInstance();
                            break;
                        case R.id.createGroup:
                            selectedFragment1 = GroupTwo.newInstance();
                            break;
                    }

                    FragmentTransaction trans = getChildFragmentManager().beginTransaction();
                    trans.replace(R.id.groupFrame_layout, selectedFragment1);
                    trans.commit();
                    return true;

                }
            });


}

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

    return inflater.inflate(R.layout.fragment_item_three, container, false);

}
}

我正在调用的片段的代码

代码语言:javascript
运行
复制
GroupOne.java

public class GroupOne extends Fragment {
public static GroupOne newInstance() {
    GroupOne fragment = new GroupOne();
    return fragment;
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_group_one, container, false);
}
}

对于GroupTwo.java

代码语言:javascript
运行
复制
public class GroupTwo extends Fragment {
public static GroupTwo newInstance() {
    GroupTwo fragment = new GroupTwo();
    return fragment;
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_group_two, container, false);
}
}

这是我的主要活动

公共类MainActivity扩展了AppCompatActivity {

代码语言:javascript
运行
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.NavBot);

    bottomNavigationView.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;
                    switch (item.getItemId()) {
                        case R.id.home:
                            selectedFragment = ItemOneFragment.newInstance();
                            break;
                        case R.id.search:
                            selectedFragment = ItemTwoFragment.newInstance();
                            break;
                        case R.id.groups:
                            selectedFragment = ItemThreeFragment.newInstance();
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout, selectedFragment);
                    transaction.commit();
                    return true;
                }
            });

    //Manually displaying the first fragment - one time only
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout, ItemOneFragment.newInstance());
    transaction.commit();

    //Used to select an item programmatically
    //bottomNavigationView.getMenu().getItem(2).setChecked(true);
}

activity_main.xml

代码语言:javascript
运行
复制
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.findforme.www.myapplication.MainActivity">

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/navigation"
    android:animateLayoutChanges="true"
    android:layout_below="@+id/bottomNavigationView">
</FrameLayout>

<android.support.design.widget.BottomNavigationView

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/NavBot"
    android:layout_gravity="bottom"
    app:menu="@menu/menu_nav"
    android:background="@color/Lime"
    app:itemIconTint="@android:color/white"
    app:itemTextColor="@android:color/white"></android.support.design.widget.BottomNavigationView>

fragment_item_three.xml

代码语言:javascript
运行
复制
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:weightSum="1">

    <SearchView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/searchGroup"
        android:scrollbarSize="10dp"/>

    <android.support.design.widget.BottomNavigationView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/NavGroup"
        android:layout_gravity="bottom"
        app:menu="@menu/menu_group"
        android:background="@color/Lime"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"></android.support.design.widget.BottomNavigationView>

    <FrameLayout
        android:id="@+id/groupFrame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"
        android:animateLayoutChanges="true"
        android:layout_below="@+id/bottomNavigationView">
    </FrameLayout>

</LinearLayout>

fragment_group_one.xml

代码语言:javascript
运行
复制
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.findforme.www.myapplication.GroupOne">

<!-- TODO: Update blank fragment layout -->

<TextView
    android:text="Hay salamat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView3" />

fragment_group_two.xml

代码语言:javascript
运行
复制
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.findforme.www.myapplication.GroupTwo">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hay Humana" />

EN

回答 2

Stack Overflow用户

发布于 2017-12-07 13:00:52

示例代码

代码语言:javascript
运行
复制
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_group_two, container, false);
    TextView tv = (TextView) view.findViewById(R.id.tv)
    return view;
}
票数 1
EN

Stack Overflow用户

发布于 2017-12-07 13:41:21

这就是我问题的答案。

代码语言:javascript
运行
复制
 private void findViews(View view)
{
    bottomNavigationView = (BottomNavigationView)view.findViewById(R.id.NavGroup);
    bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener(){

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment1 = null;
                    switch (item.getItemId()){
                        case R.id.groupJoined:
                            selectedFragment1 = GroupOne.newInstance();
                            break;
                        case R.id.createGroup:
                            selectedFragment1 = GroupTwo.newInstance();
                            break;
                    }

                    FragmentTransaction trans = getFragmentManager().beginTransaction();
                    trans.replace(R.id.groupFrame_layout, selectedFragment1);
                    trans.commit();
                    return true;

                }
            });
}

然后在onCreateView中添加

代码语言:javascript
运行
复制
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_item_three, container, false);
    findViews(view);
    return view;

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47687892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档