Java 如何删除正确的片段实例
我最近开始开发android应用程序,遇到了一个我找不到解决方案的问题 当用户按下按钮时,活动将向表中添加一个片段。然后,用户可以添加任意数量的该片段实例(每个实例都显示在表中的新行上)。这很好用 当用户单击按钮删除片段时,问题就出现了。每个片段都有自己的删除按钮,但无论用户单击哪一个按钮,最后一行总是被删除。如何使其删除正确的片段 下面是所有相关代码,包括片段的创建。如果我做错了,请告诉我,告诉我应该怎么做 对某些语法的解释将非常有用 主要活动:Java 如何删除正确的片段实例,java,android,android-fragments,gradle,android-gradle-plugin,Java,Android,Android Fragments,Gradle,Android Gradle Plugin,我最近开始开发android应用程序,遇到了一个我找不到解决方案的问题 当用户按下按钮时,活动将向表中添加一个片段。然后,用户可以添加任意数量的该片段实例(每个实例都显示在表中的新行上)。这很好用 当用户单击按钮删除片段时,问题就出现了。每个片段都有自己的删除按钮,但无论用户单击哪一个按钮,最后一行总是被删除。如何使其删除正确的片段 下面是所有相关代码,包括片段的创建。如果我做错了,请告诉我,告诉我应该怎么做 对某些语法的解释将非常有用 主要活动: public class WorkingOut
public class WorkingOut extends AppCompatActivity {
private int numOfSets = 0;
private static FragmentManager fm;
...
private void initialize(){
...
fm = getSupportFragmentManager();
}
public void addSet(View view) {
WorkoutSets newSet = new WorkoutSets();
fm.beginTransaction().add(R.id.set_container, newSet, "set_" + numOfSets).addToBackStack(null).commit();
}
public static void removeSet(String tag){
Fragment frag = fm.findFragmentByTag(tag);
fm.beginTransaction().detach(frag).commit();
//fm.beginTransaction().remove(frag).commit();
}
public class WorkoutSets extends AppCompatDialogFragment {
public WorkoutSets() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_workout_sets, null);
Button button = (Button) view.findViewById(R.id.delete_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WorkingOut.removeSet(getTag());
}
});
return view;
}
@Override
public void onDetach() {
super.onDetach();
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
片段活动:
public class WorkingOut extends AppCompatActivity {
private int numOfSets = 0;
private static FragmentManager fm;
...
private void initialize(){
...
fm = getSupportFragmentManager();
}
public void addSet(View view) {
WorkoutSets newSet = new WorkoutSets();
fm.beginTransaction().add(R.id.set_container, newSet, "set_" + numOfSets).addToBackStack(null).commit();
}
public static void removeSet(String tag){
Fragment frag = fm.findFragmentByTag(tag);
fm.beginTransaction().detach(frag).commit();
//fm.beginTransaction().remove(frag).commit();
}
public class WorkoutSets extends AppCompatDialogFragment {
public WorkoutSets() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_workout_sets, null);
Button button = (Button) view.findViewById(R.id.delete_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WorkingOut.removeSet(getTag());
}
});
return view;
}
@Override
public void onDetach() {
super.onDetach();
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
主活动表XML:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rest_for_message"
android:layout_marginTop="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/set_parent"
android:fadeScrollbars="false">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/set_container"
android:minHeight="10dp">
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Set"
android:id="@+id/add_set"
android:onClick="addSet"
android:layout_centerHorizontal="true"
android:layout_below="@+id/set_container"
android:layout_marginBottom="10dp"/>
</RelativeLayout>
</ScrollView>
片段删除按钮XML:
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="x"
android:id="@+id/delete_button"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"/>
固定的
以下两种解决方案都有效。
我觉得有点傻,因为我专门设置了标签系统,这样每个人都有自己的标签,然后忘记了增加它--
多亏了这两个答案。在您的addSet()
方法中,您正在添加具有相同标记名的每个片段。因此,在删除任何片段时,仅删除最后添加的(位于堆栈顶部)片段。
您可以将其更改为以下内容
public void addSet(View view) {
WorkoutSets newSet = new WorkoutSets();
fm.beginTransaction().add(R.id.set_container, newSet, "set_" + numOfSets++).addToBackStack(null).commit();
}
希望这有帮助。既然您说每个片段都包含删除按钮,那么您真的不必做那么多。片段有自己的“getFragmentManager()”方法,该方法返回与放置它们的FragmentManager完全相同的FragmentManager 因此,在delete方法中,只需执行以下操作:
Button button = (Button) view.findViewById(R.id.delete_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.remove(WorkoutSets.this).commit();
}
});