level 1
车前卒_forever
楼主
解决了一个上午没有解决的fragment问题。一开始是关于fragment继承要实例化的否则不能打包为apk的问题,后来实例化了却出现了如图片的三个问题





下面是写的mainactivity.java
public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
//UI Object
private TextView txt_upbar;
private TextView txt_main;
private TextView txt_suggest;
private TextView txt_news;
private TextView txt_chat;
private TextView txt_me;
private FrameLayout ly_content;
//Fragment Object
private MyFragment fg1,fg2,fg3,fg4,fg5;
private FragmentManager fManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
fManager = getFragmentManager();
bindViews();
txt_main.performClick(); //模拟一次点击,既进去后选择第一项
}
//UI组件初始化与事件绑定
private void bindViews() {
txt_upbar = (TextView) findViewById(R.id.txt_upbar);
txt_main = (TextView) findViewById(R.id.txt_main);
txt_suggest = (TextView) findViewById(R.id.txt_suggest);
txt_news = (TextView) findViewById(R.id.txt_news);
txt_chat = (TextView) findViewById(R.id.txt_chat);
txt_me = (TextView) findViewById(R.id.txt_me);
ly_content = (FrameLayout) findViewById(R.id.ly_content);
txt_main.setOnClickListener(this);
txt_suggest.setOnClickListener(this);
txt_news.setOnClickListener(this);
txt_chat.setOnClickListener(this);
txt_me.setOnClickListener(this);
}
//重置所有文本的选中状态
private void setSelected(){
txt_main.setSelected(false);
txt_suggest.setSelected(false);
txt_news.setSelected(false);
txt_chat.setSelected(false);
txt_me.setSelected(false);
}
//隐藏所有Fragment
private void hideAllFragment(FragmentTransaction fragmentTransaction){
if(fg1 != null)fragmentTransaction.hide(fg1);
if(fg2 != null)fragmentTransaction.hide(fg2);
if(fg3 != null)fragmentTransaction.hide(fg3);
if(fg4 != null)fragmentTransaction.hide(fg4);
if(fg5 != null)fragmentTransaction.hide(fg5);
}
@Override
public void onClick(View v) {
FragmentTransaction fTransaction = fManager.beginTransaction();
hideAllFragment(fTransaction);
switch (v.getId()){
case R.id.txt_main:
setSelected();
txt_main.setSelected(true);
if(fg1 == null){
fg1 = new MyFragment ("第一个Fragment");
fTransaction.add(R.id.ly_content,fg1);
}else{
fTransaction.show(fg1);
}
break;
case R.id.txt_suggest:
setSelected();
txt_suggest.setSelected(true);
if(fg2 == null){
fg2 = new MyFragment("第二个Fragment");
fTransaction.add(R.id.ly_content,fg2);
}else{
fTransaction.show(fg2);
}
break;
case R.id.txt_news:
setSelected();
txt_news.setSelected(true);
if(fg3 == null){
fg3 = new MyFragment("第三个Fragment");
fTransaction.add(R.id.ly_content,fg3);
}else{
fTransaction.show(fg3);
}
break;
case R.id.txt_chat:
setSelected();
txt_chat.setSelected(true);
if(fg4 == null){
fg4 = new MyFragment("第四个Fragment");
fTransaction.add(R.id.ly_content,fg4);
}else{
fTransaction.show(fg4);
}
break;
case R.id.txt_me:
setSelected();
txt_me.setSelected(true);
if(fg5 == null){
fg5 = new MyFragment("第五个Fragment");
fTransaction.add(R.id.ly_content,fg5);
}else{
fTransaction.show(fg5);
}
break;
}
fTransaction.commit();
}
}
大部分都改编自教程
然后是fragment.java
public class MyFragment extends Fragment {
private String content;
public static MyFragment newinstance(String content) {
MyFragment newFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("content", content);
newFragment.setArguments(bundle);
return newFragment;
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fg_content,container,false);
TextView txt_content = (TextView) view.findViewById(R.id.txt_content);
txt_content.setText(content);
return view;
}
}
fragment未报错然而我觉得问题应该就是出在这两个.java上。
——————————————————————————————
改了快一整天了,求大神指教。
2017年03月13日 05点03分
1





