java如何在父布局中多次添加片段?
我想添加一个特殊的布局,直到14个视图。这样布局可以添加到14次,而不是之后。单击下一个目标后,我有一个下一个目标布局。应该添加此布局
因此,我选择了片段。现在我可以添加下一个目标布局的片段onClick。但当我再次点击下一个目的地布局时,什么也没发生。我怎样才能把这个加到14次
我有一个删除目标布局。单击此按钮后,布局应向右滑动,并应通过动画移除。我怎样才能做到这一点
戈博克斯活性
nextDestination.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//addedDestination.setVisibility(View.VISIBLE);
//removeDestination.setVisibility(View.VISIBLE);
fragment = new NextDestinationFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
}
});
片段布局
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:orientation="vertical"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="10dp"
安卓:layout_weight="1.00"
安卓:background="@安卓:color/white"
安卓:layout_marginLeft="15dp"
安卓:layout_marginRight="15dp"
安卓:id="@+id/LinearAddedDestination">
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:text="DESTINATION"
安卓:layout_marginStart="20dp"
安卓:layout_marginTop="20dp" />
<LinearLayout
安卓:layout_width="wrap_content"
安卓:layout_height="40dp"
安卓:orientation="horizontal"
安卓:background="@drawable/shape"
安卓:gravity="right"
安卓:layout_gravity="center|right"
安卓:layout_marginTop="10dp"
安卓:layout_marginBottom="10dp"
安卓:layout_marginLeft="80dp">
<ImageView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:background="@drawable/ic_clear_black_18dp"
安卓:layout_marginLeft="05dp"
安卓:layout_marginTop="05dp" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="REMOVE"
安卓:layout_gravity="center"
安卓:layout_marginLeft="05dp"
安卓:textSize="14sp"
安卓:layout_marginRight="15dp" />
</LinearLayout>
</LinearLayout>
<ImageView
安卓:layout_width="match_parent"
安卓:layout_height="08dp"
安卓:background="@drawable/line2"/>
<LinearLayout
安卓:orientation="horizontal"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
<ImageView
安卓:layout_width="25dp"
安卓:layout_height="25dp"
安卓:layout_marginStart="20dp"
安卓:background="@drawable/ic_place_black_48dp"
安卓:layout_marginTop="05dp"
安卓:layout_gravity="center" />
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:drawableEnd="@drawable/ic_chevron_right_black_24dp"
安卓:layout_marginEnd="10dp"
安卓:text="@string/Loc"
安卓:layout_gravity="center"
安卓:layout_marginTop="05dp"
安卓:textAppearance="@安卓:style/TextAppearance.Medium"
安卓:cursorVisible="false"/>
</LinearLayout>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:hint="Street/Building/Place"
安卓:layout_marginStart="48dp"/>
<LinearLayout
安卓:orientation="horizontal"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
<ImageView
安卓:layout_width="25dp"
安卓:layout_height="25dp"
安卓:layout_marginStart="20dp"
安卓:background="@drawable/ic_description_black_48dp"
安卓:layout_marginTop="10dp" />
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="45dp"
安卓:layout_marginEnd="10dp"
安卓:hint="@string/LocationDetails"/>
</LinearLayout>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:layout_marginStart="48dp"
安卓:hint="@string/HouseDetails" />
<LinearLayout
安卓:orientation="horizontal"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center_horizontal">
<ImageView
安卓:layout_width="25dp"
安卓:layout_height="25dp"
安卓:layout_marginStart="20dp"
安卓:background="@drawable/ic_person_black_48dp"
安卓:layout_marginTop="05dp" />
<TextView
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginEnd="10dp"
安卓:text="@string/additionalContact"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:drawableEnd="@drawable/ic_expand_more_black_24dp"
安卓:layout_marginTop="08dp"
安卓:layout_marginStart="05dp" />
</LinearLayout>
<LinearLayout
安卓:orientation="vertical"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="60dp"
安卓:layout_marginEnd="50dp"
安卓:layout_gravity="center"
安卓:visibility="gone"
安卓:layout_marginTop="20dp">
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:hint="Name"
安卓:layout_gravity="center"/>
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:hint="Phone"
安卓:layout_gravity="center"
/>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="@string/or"
安卓:layout_gravity="center"
安卓:layout_marginTop="20dp" />
<LinearLayout
安卓:layout_width="220dp"
安卓:layout_height="40dp"
安卓:background="@drawable/shape"
安卓:layout_gravity="center_horizontal">
<ImageButton
安卓:layout_width="20dp"
安卓:layout_height="20dp"
安卓:background="@drawable/phone2"
安卓:layout_gravity="center"
安卓:layout_marginStart="30dp" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="@string/addFromContacts"
安卓:layout_gravity="center"
安卓:layout_marginStart="20dp" />
</LinearLayout>
</LinearLayout>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:layout_marginLeft="48dp"
安卓:hint="@string/contact1"
安卓:layout_marginTop="10dp" />
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:focusable="true"
安卓:focusableInTouchMode="true">
<ImageView
安卓:layout_width="25dp"
安卓:layout_height="25dp"
安卓:layout_marginStart="20dp"
安卓:background="@drawable/ic_description_black_48dp"
安卓:layout_marginTop="10dp" />
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="45dp"
安卓:layout_marginEnd="10dp"
安卓:hint="Instruction"
安卓:layout_gravity="center"
安卓:layout_marginLeft="10dp" />
</LinearLayout>
</LinearLayout>
有人能帮忙吗
# 1 楼答案
您应该使用
fragmentManager.beginTransaction().add(R.id.container, fragment)
来添加新的Fragment
,并使用remove()
来删除它对于动画,可以使用
fragmentManager.beginTransaction(). setTransition()
设置添加、删除片段时Android提供的动画。如果要实现自定义动画,可以使用fragmentTransaction.setCustomAnimations(enterAnimation,exitAnimation)
,并将这两个动画写入XML
文件希望这能对你有所帮助
# 2 楼答案
为了管理多个片段,您需要实现FragmentAdapter。然后你必须在某种ListView或ViewPager中放大这些片段。看看this或that教程
使用OnTouchListener,您可以实现滑动手势,从列表/寻呼机中删除片段
希望这有帮助。干杯