gpt4 book ai didi

java - 片段 - 标签、条件和展示

转载 作者:行者123 更新时间:2023-11-30 12:04:38 24 4
gpt4 key购买 nike

我将非常感谢分析这些代码行,因为我不明白它们之间的关系。特别是如果我我会将标签更改为 2 个不同的标签,应用程序中没有任何变化。另外,如果条件if去掉,也没有变化。评论里我有简短的翻译,这三行我还是看不懂。

Fragment fr = manager.findFragmentByTag("AddCity");

这个条件满足什么作用?

if (fr != null) {// if ???
manager.beginTransaction().remove(fr).commit();
}

和:

fr2.show(manager, "AddCity");  

这是完整的代码片段

/*
I create a variable manager, and then I get a FragmentManager and assign it to the variable manager,
thanks to the variable manager, I can run actions on fragments
*/
FragmentManager manager = getSupportFragmentManager();

Fragment fr = manager.findFragmentByTag("AddCity");//I created fr variable and ???
Fragment1 fr1 = (Fragment1)
manager.findFragmentById(R.id.list); // I created fr1 variable and assigned fragment list to the variable fr1
if (view.equals(b1)) {
if (fr != null) {// if ???
manager.beginTransaction().remove(fr).commit();//Creates a new transaction to change fragments at runtime, removes an existing fragment, plans to commit the transaction.
}
Fragment2 fr2 = new Fragment2();//Created object Fragment2
fr2.show(manager, "AddCity");//Display the dialog box by adding the fragment to the given FragmentManager.
}

编辑:

这是我的主要 Activity :

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


Button b1, b2;
static int itemSelected;
static boolean isSelected;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1 = findViewById(R.id.button1);
b2 = findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}

@Override
public void onClick(View view) {

FragmentManager manager = getSupportFragmentManager();//
Fragment fr = manager.findFragmentByTag("addCity");
Fragment1 fr1 = (Fragment1)
getSupportFragmentManager().findFragmentById(R.id.list);
if (view.equals(b1)) {
if (fr != null) {
manager.beginTransaction().remove(fr).commit();
}
Fragment2 fr2 = new Fragment2();
fr2.show(manager, "");
}
if (view.equals(b2)) {
if (!fr1.cities.isEmpty()) {
if (isSelected) {
fr1.cities.remove(itemSelected);
fr1.adapter.notifyDataSetChanged();
itemSelected = 0;
isSelected = false;
fr1.getListView()
.setSelector(android.R.color.transparent);
} else {
Toast.makeText(this, "Select city to be deleted",
Toast.LENGTH_LONG).show();
}
}
}

}
}

片段 1:

public class Fragment1 extends ListFragment {

ArrayList<String> cities = new ArrayList<>();
ArrayAdapter<String> adapter;
String name;

public Fragment1() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment1,
viewGroup, false);
adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, cities);
setListAdapter(adapter);

return view;
}

public void onListItemClick(ListView listView, View view,
int position, long id) {
MainActivity.itemSelected = position;
MainActivity.isSelected = true;
getListView().setSelector(android.R.color.holo_blue_dark);
}

}

片段2:

public class Fragment2 extends DialogFragment {

EditText et;
public Fragment2() {
// Required empty public constructor
}


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

public void onViewCreated(View view, Bundle savedInstanceState) {

et = view.findViewById(R.id.editText);
Button b = view.findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String country = et.getText().toString();
Fragment1 fr1 = (Fragment1)
getFragmentManager().findFragmentById(R.id.list);
if (!country.equals("")) {
fr1.cities.add(country);
fr1.adapter.notifyDataSetChanged();
}
getActivity().getSupportFragmentManager().beginTransaction()
.remove(Fragment2.this).commit();
}
});
}

}

最佳答案

您正在添加另一个同名片段。您可以尝试将名称更改为其他名称并尝试吗?

喜欢

fr2.show(manager, "Add District");

这样你就可以找到不同之处。

添加片段

Fragment fr2 = new Fragment();

FragmentManager manger = getSupportFragmentManager();

FragmentTransaction ft = manger.beginTransaction();

ft.add(R.id.placeHolder, fr2,tag);

ft.commitAllowingStateLoss();

if(fr2.isAdded()){

fr2.show(manager, "");

}

在调用 show 之前,您必须添加片段。

关于java - 片段 - 标签、条件和展示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57033129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com