我想在我的片段中显示已经显示的另一个片段,当我运行它时会显示错误错误:(31,75)错误:找不到符号方法findViewById(int),是他们的某种方式我可以在片段类中使用findViewByID而不是AppCompatActivity吗?
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);
}
}
我正在调用的片段的代码
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);
}
}
for GroupTwo.java
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 {
@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中
<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
<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
<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
<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" />
答案 0 :(得分:1)
这是我的问题的答案
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
@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 :(得分:0)
示例代码
@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;
}