我正在创建一个具有底部导航视图的简单应用程序。
这将使用户在各种类别之间。这已经与Fragments和案例系统
一起设置然后,我希望这些类别页面上的按钮可以将您带到其他页面,并在每个页面底部显示导航。
当我把我在此应用程序的先前版本上使用的按钮代码时,它失败了
error: cannot find symbol method findViewById(int)
经过一些阅读后,我可以看到这对片段不起作用,但是我找不到任何清楚或易于理解的如何使这些工作。
简而言之,我希望导航面板在我加载的任何页面上保持不变,而不仅仅是我在面板中列出的类别页面。
任何来自按钮的字母页面和其他链接都需要在导航栏上方的面板中打开,但我不知道如何实现这一点。
任何帮助都会......乐于助人。我花了大约12个小时尝试各种事情但没有用。
处理导航片段的代码:
package breed.sheep.british.com.britishsheepbreeds;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import breed.sheep.british.com.britishsheepbreeds.fragment.BreedsFragment;
import breed.sheep.british.com.britishsheepbreeds.fragment.ColoursFragment;
import breed.sheep.british.com.britishsheepbreeds.fragment.DefaultFragment;
import breed.sheep.british.com.britishsheepbreeds.fragment.TerminologyFragment;
import breed.sheep.british.com.britishsheepbreeds.fragment.TypeFragment;
import breed.sheep.british.com.britishsheepbreeds.helper.BottomNavigationBehavior;
public class MainActivity extends AppCompatActivity {
private ActionBar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = getSupportActionBar();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationBehavior.disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
// attaching bottom sheet behaviour - hide / show on scroll
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) navigation.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationBehavior());
// load the default fragment
toolbar.setTitle("British Sheep Breeds");
loadFragment(new DefaultFragment());
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.home:
toolbar.setTitle("British Sheep Breeds");
fragment = new DefaultFragment();
loadFragment(fragment);
return true;
case R.id.breeds:
toolbar.setTitle("Breeds A-Z");
fragment = new BreedsFragment();
loadFragment(fragment);
return true;
case R.id.colour:
toolbar.setTitle("Colours");
fragment = new ColoursFragment();
loadFragment(fragment);
return true;
case R.id.type:
toolbar.setTitle("Type ");
fragment = new TypeFragment();
loadFragment(fragment);
return true;
case R.id.term:
toolbar.setTitle("Terminology ");
fragment = new TerminologyFragment();
loadFragment(fragment);
return true;
}
return false;
}
};
/**
* loading fragment into FrameLayout
*
* @param fragment
*/
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
} }
片段页面
package breed.sheep.british.com.britishsheepbreeds.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import breed.sheep.british.com.britishsheepbreeds.R;
public class DefaultFragment extends Fragment {
public DefaultFragment() {
// Required empty public constructor
}
public static DefaultFragment newInstance(String param1, String param2) {
DefaultFragment fragment = new DefaultFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
上一个按钮代码
Button balwen = (Button) findViewById(R.id.balwen);
balwen.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), balwen.class);
startActivityForResult(myIntent, 0);
}
});
答案 0 :(得分:0)
Button balwenLeft, balwenRight;
FragmentManager fragmentManager;
Fragment fragment;
public Timeline() {
// Required empty public constructor
}
public static Timeline newInstance(String param1, String param2) {
Timeline fragment = new Timeline();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
fragmentManager = this.getActivity().getSupportFragmentManager();
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final LinearLayout l = (LinearLayout) inflater.inflate(R.layout.fragment_colours, container, false);
fragment = new FirstPageOnBreedFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.channelactivitycontainer, fragment).commit();
balwenLeft = (Button) l.findViewById(R.id.btnLeft);
balwenLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragment = new AnotherFragmentWithInBreedFrag();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_container, fragment).commit();
}
});
balwenRight = (Button) l.findViewById(R.id.btnRight);
balwenRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragment = new AnotherFragmentWithInBreedFrag();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_container, fragment).commit();
}
});
return l;
所以这看起来像你想要做的,实际上发生的是从每个片段作为一个类别,你将在布局上创建自定义导航按钮,并从片段调用它时, 不要忘记你需要像我一样膨胀片段的布局,在我的情况下,片段布局的父布局是
LinearLayout
,所以如果你正在使用Relating layout或Coor ......分别改变它们,然后回到导航,发生的事情就是我很难编码它但你可以用你的开关盒功能替换它实际上更简单,所以如果你在BreedsFragment上,你将有两个按钮可以帮助你在BreedFrag中的页面之间导航...一般来说,我们只是替换frame_container
中存在的任何内容,就像底部导航一样,单击按钮时会发生同样的事情,但在这种情况下,你仍然在相同的页面品种,并注意到我假设那些页面也是fragements。我希望你理解,否则,如果你可以在gitHub上分享你的代码,我可以帮助你做出贡献。