Mad*_*han 5 xml android android-fragments android-viewpager
我有一个活动在视图寻呼机内托管两个片段。我使用相同的布局来膨胀这些片段。该布局有两个编辑文本放置在线性布局内,该线性布局位于相对布局内。问题是,当我从片段 A 切换到片段 B 时,他第一个编辑文本在片段 A 中获得焦点,当我从片段 B 返回到片段 A 时,第二个编辑文本获得焦点,而不是第一个编辑文本获得焦点。怎么解决呢。我在下面提供了布局和源代码。我没有在片段类中返回任何代码。
活动:
public class LoginActivity extends BaseActivity {
public static final String selectedTabPosition = "selectedTabPosition";
//Tab tag name
private static String TAB_1_TAG = "Email";
private static String TAB_2_TAG = "Mobile";
private TabLayout mTabLayout;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
TAB_1_TAG = getResources().getString(R.string.tab_email);
TAB_2_TAG = getResources().getString(R.string.tab_mobile);
//Initialise views
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mTabLayout = (TabLayout) findViewById(R.id.tabs);
//set tab with view pager
setupViewPager(mViewPager);
mTabLayout.setupWithViewPager(mViewPager);
setupTabIcons();
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
dismissKeyboard(mViewPager);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
/**
* Adding custom view to tab
*/
private void setupTabIcons() {
LinearLayout tabOne = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.tab_custom, null);
TextView tvIconOne = (TextView) tabOne.findViewById(R.id.tv_tab_title);
tvIconOne.setText(TAB_1_TAG);
mTabLayout.getTabAt(0).setCustomView(tabOne);
setTypeface(tvIconOne, CustomFonts.Prime_regular);
mTabLayout.getTabAt(0).getCustomView().setSelected(true);
LinearLayout tabTwo = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.tab_custom, null);
TextView tvIconTwo = (TextView) tabTwo.findViewById(R.id.tv_tab_title);
tvIconTwo.setText(TAB_2_TAG);
setTypeface(tvIconTwo, CustomFonts.Prime_regular);
mTabLayout.getTabAt(1).setCustomView(tabTwo);
}
/**
* Adding fragments to ViewPager
*
* @param viewPager The view pager
*/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
LoginFragment loginFragmentEmail = new LoginFragment();
Bundle emailBundle = new Bundle();
emailBundle.putInt(selectedTabPosition, 0);
loginFragmentEmail.setArguments(emailBundle);
LoginFragment loginFragmentMobile = new LoginFragment();
Bundle phoneBundle = new Bundle();
phoneBundle.putInt(selectedTabPosition, 1);
loginFragmentMobile.setArguments(phoneBundle);
adapter.addFrag(loginFragmentEmail, TAB_1_TAG);
adapter.addFrag(loginFragmentMobile, TAB_2_TAG);
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Run Code Online (Sandbox Code Playgroud)
分段:
public class LoginFragment extends BaseFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
return rootView;
}
Run Code Online (Sandbox Code Playgroud)
活动登录.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_title"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/custom_tab_layout_height"
android:layout_below="@+id/iv_title"
android:layout_marginTop="@dimen/spacing_10"
app:tabGravity="fill"
app:tabIndicatorColor="@color/app_color_dark"
app:tabMode="fixed"/>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="@dimen/divider_height_small"
android:layout_below="@+id/tabs"
android:background="@color/gray_medium"/>
<com.helper.CustomNonSwipeableViewpager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/view"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
片段_登录.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_50"
android:orientation="vertical">
<EditText
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="@dimen/spacing_48"
/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="@dimen/spacing_48"
android:layout_marginTop="@dimen/spacing_15"/>
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml:
<activity
android:name=".activities.LoginActivity"
android:screenOrientation="portrait"
/>
Run Code Online (Sandbox Code Playgroud)
在AndrodManifest文件中,尝试了
android:windowSoftInputMode = stateHidden 和 android:windowSoftInputMode = adjustmentPan
CustomNonSwipeableViewpager.java:
public class CustomNonSwipeableViewpager extends ViewPager {
public CustomNonSwipeableViewpager(Context context) {
super(context);
}
public CustomNonSwipeableViewpager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
屏幕截图: 在转到 FragmentB 之前:
从片段B返回片段A:
使用requestFocus第一个属性editText,这总是会导致 editText 获得焦点。
在您的文件中进行以下更改fragment_login.xml,
<EditText
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="@dimen/spacing_48">
<requestFocus />
</EditText>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2929 次 |
| 最近记录: |