我在我的活动中使用SwipeLayout
。
活动有2个按钮。单击按钮,我想在底部布局上设置相应的片段并以编程方式滑动视图。接下来,应该使用默认片段设置替换底部布局,以便在滑动时显示默认片段。
当我在swipeLayout的addSwipeListener的onClose方法中设置默认片段时,它不会听按钮点击。
这适用于RecyclerView,但现在我想在活动中使用相同的
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="6dp"
xmlns:swipe="http://schemas.android.com/tools">
<com.daimajia.swipe.SwipeLayout
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Bottom View Start-->
<!--What you want to show-->
<LinearLayout
android:id="@+id/sub_item_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/holo_orange_light">
</LinearLayout>
<!-- Bottom View End-->
<!-- Surface View Start -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/frame_list_card_item"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:background="#eefeef"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is the Title this is the Title this is the Title this is the Title this is the Title "
android:textColor="#000"
android:textSize="15sp"/>
<LinearLayout
android:id="@+id/container_list_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="this is description text this is description text this is description text this is description text this is description text this is description text this is description text this is description text this is description text this is description text this is description text "
android:textSize="13sp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:text="4"/>
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:text="5"/>
</LinearLayout>
</LinearLayout>
<!--What you want to show in SurfaceView-->
</LinearLayout>
</LinearLayout>
<!-- Surface View End -->
</com.daimajia.swipe.SwipeLayout>
</android.support.v7.widget.CardView>
JAVA
package com.sp.spsamples3.activities;
public class SwipeLayoutActivity extends AppCompatActivity {
SwipeLayout mSwipeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_layout);
mSwipeLayout = (SwipeLayout) findViewById(R.id.swipe_layout);
getSupportFragmentManager().beginTransaction().replace(R.id.sub_item_container, new DefaultFragment()).commit();
mSwipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
mSwipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onStartOpen(SwipeLayout layout) {
}
@Override
public void onOpen(SwipeLayout layout) {
}
@Override
public void onStartClose(SwipeLayout layout) {
}
@Override
public void onClose(SwipeLayout layout) {
getSupportFragmentManager().beginTransaction().replace(R.id.sub_item_container, new DefaultFragment()).commit();
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
}
});
initClicks();
}
private void initClicks() {
((Button) findViewById(R.id.btn_one)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("sp testing", "1 clicked");
setFragment(new ScreenOneFragment());
mSwipeLayout.open(true);
}
});
((Button) findViewById(R.id.btn_two)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("sp testing", "2 clicked");
setFragment(new ScreenTwoFragment());
mSwipeLayout.open(true);
}
});
}
private void setFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction().replace(R.id.sub_item_container, fragment).commit();
}
}