下面是写的mainactivity.javapublic class Main2Activity extends AppCompatActivity implements View.OnClickListener{
//UI Object
private TextView txt_upbar;
private TextView txt_main;
private TextView txt_suggest;
private TextView txt_news;
private TextView txt_chat;
private TextView txt_me;
private FrameLayout ly_content;
//Fragment Object
private MyFragment fg1,fg2,fg3,fg4,fg5;
private FragmentManager fManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
fManager = getFragmentManager();
bindViews();
txt_main.performClick(); //模拟一次点击,既进去后选择第一项
}
//UI组件初始化与事件绑定
private void bindViews() {
txt_upbar = (TextView) findViewById(R.id.txt_upbar);
txt_main = (TextView) findViewById(R.id.txt_main);
txt_suggest = (TextView) findViewById(R.id.txt_suggest);
txt_news = (TextView) findViewById(R.id.txt_news);
txt_chat = (TextView) findViewById(R.id.txt_chat);
txt_me = (TextView) findViewById(R.id.txt_me);
ly_content = (FrameLayout) findViewById(R.id.ly_content);
txt_main.setOnClickListener(this);
txt_suggest.setOnClickListener(this);
txt_news.setOnClickListener(this);
txt_chat.setOnClickListener(this);
txt_me.setOnClickListener(this);
}
//重置所有文本的选中状态
private void setSelected(){
txt_main.setSelected(false);
txt_suggest.setSelected(false);
txt_news.setSelected(false);
txt_chat.setSelected(false);
txt_me.setSelected(false);
}
//隐藏所有Fragment
private void hideAllFragment(FragmentTransaction fragmentTransaction){
if(fg1 != null)fragmentTransaction.hide(fg1);
if(fg2 != null)fragmentTransaction.hide(fg2);
if(fg3 != null)fragmentTransaction.hide(fg3);
if(fg4 != null)fragmentTransaction.hide(fg4);
if(fg5 != null)fragmentTransaction.hide(fg5);
}
@Override
public void onClick(View v) {
FragmentTransaction fTransaction = fManager.beginTransaction();
hideAllFragment(fTransaction);
switch (v.getId()){
case R.id.txt_main:
setSelected();
txt_main.setSelected(true);
if(fg1 == null){
fg1 = new MyFragment ("第一个Fragment");
fTransaction.add(R.id.ly_content,fg1);
}else{
fTransaction.show(fg1);
}
break;
case R.id.txt_suggest:
setSelected();
txt_suggest.setSelected(true);
if(fg2 == null){
fg2 = new MyFragment("第二个Fragment");
fTransaction.add(R.id.ly_content,fg2);
}else{
fTransaction.show(fg2);
}
break;
case R.id.txt_news:
setSelected();
txt_news.setSelected(true);
if(fg3 == null){
fg3 = new MyFragment("第三个Fragment");
fTransaction.add(R.id.ly_content,fg3);
}else{
fTransaction.show(fg3);
}
break;
case R.id.txt_chat:
setSelected();
txt_chat.setSelected(true);
if(fg4 == null){
fg4 = new MyFragment("第四个Fragment");
fTransaction.add(R.id.ly_content,fg4);
}else{
fTransaction.show(fg4);
}
break;
case R.id.txt_me:
setSelected();
txt_me.setSelected(true);
if(fg5 == null){
fg5 = new MyFragment("第五个Fragment");
fTransaction.add(R.id.ly_content,fg5);
}else{
fTransaction.show(fg5);
}
break;
}
fTransaction.commit();
}
}
大部分都改编自教程
然后是fragment.java
public class MyFragment extends Fragment {
private String content;
public static MyFragment newinstance(String content) {
MyFragment newFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("content", content);
newFragment.setArguments(bundle);
return newFragment;
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fg_content,container,false);
TextView txt_content = (TextView) view.findViewById(R.id.txt_content);
txt_content.setText(content);
return view;
}
}
fragment未报错然而我觉得问题应该就是出在这两个.java上。
——————————————————————————————
改了快一整天了,求大神指教。