RadioButton+ViewPager+FragmentPagerAdapter快

系统 1477 0
一切为了快速开发

开发类似界面
RadioButton+ViewPager+FragmentPagerAdapter快速搭建页面结构

    
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;

import com.yirui.youbao.App;
import com.yirui.youbao.fragment.ParkingPayedListFragment;
import com.yirui.youbao.fragment.ParkingUnpayListFragment;

/**
 * 
 * @author pythoner
 *
 */
public class ParkingPayActivity extends BaseActivity implements View.OnClickListener{

	private final int count = 2;//页数
	private int curPosition=0;//当前页
	private RadioButton[] rbs;
	private ViewPager viewPager;
	private FragmentPagerAdapter pagerAdapter;
	private Fragment[] fragments;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_parking_pay);
		initActionBar("App付费");
		initViews();
	}
	
	private void initViews() {
		initIndicator();
		initViewPager();
	}

	private void initIndicator(){
		rbs = new RadioButton[count];
		rbs[0] = (RadioButton) findViewById(R.id.rb_0);
		rbs[1] = (RadioButton) findViewById(R.id.rb_1);

		for (int i = 0; i < rbs.length; i++) {
			rbs[i].setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					String tag = (String) v.getTag();
					curPosition=Integer.parseInt(tag);
					viewPager.setCurrentItem(curPosition);
				}
			});
		}
		
		rbs[curPosition].setChecked(true);
	}
	
	private void initViewPager(){
		fragments = new Fragment[count];
		fragments[0] = ParkingUnpayListFragment.newInstance();
		fragments[1] = ParkingPayedListFragment.newInstance();

		pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public int getCount() {
				return fragments.length;
			}

			@Override
			public Fragment getItem(int position) {
				return fragments[position];
			}

		};

		viewPager = (ViewPager) findViewById(R.id.viewPager);
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				rbs[position].setChecked(true);
				curPosition=position;
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

		viewPager.setAdapter(pagerAdapter);
		viewPager.setCurrentItem(curPosition);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}
	
}

  


    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_gray"
    android:orientation="vertical" >

    <include layout="@layout/actionbar_comm" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:padding="8dp" >

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rb_0"
                android:tag="0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bg_blue_trans_with_frame"
                android:button="@null"
                android:paddingBottom="4dp"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:paddingTop="4dp"
                android:text="未付费"
                android:textColor="@color/textcolor_blue_white_radio" />

            <RadioButton
                android:id="@+id/rb_1"
                android:tag="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bg_blue_trans_with_frame"
                android:button="@null"
                android:paddingBottom="4dp"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:paddingTop="4dp"
                android:text="已付费"
                android:textColor="@color/textcolor_blue_white_radio" />
        </RadioGroup>
    </FrameLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:flipInterval="30"
        android:background="@android:color/white"
        android:layout_marginTop="16dp"
        android:persistentDrawingCache="animation" />

</LinearLayout>

  


背景:
    
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true">
        <shape>
            <solid android:color="@color/primary" />
            <stroke android:width="1dp" android:color="@color/primary" />
            <corners android:topLeftRadius="dp" android:topRightRadius="0dp" android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp"/>
        </shape>
   	</item>
    <item>
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke android:width="1dp" android:color="@color/primary" />
            <corners android:topLeftRadius="dp" android:topRightRadius="0dp" android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp"/>
        </shape>
    </item>

</selector>

  


文字颜色:
    
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@android:color/white" />
    <item android:color="@color/primary" />
</selector>

  

RadioButton+ViewPager+FragmentPagerAdapter快速搭建页面结构


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论