在视图寻呼机

时间:2016-07-04 05:01:36

标签: android xml 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);
        }
    }
}

片段:

    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;

}

activity_login.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>

fragment_login.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>

的AndroidManifest.xml:

 <activity
    android:name=".activities.LoginActivity"
    android:screenOrientation="portrait"
    />

在AndrodManifest文件中,尝试了

  

android:windowSoftInputMode = stateHidden和android:windowSoftInputMode = adjustPan

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;
    }
}

截图: 在转移到FragmentB之前:

Before moving to FragmentB.

在片段B中 In Fragment B.

从片段B返回片段A:

enter image description here

3 个答案:

答案 0 :(得分:0)

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>

答案 1 :(得分:0)

您可以在片段运行时禁用焦点EditText

EditText et_email_view = (EditText) rootView.findViewById(R.id.et_email);
et_email_view.setFocusable(false);

答案 2 :(得分:0)

例如在我的情况下,viewPager覆盖了editText。我在editText(translationZ)和viewPage(translationZ="2")设置translationZ="1",它帮助了我。

相关问题