Viewpager:从左向右滑动的问题
Viewpager: problems with left-to-right swipe
我在 LinearLayout 中有一个 ViewPager。当我滑动到下一个片段(从右到左)时它工作正常,我可以在任何地方触摸屏幕并且它工作。但是当我尝试返回上一个片段时,从左向右滑动,它只适用于屏幕左侧的狭窄部分(图片上的黄色区域):
我的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/palette_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:windowSoftInputMode="adjustPan">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:windowSoftInputMode="adjustPan"
tools:context="com.internet_of_everything.chineesecards.MainActivity">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="0.85"
android:baselineAligned="false"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1">
<ImageButton
android:id="@+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="?android:attr/windowBackground"
android:contentDescription="@android:string/untitled"
android:visibility="visible"
app:srcCompat="@drawable/button_back" />
<ImageButton
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:adjustViewBounds="false"
android:background="?android:attr/windowBackground"
android:contentDescription="@android:string/untitled"
android:elevation="0dp"
android:visibility="visible"
app:srcCompat="@drawable/button_next" />
</LinearLayout>
</LinearLayout>
更新
在我的片段中,我有 2 个线性布局:EditText 和 TextView。同时只能显示 EditText 或 TextView。
我有 onTouchListener:当显示 EditText 时,它会隐藏键盘并在用户单击此 edittext 外部时检查 EditText 的内容。
那么我可以使用这个 onTouchListener 并解决从左到右滑动的问题吗?
public class OneCardFragment extends Fragment {
public OneCardFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
//hides keyboard
imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
//checks edittext content
}
});
}
}
Return false 其他视图也有机会处理该事件。
public class OneCardFragment extends Fragment {
public OneCardFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
//hides keyboard
imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
//checks edittext content
return false;
}
});
}
}
我在 LinearLayout 中有一个 ViewPager。当我滑动到下一个片段(从右到左)时它工作正常,我可以在任何地方触摸屏幕并且它工作。但是当我尝试返回上一个片段时,从左向右滑动,它只适用于屏幕左侧的狭窄部分(图片上的黄色区域):
我的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/palette_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:windowSoftInputMode="adjustPan">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:windowSoftInputMode="adjustPan"
tools:context="com.internet_of_everything.chineesecards.MainActivity">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="0.85"
android:baselineAligned="false"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1">
<ImageButton
android:id="@+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="?android:attr/windowBackground"
android:contentDescription="@android:string/untitled"
android:visibility="visible"
app:srcCompat="@drawable/button_back" />
<ImageButton
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:adjustViewBounds="false"
android:background="?android:attr/windowBackground"
android:contentDescription="@android:string/untitled"
android:elevation="0dp"
android:visibility="visible"
app:srcCompat="@drawable/button_next" />
</LinearLayout>
</LinearLayout>
更新 在我的片段中,我有 2 个线性布局:EditText 和 TextView。同时只能显示 EditText 或 TextView。 我有 onTouchListener:当显示 EditText 时,它会隐藏键盘并在用户单击此 edittext 外部时检查 EditText 的内容。 那么我可以使用这个 onTouchListener 并解决从左到右滑动的问题吗?
public class OneCardFragment extends Fragment {
public OneCardFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
//hides keyboard
imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
//checks edittext content
}
});
}
}
Return false 其他视图也有机会处理该事件。
public class OneCardFragment extends Fragment {
public OneCardFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
//hides keyboard
imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
//checks edittext content
return false;
}
});
}
}