我创建了一个customViewPager类来阻止在tabs之间滑动.Swiping已被禁用但是标签内的UI组件没有显示。
这是CustomViewPager.java文件。
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs){
super(context,attrs);
this.enabled = true;
}
public CustomViewPager(Context context){
super(context);
this.enabled = true;
}
@Override
public boolean onTouchEvent(MotionEvent event){
if(this.enabled){
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event){
if(this.enabled){
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled){
this.enabled = enabled;
}
}
这是MainActivity.java文件
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{
private TabLayout tabLayout;
public static int swipe = 0;
private CustomViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adding toolbar to the activity
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing the tablayout
tabLayout = (TabLayout)findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (CustomViewPager)findViewById(R.id.mViewPager);
Pager adapter= new
Pager(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
//Adding onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
}
这是Tab1.java文件
package com.example.nitesh.finaltab;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by Nitesh on 2/6/2017.
*/
public class Tab1 extends Fragment {
int click = 0;
View view;
Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
view = inflater.inflate(R.layout.tab1, container, false);
button = (Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Button is clicked","Tab1");
if(click == 0){
MainActivity.swipe = 1;
click = 1;
}
else if(click == 1){
MainActivity.swipe = 0;
click = 0;
}
}
});
return view;
}
}
这是activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nitesh.finaltab.MainActivity">
<!-- our toolbar -->
<android.support.v7.widget.Toolbar
android:id = "@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight = "?attr/actionBarSize"
/>
<android.support.design.widget.TabLayout
android:id = "@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<!-- View pager to swipe views -->`
<com.example.nitesh.finaltab.CustomViewPager
android:layout_width="0dp"
android:id="@+id/mViewPager"
android:layout_height="0dp">
</com.example.nitesh.finaltab.CustomViewPager>
</LinearLayout>
在选择tab1时看到一个空标签。请指出我正在做的错误。