以编程方式阻止页面在视图寻呼机中滑动

时间:2014-10-02 14:54:35

标签: android android-fragments android-viewpager

我正在开发一个应用程序,用户可以在edittexts中输入内容并通过滑动屏幕进入下一个视图。滑动内容由viewpager处理。

我想做的是以下内容:

用户应该只能在填写完所有edittexts时滑动。目前我的所有edittex都有一个TextWatcher,它将布尔值设置为" true"每个领域都填满了。如果是,我想启用viewpager,当它不是时,我想禁用它。

我使用带有三个片段的CustomViewPager。我发布了我的CustomViewPager代码:

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Toast;

public class CustomViewPager extends ViewPager {

private boolean isPagingEnabled;
public Context context;
public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.isPagingEnabled = true;
    this.context = context;
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (isPagingEnabled) {
        return super.onTouchEvent(event);
    }
    Toast.makeText(context, "Please fill in the details, then swipe !",
            Toast.LENGTH_LONG).show();
    return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (isPagingEnabled) {
        return super.onInterceptTouchEvent(event);

    }

    return false;
}

public void setPagingEnabled(boolean b) {
    isPagingEnabled = b;
}
}

PageViewActivity.java

public class PageViewActivity extends FragmentActivity {
    MyPageAdapter pageAdapter;
    int pageId=0;
    public static CustomViewPager pager;
    PageListener pageListener;
    int currentPage=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      List<Fragment> fragments = getFragments();
      pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
      pager =(CustomViewPager)findViewById(R.id.viewpager);



      pager.setAdapter(pageAdapter);
      pager.setPagingEnabled(false); 

      pageListener = new PageListener();

      pager.setOnPageChangeListener(pageListener);

    }

    private List<Fragment> getFragments(){
     List<Fragment> fList = new ArrayList<Fragment>();

     fList.add(MyFragmentA.newInstance("Fragment 1"));
     fList.add(MyFragmentB.newInstance("Fragment 2"));
     fList.add(MyFragmentC.newInstance("Fragment 3"));


     return fList;
    }

    public void next(View v){

        if(pageId<=2)
        {
        pager.setCurrentItem(pageId);
        }
        pageId++;
    }

    private class PageListener extends SimpleOnPageChangeListener{


        public void onPageSelected(int position) {
            Log.i("test", "onPageSelected " + position);
            currentPage = position;


         }

        //Called when the scroll state changes.
        public void onPageScrollStateChanged(int state)
        {

        }

        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
        {
        Log.i("test", "onPageScrolled " + position);
            currentPage = position;

        }
    }

}

我的页面适配器是这样的:

&#13;
&#13;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.Toast;

class MyPageAdapter extends FragmentPagerAdapter {
 private List<Fragment> fragments;

 public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
   super(fm);
   this.fragments = fragments;
 }
 @Override
 public Fragment getItem(int position) {
   return this.fragments.get(position);
 }

 @Override
 public int getCount() {
   return this.fragments.size();
 }
 
}
&#13;
&#13;
&#13;

最后,我使用唯一的相关代码发布我的一个片段:

&#13;
&#13;
public class MyFragmentA extends Fragment {
	
	EditText editname;
	public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
	
	
	public static final MyFragmentA newInstance(String message)
	{
	  MyFragmentA f = new MyFragmentA();
	  Bundle bdl = new Bundle(1);
	  bdl.putString(EXTRA_MESSAGE, message);
	  f.setArguments(bdl);
	  return f;
	}
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
		
		
		editname = (EditText)myFragmentView.findViewById(R.id.name_input);
		
		editname.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                
            	if(editname.getText().toString().trim().length() == 0) {
            		
                    // Can I set here isPagingEnabled = false??
            		Toast.makeText(getActivity(), "SwipeFlag false!", Toast.LENGTH_SHORT).show();
            	
            	} else if(editname.getText().toString().trim().length() > 0) {
            		
                    // Can I set here isPagingEnabled = true??
            		Toast.makeText(getActivity(), "SwipeFlag true!",   Toast.LENGTH_SHORT).show();
            	
            	}
           }
            
        });
		
		return myFragmentView;
	}	
						
}
&#13;
&#13;
&#13;

我知道阻止页面滑动的关键点是设置标志&#34; isPagingEnabled&#34;为假,但我无法理解在运行时从片段设置它的最佳方法是什么。

1 个答案:

答案 0 :(得分:3)

好的,我终于找到了一个解决方案,我在这里发布..阻止页面滑动的最佳方法是控制编辑文本是否填充在PageViewActivity.java中的onPageScrolled(我的MainActivity ..):

&#13;
&#13;
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        
     Log.i("test", "onPageScrolled - position = " + position);
        
     currentPage = position;
     txt1 = (EditText) findViewById(R.id.name_input);
            
     if(position == 0) {
           
         if(txt1.getText().toString().trim().length() == 0) {
            		
                pager.setCurrentItem(0);
            		
         } else if(txt1.getText().toString().trim().length() > 0) {
            	  
                // DO NOTHING
            	
         }
            
    }
}
&#13;
&#13;
&#13;

这样,在填充片段的edittext字段之前,不允许页面滑动。

阻止页面滑动的另一种明智方法,但只有正确的方向是以这种方式修改onPageSelected:

&#13;
&#13;
boolean inibitFlag = false;
int maxAllowedPage = 1; // .. or you can set this value in your fragment..
    	
public void onPageSelected(int position) {
     Log.i("test", "onPageSelected " + position);
            
     if(inibitFlag)
          return;
            
     if(position > maxAllowedPage) {

          inibitFlag = true;
          pager.setCurrentItem(currentPage);
          inibitFlag = false;

     } else {

          currentPage = position;
     }        
}
&#13;
&#13;
&#13;

希望这有助于某人!