是否可以在viewpager android中只创建一个可滑动的视图
Posted
技术标签:
【中文标题】是否可以在viewpager android中只创建一个可滑动的视图【英文标题】:Is it possible to create only one view swipeable in viewpager android 【发布时间】:2021-05-01 07:29:36 【问题描述】:我在我的应用程序中使用 viewpager,我只想让一个视图可滑动。有没有可能这样做。 当我滑动时,我不希望底部的按钮移动我只想改变文本视图。
任何人都可以帮助我搜索了很多但找不到任何东西。
这是我的代码。 有很多代码我不能在这里全部粘贴。 Here is the link to the rest of it.
ViewPagerAdapter.java
public class ViewPagerAdapter extends PagerAdapter
private Activity activity;
private Cursor cursor;
private int i = 0;
private int f = 0;
OnItemClickListener mOnItemClickListener;
public ViewPagerAdapter(Activity activity, Cursor cursor)
this.activity = activity;
this.cursor = cursor;
public void setOnItemClickListener(OnItemClickListener onItemClickListener)
mOnItemClickListener = onItemClickListener;
@Override
public int getCount()
return cursor.getCount();
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
return view == object;
@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup container, final int position)
final Context context = container.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.quote_detail_viewpager, container, false);
final TextView tvQuotes;
final ImageButton favouriteBtn, shareBtn, copyBtn, bgBtn, fontBtn;
tvQuotes = v.findViewById(R.id.vp_quotes);
favouriteBtn = v.findViewById(R.id.vp_fav);
shareBtn = v.findViewById(R.id.vp_share);
copyBtn = v.findViewById(R.id.vp_copy);
bgBtn = v.findViewById(R.id.vp_bg);
fontBtn = v.findViewById(R.id.vp_font);
int[] fonts =
R.font.architects_daughter, R.font.artifika, R.font.carter_one,
R.font.expletus_sans, R.font.fredoka_one, R.font.graduate, R.font.jose,
R.font.magnolia, R.font.oswald, R.font.quicksand_bold, R.font.righteous,
R.font.salsa, R.font.schoolbell, R.font.sofadi_one
;
cursor.moveToPosition(position);
String id = cursor.getString(0);
String quote = cursor.getString(1);
String author = cursor.getString(2);
String text = quote + "\n \n - "+ author;
if (new DatabaseHelper(context).isFavourite(id))
favouriteBtn.setImageResource(R.drawable.ic_favorite);
tvQuotes.setText(text);
tvQuotes.setTypeface(PrefUtils.getTypefaceFromPrefs(context));
favouriteBtn.setOnClickListener(view ->
UtilsHelper.addOrDeleteFavourite(context, favouriteBtn, id, quote, author));
shareBtn.setOnClickListener(view -> UtilsHelper.shareTextIntent(context, text));
copyBtn.setOnClickListener(view -> UtilsHelper.copyText(context, text));
bgBtn.setOnClickListener(view -> mOnItemClickListener.onItemClick(view));
fontBtn.setOnClickListener(view ->
int j = f++;
if (j < fonts.length)
PrefUtils.setTypefaceFromPrefs(context, fonts[j]);
tvQuotes.setTypeface(PrefUtils.getTypefaceFromPrefs(context));
notifyDataSetChanged();
if (f == fonts.length)
f = 0;
);
container.addView(v);
return v;
@Override
public int getItemPosition(@NonNull Object object)
return POSITION_NONE;
@Override
public void destroyItem(ViewGroup container, int position, @NonNull Object object)
container.removeView((View) object);
public interface OnItemClickListener
void onItemClick(View view);
DetailActivity.java
public class QuoteDetailActivity extends AppCompatActivity
// Max swipe ad interval
private final int MAX_AD_COUNT = 10;
// count swipes
private int SWIPE_COUNT = 0;
// Min swipe ad interval
private int MIN_AD_COUNT = 6;
private int i = 0;
private InterstitialAd mInterstitialAd;
ViewPagerAdapter pagerAdapter;
RelativeLayout rootLayout;
int[] backgrounds;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
Objects.requireNonNull(getSupportActionBar()).hide(); //hide the title bar
setContentView(R.layout.activity_quote_detail);
showInterstitialAd();
// String extras
String incomingActivity = getIntent().getStringExtra(Constants.STRING_EXTRA_INCOMING_ACTIVITY);
int position = getIntent().getIntExtra(Constants.STRING_EXTRA_ADAPTER_POSITION, 0);
DatabaseHelper mDBHelper = new DatabaseHelper(this);
ViewPager viewPager = findViewById(R.id.single_quote_viewpager);
backgrounds = BackgroundUtils.getAllBackgrounds();
rootLayout = findViewById(R.id.root_container);
rootLayout.setBackgroundResource(BackgroundUtils.getBackground(position));
FrameLayout adContainerView = findViewById(R.id.ad_view_container);
adContainerView.post(() ->
AdHelper.loadBanner(this, adContainerView, viewPager));
assert incomingActivity != null;
if (incomingActivity.contains(Constants.ACTIVITY_QUOTES))
pagerAdapter = new ViewPagerAdapter(this, mDBHelper.getAllQuotes());
else if (incomingActivity.contains(Constants.ACTIVITY_FAVOURITE))
pagerAdapter = new ViewPagerAdapter(this, mDBHelper.getFavourites());
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(position); // Setup recycleView Item position
pagerAdapter.setOnItemClickListener(new ViewPagerAdapter.OnItemClickListener()
@Override
public void onItemClick(View view)
changeBackground();
);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
int count = SWIPE_COUNT++;
if (count >= MIN_AD_COUNT)
if (mInterstitialAd != null)
mInterstitialAd.show(QuoteDetailActivity.this);
SWIPE_COUNT = 0;
@Override
public void onPageScrollStateChanged(int state)
);
private void changeBackground()
int j = i++;
if (j < backgrounds.length)
rootLayout.setBackgroundResource(backgrounds[j]);
if (i == backgrounds.length)
i = 0;
private void showInterstitialAd()
MobileAds.initialize(this, initializationStatus ->
);
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, getString(R.string.interstitial_ad_unit),
adRequest, new InterstitialAdLoadCallback()
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd)
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback()
@Override
public void onAdDismissedFullScreenContent()
// Called when fullscreen content is dismissed.
if (MIN_AD_COUNT < MAX_AD_COUNT)
MIN_AD_COUNT++;
@Override
public void onAdShowedFullScreenContent()
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
);
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError)
// Handle the error
mInterstitialAd = null;
);
【问题讨论】:
【参考方案1】:当然,类似于下面的 pseudo_xml
<Parent View> -- background set to image
<ViewPager> -- only this part is swipable
<View Containing Buttons Fixed To The Bottom Of Parent/>
</Parent View>
【讨论】:
但问题是我想用其中一个按钮更改我的 textview 的字体,我可以从父活动中做到这一点 你可以。这将需要一个新问题以上是关于是否可以在viewpager android中只创建一个可滑动的视图的主要内容,如果未能解决你的问题,请参考以下文章
android 内部viewpager添加点击事件之后不能滑动了