android使用viewpager計(jì)算偏移量實(shí)現(xiàn)選項(xiàng)卡功能
本文實(shí)例為大家分享了android實(shí)現(xiàn)選項(xiàng)卡功能,通過計(jì)算偏移量,設(shè)置tetxview和imageView的對(duì)應(yīng)值,一些color的值讀者自己去補(bǔ)充
實(shí)現(xiàn)效果圖:

(1)簡(jiǎn)單寫一個(gè)主界面的布局activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="true" android:fitsSystemWindows="true" android:background="@color/bg_color"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/bag_gray" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="36dp" android:background="@android:color/white" android:orientation="horizontal" android:weightSum="3"> <TextView android:id="@+id/tab1_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="商品" android:textColor="@color/title_bag" android:textSize="18sp" /> <TextView android:id="@+id/tab2_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="評(píng)價(jià)" android:textColor="@color/text_color_context" android:textSize="18sp" /> <TextView android:id="@+id/tab3_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="詳情" android:textColor="@color/text_color_context" android:textSize="18sp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/text_color_context" /> <View android:id="@+id/cursor" android:layout_width="50dp" android:layout_height="2dp" android:layout_marginLeft="40dp" android:layout_marginTop="0dip" android:background="@color/title_bag" /> <android.support.v4.view.ViewPager android:id="@+id/thire_vp" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> </LinearLayout>
(2)設(shè)置viewpager的適配器:FragmentAdapter
public class FragmentAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> list;
FragmentManager fm;
public FragmentAdapter(FragmentManager fm, ArrayList<Fragment> list){
super(fm);
this.fm = fm;
this.list = list;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
(3)然后設(shè)置三個(gè)fragment,因?yàn)橛腥齻€(gè)選項(xiàng)卡,所以我們新建三個(gè)fragment,分別是OneFragment、TwoFragment 、ThreeFragment ,布局的話也需要新建三個(gè),跟fragment一一對(duì)應(yīng),因?yàn)椴季诌^于簡(jiǎn)單,這里就不寫了,簡(jiǎn)單寫一點(diǎn)fragment的代碼吧
public class OneFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one,null);
return view;
}
}
(4)在MainActivity中,設(shè)置fragment的適配器,設(shè)置顯示內(nèi)容,并且做viewpager的事件監(jiān)聽
public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener,View.OnClickListener{
private TextView tab1Tv;
private TextView tab2Tv;
private TextView tab3Tv;
private View cursor;
private ViewPager thirdVp;
private ArrayList<Fragment> fragmentlist;
private int offset = 0;
private int screenWidth = 0;
private int screenl_3;
private LinearLayout.LayoutParams lp;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product);
//綁定控件
tab1Tv = (TextView)findViewById(R.id.tab1_tv);
tab2Tv = (TextView)findViewById(R.id.tab2_tv);
tab3Tv = (TextView)findViewById(R.id.tab3_tv);
cursor = (View) findViewById(R.id.cursor);
thirdVp = (ViewPager) findViewById(R.id.thire_vp);
//獲取屏幕寬度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenl_3 = screenWidth/3; //裁剪3分之1
lp = (LinearLayout.LayoutParams)cursor.getLayoutParams();
fragmentlist = new ArrayList<>();
fragmentlist.add(new OneFragment());
fragmentlist.add(new TwoFragment());
fragmentlist.add(new ThreeFragment());
thirdVp.setAdapter(new FragmentAdapter(getSupportFragmentManager(),fragmentlist));
thirdVp.setCurrentItem(0);
thirdVp.setOffscreenPageLimit(2);
thirdVp.setOnPageChangeListener(this);
tab1Tv.setOnClickListener(this);
tab2Tv.setOnClickListener(this);
tab3Tv.setOnClickListener(this);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
offset = (screenl_3-cursor.getLayoutParams().width)/2;
Log.d("TAG", "111----"+position + "--" + positionOffset + "--"
+ positionOffsetPixels);
final float scale = getResources().getDisplayMetrics().density;
if (position == 0){
lp.leftMargin = (int)(positionOffsetPixels/3)+offset;
}else if(position ==1){
lp.leftMargin = (int)(positionOffsetPixels/3)+screenl_3+offset;
}
cursor.setLayoutParams(lp);
upTextcolor(position);
}
private void upTextcolor(int position){
if (position==0){
tab1Tv.setTextColor(getResources().getColor(R.color.title_bag));
tab2Tv.setTextColor(getResources().getColor(R.color.text_color_context));
tab3Tv.setTextColor(getResources().getColor(R.color.text_color_context));
}else if(position==1){
tab1Tv.setTextColor(getResources().getColor(R.color.text_color_context));
tab2Tv.setTextColor(getResources().getColor(R.color.title_bag));
tab3Tv.setTextColor(getResources().getColor(R.color.text_color_context));
}else if(position==2){
tab1Tv.setTextColor(getResources().getColor(R.color.text_color_context));
tab2Tv.setTextColor(getResources().getColor(R.color.text_color_context));
tab3Tv.setTextColor(getResources().getColor(R.color.title_bag));
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tab1_tv:
thirdVp.setCurrentItem(0);
break;
case R.id.tab2_tv:
thirdVp.setCurrentItem(1);
break;
case R.id.tab3_tv:
thirdVp.setCurrentItem(2);
break;
}
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用RollViewPager實(shí)現(xiàn)輪播圖
- Android使用ViewPager實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁效果
- Android使用ViewPager完成app引導(dǎo)頁
- Android ViewPager實(shí)現(xiàn)滑動(dòng)指示條功能
- android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換
- Android自定義引導(dǎo)玩轉(zhuǎn)ViewPager的方法詳解
- Android使用ViewPager快速切換Fragment時(shí)卡頓的優(yōu)化方案
- Android Studio使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)菜單Tab效果
- Android原生ViewPager控件實(shí)現(xiàn)卡片翻動(dòng)效果
- Android用viewPager2實(shí)現(xiàn)UI界面翻頁滾動(dòng)的效果
相關(guān)文章
Android中ViewPager帶來的滑動(dòng)卡頓問題解決要點(diǎn)解析
這里我們主要針對(duì)ViewGroup的SwipeRefreshLayout中引入ViewPager所引起的滑動(dòng)沖突問題進(jìn)行討論,一起來看一下Android中ViewPager帶來的滑動(dòng)卡頓問題解決要點(diǎn)解析:2016-06-06
解決Android MediaRecorder錄制視頻過短問題
本文主要介紹Android MediaRecorder,在使用MediaRecorder時(shí)經(jīng)常會(huì)遇到視頻錄制太短問題,這里提供解決問題的實(shí)例代碼以供大家參考2016-07-07
Android系統(tǒng)進(jìn)程間通信(IPC)機(jī)制Binder中的Server和Client獲得Service Manager接
本文主要介紹Android IPC通信Binder中的Server和Client獲得Service Manager接口,這里詳細(xì)的說明了如何實(shí)現(xiàn)Service Manager接口,對(duì)研究Android源碼的朋友提供幫助,有需要的小伙伴可以參考下2016-08-08
Android中實(shí)現(xiàn)詞組高亮TextView方法示例
高亮顯示大家應(yīng)該都不陌生,在開發(fā)中經(jīng)常會(huì)遇到這個(gè)需求,所以下面這篇文章主要給大家介紹了關(guān)于Android中實(shí)現(xiàn)詞組高亮TextView的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10
Android kotlin+協(xié)程+Room數(shù)據(jù)庫的簡(jiǎn)單使用
這篇文章主要介紹了Android kotlin+協(xié)程+Room數(shù)據(jù)庫的簡(jiǎn)單使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Android實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)自定義控件
大家好,本篇文章主要講的是Android實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)自定義控件,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Android中使用IntentService創(chuàng)建后臺(tái)服務(wù)實(shí)例
這篇文章主要介紹了Android中使用IntentService創(chuàng)建后臺(tái)服務(wù)實(shí)例,IntentService提供了在單個(gè)后臺(tái)線程運(yùn)行操作的簡(jiǎn)單結(jié)構(gòu),需要的朋友可以參考下2014-06-06
Android開發(fā)中的數(shù)據(jù)庫事務(wù)用法分析
這篇文章主要介紹了Android開發(fā)中的數(shù)據(jù)庫事務(wù)用法,分析了Android數(shù)據(jù)庫事務(wù)的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-06-06

