Fragment中replace方法,导致Activity中的点击切换Fragment事件无法触发
//Activity
package com.example.kaka.dialogtest.ormlitenote;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import com.example.kaka.dialogtest.R;
import com.example.kaka.dialogtest.ormlitenote.fargment.AddItem_Fragment;
import com.example.kaka.dialogtest.ormlitenote.fargment.AllItem_Fragment;
import com.example.kaka.dialogtest.ormlitenote.fargment.My_Fragment;
/**
* Created by kaka on 2018/12/24.
*/
public class Note extends AppCompatActivity implements View.OnClickListener {
private AddItem_Fragment addItem_fragment = new AddItem_Fragment();
private AllItem_Fragment allItem_fragment = new AllItem_Fragment();
private My_Fragment my_fragment = new My_Fragment();
private LinearLayout all_lin;
private LinearLayout add_lin;
private LinearLayout my_lin;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
initView();
this.getSupportFragmentManager()
.beginTransaction()
.add(R.id.main_fragment,allItem_fragment)
.add(R.id.main_fragment,addItem_fragment)
.hide(addItem_fragment)
.add(R.id.main_fragment,my_fragment)
.hide(my_fragment)
.commit();
}
private void initView() {
all_lin = findViewById(R.id.all_lin);
all_lin.setOnClickListener(this);
add_lin = findViewById(R.id.add_lin);
add_lin.setOnClickListener(this);
my_lin = findViewById(R.id.my_lin);
my_lin.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.all_lin:
//执行AddItem_Fragment中replace方法后,下方代码无法触发
this.getSupportFragmentManager()
.beginTransaction()
.show(allItem_fragment)
.hide(addItem_fragment)
.hide(my_fragment)
.commit();
break;
case R.id.add_lin:
this.getSupportFragmentManager()
.beginTransaction()
.show(addItem_fragment)
.hide(allItem_fragment)
.hide(my_fragment)
.commit();
break;
case R.id.my_lin:
this.getSupportFragmentManager()
.beginTransaction()
.show(my_fragment )
.hide(allItem_fragment)
.hide(addItem_fragment)
.commit();
break;
default:
break;
}
}
}
//Fragment
package com.example.kaka.dialogtest.ormlitenote.fargment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.kaka.dialogtest.R;
import com.example.kaka.dialogtest.ormlitenote.Note;
import com.example.kaka.dialogtest.ormlitenote.bean.NoteBean;
import com.example.kaka.dialogtest.ormlitenote.helper.OrmLiteHelper;
import com.j256.ormlite.dao.Dao;
import java.sql.SQLException;
/**
* Created by kaka on 2018/12/24.
*/
public class AddItem_Fragment extends Fragment{
private ImageView add_break,add_over;
private EditText add_title,add_context;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.additem_fragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
}
private void initView() {
add_title = getActivity().findViewById(R.id.add_title);
add_context = getActivity().findViewById(R.id.add_context);
add_break = getActivity().findViewById(R.id.add_break);
add_break.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String title = add_title.getText()+"";
String ctext = add_context.getText()+"";
try {
Dao<NoteBean,Integer> dao = getnoteDao();
NoteBean noteBean = new NoteBean(title,ctext);
if(title!=null&&ctext!=null){
int num = dao.create(noteBean);
Toast.makeText(getActivity(),num+"",Toast.LENGTH_SHORT).show();
}
} catch (SQLException e) {
e.printStackTrace();
}
//遇到问题的地方
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_fragment,new AllItem_Fragment())
.commit();
}
});
add_over = getActivity().findViewById(R.id.add_over);
add_over.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
public OrmLiteHelper gethelper(){
return OrmLiteHelper.getdb(this.getActivity());
}
public Dao<NoteBean,Integer> getnoteDao() throws SQLException {
return gethelper().getDao(NoteBean.class);
}
}
正在回答
如果你前面都是用的show()和hide(),那后面也使用这种方式,原因如下:
Fragment的切换有两种方式:1)replace();2)hide()和show()。
1)replace()方法是把原有的fragment替换掉,其实执行的是原有fragment的remove()被销毁,新的fragment的add(),这种方式一般适用于原有fragment不再需要。
2)hide()和show()方法是可以重用Fragment,原有fragment不会销毁也不会调用生命周期各个方法,这种方式适用于多个fragment的重用或不断切换。
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星