我有许多标签和单个TabViewPagerAdapter
,其扩展名为FragmentPagerAdapter
,如下所示,
package com.example.newsline.Adapters;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class TabViewPagerAdapter extends FragmentPagerAdapter {
private boolean swipable;
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public TabViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return mFragmentList.get(i);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
现在,我要禁用左右滑动选项卡,只有在单击该选项卡时才能更改它。以下是我的活动,
package com.example.newsline.Activities;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.newsline.Adapters.NewsCardAdapter;
import com.example.newsline.Adapters.TabViewPagerAdapter;
import com.example.newsline.Fragments.AhmedabadFragment;
import com.example.newsline.Fragments.AjabGajabFragment;
import com.example.newsline.Fragments.BollyWoodFragment;
import com.example.newsline.Fragments.BusinessFragment;
import com.example.newsline.Fragments.DharmDarshanFragment;
import com.example.newsline.Fragments.GadgetsFragment;
import com.example.newsline.Fragments.GujratFragment;
import com.example.newsline.Fragments.InternationalFragment;
import com.example.newsline.Fragments.NRGFragment;
import com.example.newsline.Fragments.NationalFragment;
import com.example.newsline.Fragments.RajkotFragment;
import com.example.newsline.Fragments.RasdharFragment;
import com.example.newsline.Fragments.RecipesFragment;
import com.example.newsline.Fragments.SportsFragment;
import com.example.newsline.Fragments.SuratFragment;
import com.example.newsline.Fragments.TopNewsFragment;
import com.example.newsline.Fragments.VadodaraFragment;
import com.example.newsline.Model.NewsModel;
import com.example.newsline.R;
import com.example.newsline.Utils.URLs;
import com.mindorks.placeholderview.SwipeDecor;
import com.mindorks.placeholderview.SwipePlaceHolderView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class NewsActivity extends AppCompatActivity {
private static final String TAG = "NewsActivity";
Context context;
ProgressDialog progress;
private TabViewPagerAdapter adapter;
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
context = this;
progress = new ProgressDialog(context,
R.style.AppCompatAlertDialogStyle);
progress.setMessage("Loading News...Please Wait! ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.setCancelable(false);
progress.show();
customActionBar();
pager = (ViewPager) findViewById(R.id.viewpager_news_activity);
setUpViewPager(pager);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
tabs.setupWithViewPager(pager);
tabs.setSelectedTabIndicatorColor(Color.WHITE);
}
private void setUpViewPager(ViewPager pager) {
adapter = new TabViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new AjabGajabFragment(), "Ajab-Gajab");
adapter.addFragment(new BollyWoodFragment(), "Bollywood");
adapter.addFragment(new BusinessFragment(), "Business");
adapter.addFragment(new DharmDarshanFragment(), "Dharm Darshan");
adapter.addFragment(new GadgetsFragment(), "Gadgets");
adapter.addFragment(new GujratFragment(), "Gujrat");
adapter.addFragment(new InternationalFragment(), "International");
adapter.addFragment(new NationalFragment(), "National");
adapter.addFragment(new NRGFragment(), "NRG");
adapter.addFragment(new RajkotFragment(), "Rajkot");
adapter.addFragment(new RasdharFragment(), "Rasdhar");
adapter.addFragment(new RecipesFragment(), "Recipes");
adapter.addFragment(new SportsFragment(), "Sports");
adapter.addFragment(new SuratFragment(), "Surat");
adapter.addFragment(new TopNewsFragment(), "Top News");
adapter.addFragment(new VadodaraFragment(), "Vadodara");
pager.setAdapter(adapter);
progress.cancel();
}
public void customActionBar() {
android.support.v7.app.ActionBar bar = getSupportActionBar();
LayoutInflater inflater = LayoutInflater.from(NewsActivity.this);
View view = inflater.inflate(R.layout.action_bar_new_activity, null);
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
bar.setCustomView(view);
ImageView ivCategories = (ImageView)
view.findViewById(R.id.iv_categories);
ivCategories.setVisibility(View.GONE);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
谢谢。