activity获取viewpager中fragment里的button按钮,并设置监听怎么实现

菜鸟摸鱼 2015-10-20 01:56:58
下面是一个Fragment的xml,fragment是放在activity的viewpager中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" >

<EditText
android:id="@+id/et_one_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/one_nextpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="nextpager"
android:text="下一页" />

</RelativeLayout>

我需要在activity中拿到button并设置监听,,求解
...全文
618 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
菜鸟摸鱼 2015-10-22
  • 打赏
  • 举报
回复
引用 6 楼 congatmoon 的回复:
推荐1L给出的回调函数解决方法。
用回调已解决
菜鸟摸鱼 2015-10-22
  • 打赏
  • 举报
回复
引用 5 楼 u014117701 的回复:

		((Button)getFragmentManager().findFragmentById(/*frgment的id*/).getView().findViewById(R.id.one_nextpager)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				
			}
		});
谢谢,,已经通过回调搞定了
我是纸老虎 2015-10-22
  • 打赏
  • 举报
回复
可以通过回调接口,也可以通过FragmentManager拿到Fragment控件
菜鸟摸鱼 2015-10-21
  • 打赏
  • 举报
回复
引用 3 楼 wzmde007 的回复:
getfragment().变量名,不就获取了吗
能说明白点吗,,小弟菜鸟一个
安迪爸爸 2015-10-21
  • 打赏
  • 举报
回复
getfragment().变量名,不就获取了吗
congatmoon 2015-10-21
  • 打赏
  • 举报
回复
推荐1L给出的回调函数解决方法。
a6141 2015-10-21
  • 打赏
  • 举报
回复

		((Button)getFragmentManager().findFragmentById(/*frgment的id*/).getView().findViewById(R.id.one_nextpager)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				
			}
		});
菜鸟摸鱼 2015-10-20
  • 打赏
  • 举报
回复
引用 1 楼 gfy65402655 的回复:
为毛不在Fragment里面写Button点击事件?如果一定要在Activity里面监听Button点击事件的话,你可以在fragment里面写一个该Button的点击事件回调,不就能满足你的需求?具体怎么写点击事件回调如果不清楚的话就百度,不难。
按键的问题搞定了,,我想在activity获取viewpager中fragment里的数据怎么搞,有多个fragment,像上面edittext输入的数据怎么获取
gfy65402655 2015-10-20
  • 打赏
  • 举报
回复
为毛不在Fragment里面写Button点击事件?如果一定要在Activity里面监听Button点击事件的话,你可以在fragment里面写一个该Button的点击事件回调,不就能满足你的需求?具体怎么写点击事件回调如果不清楚的话就百度,不难。
下面是安卓开发仿微信界面的代码。 分为3步,第一步是界面的编写,第二步是导航界面,第三步是右上角菜单栏。 开始第一步前先预览一下效果。 第一步,界面。 界面的思路是利用ViewPager+Fragment实现,所以activity_main.xml添加一个ViewPager。顶部和底部include的顶部栏和底部栏后面再说。 MainActivity的界面activity_main.xml: <?xml version="1.0" encoding="utf-8"?> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 当然,要用到ViewPager+Fragment就要建立Fragment,如图我建了三个Fragment,这个可以根据需要自己创建。 这三个Fragment很类似,这写出一个,其他以此类推。 package activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.chase.cn.money_of_my.R; /** * Created by Chase on 2017/2/6. */ public class Fragment_tab01 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View tab01 = inflater.inflate(R.layout.fragment_tab01_home,container,false); return tab01; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 此Fragment对应的xml文件: <?xml version="1.0" encoding="utf-8"?> 1 2 3 4 5 6 7 现在回到MainActivity: package activity; import ... public class MainActivity extends FragmentActivity { private ViewPager mViewPager; private MyFragmentPagerAdapter mAdapter; private List fragmentList; //保存界面的view @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StatusBarUtil.setWindowStatusBarColor(this, R.color.colorTitleGray); initViews(); initDatas(); } /** * 数据初始化 */ private void initDatas() { //fragment数据源 fragmentList = new ArrayList(); fragmentList.add(new Fragment_tab01()); fragmentList.add(new Fragment_tab02()); fragmentList.add(new Fragment_tab03()); mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList); mViewPager.setAdapter(mAdapter); } /** * 初始化控件 */ private void initViews() { mViewPager = (ViewPager) findViewById(R.id.vp_mainvp); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 需要编写一个ViewPager的Adapter: package utils; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.List; /** * Created by Chase on 2017/2/6. */ public class MyFragmentPagerAdapter extends FragmentPagerAdapter { private List fragList; private List tabList; public MyFragmentPagerAdapter(FragmentManager fm, List fragList) { super(fm); this.fragList = fragList; } @Override public CharSequence getPageTitle(int position) { return tabList.get(position); } @Override public Fragment getItem(int position) { return fragList.get(position); } @Override public int getCount() { return fragList.size(); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 现在三个Fragment已经添加到了MainActivity,滑动ViewPager切换Fragment,同时底部的导航栏也会切换,在为ViewPager添加监听以前,先说说底部导航栏。 第二步,底部导航。 这个的切换其实就是切换准备好的png图片和改变文字的颜色。 下面是刚才导入的底部导航栏xml文件: <?xml version="1.0" encoding="utf-8"?> <FrameLayout android:id="@+id/fl_page_home" android:layout_width="wrap_content" android:layout_height="57dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> </FrameLayout> <FrameLayout android:id="@+id/fl_page_budget" android:layout_width="wrap_content" android:layout_height="57dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> </FrameLayout> <FrameLayout android:id="@+id/fl_page_more" android:layout_width="wrap_content" android:layout_height="57dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> </FrameLayout> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 继续回到对应的MainActivity:并加入了按两次回退键退出程序。 package activity; import ... public class MainActivity extends FragmentActivity implements View.OnClickListener { private ViewPager mViewPager; private MyFragmentPagerAdapter mAdapter; private List fragmentList; //保存界面的view private FrameLayout fl_page_home, fl_page_budget, fl_page_more; private LinearLayout ll_taball; private Button bt_page_home, bt_page_budget, bt_page_more; private TextView tv_page_home; private TextView tv_page_budget; private TextView tv_page_more; private TextView tv_top_title; //onkeydown_ private static boolean isQuit = false; private Timer timer = new Timer(); //onResult的码 private static final int addActivityRequestCodeOfPage2 = 0,addActivityRequestCodeOfPage1=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StatusBarUtil.setWindowStatusBarColor(this, R.color.colorTitleGray); initViews(); setViewPagerEvent(); initEvents(); initDatas(); } @Override protected void onRestart() { super.onRestart(); } /** * viewPager切换页面的事件 */ private void setViewPagerEvent() { //设置viewpager的page监听换bottom按钮颜色 mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { int currentItem = mViewPager.getCurrentItem(); switch (currentItem) { case 0: resetImgAndTextColorAndButton(); bt_page_home.setBackgroundResource(R.drawable.home_pressed); tv_page_home.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("首页"); bt_add.setVisibility(View.VISIBLE); bt_add.setBackgroundResource(R.drawable.selector_main_top_menu); break; case 1: resetImgAndTextColorAndButton(); bt_page_budget.setBackgroundResource(R.drawable.budget_pressed); tv_page_budget.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("记录"); bt_add.setVisibility(View.VISIBLE); bt_add.setBackgroundResource(R.drawable.selector_add_button); break; case 2: resetImgAndTextColorAndButton(); bt_page_more.setBackgroundResource(R.drawable.more_pressed); tv_page_more.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("更多"); bt_add.setVisibility(View.INVISIBLE); break; default: break; } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } }); } /** * 数据初始化 */ private void initDatas() { //fragment数据源 fragmentList = new ArrayList(); fragmentList.add(new Fragment_tab01()); fragmentList.add(new Fragment_tab02()); fragmentList.add(new Fragment_tab03()); mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList); mViewPager.setAdapter(mAdapter); } /** * 初始化事件 */ private void initEvents() { fl_page_home.setOnClickListener(this); fl_page_budget.setOnClickListener(this); fl_page_more.setOnClickListener(this); bt_add.setOnClickListener(this); } /** * 初始化控件 */ private void initViews() { mViewPager = (ViewPager) findViewById(R.id.vp_mainvp); //底部的布局 fl_page_home = (FrameLayout) findViewById(R.id.fl_page_home); fl_page_budget = (FrameLayout) findViewById(R.id.fl_page_budget); fl_page_more = (FrameLayout) findViewById(R.id.fl_page_more); //底部的按钮 bt_page_home = (Button) findViewById(R.id.bt_page_home); bt_page_budget = (Button) findViewById(R.id.bt_page_budget); bt_page_more = (Button) findViewById(R.id.bt_page_more); //按钮对应文字的颜色 tv_page_home = (TextView) findViewById(R.id.tv_page_home); tv_page_budget = (TextView) findViewById(R.id.tv_page_budget); tv_page_more = (TextView) findViewById(R.id.tv_page_more); //顶部状态栏文字 tv_top_title = (TextView) findViewById(R.id.tv_top_title); ll_taball = (LinearLayout) findViewById(R.id.ll_taball); //记一笔按钮 bt_add = (Button) findViewById(R.id.bt_add); bt_add.setVisibility(View.VISIBLE); } /** * 点击下面的布局按钮事件 * * @param v */ @Override public void onClick(View v) { resetImgAndTextColorAndButton(); switch (v.getId()) { /** * 底部导航栏按钮 */ case R.id.fl_page_home: mViewPager.setCurrentItem(0);//如果首页 切换首页 bt_page_home.setBackgroundResource(R.drawable.home_pressed);//并将按钮颜色点亮 tv_page_home.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("首页"); bt_add.setVisibility(View.VISIBLE); bt_add.setBackgroundResource(R.drawable.selector_main_top_menu); break; case R.id.fl_page_budget: mViewPager.setCurrentItem(1); bt_page_budget.setBackgroundResource(R.drawable.budget_pressed); tv_page_budget.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("记录"); bt_add.setVisibility(View.VISIBLE); bt_add.setBackgroundResource(R.drawable.selector_add_button); break; case R.id.fl_page_more: mViewPager.setCurrentItem(2); bt_page_more.setBackgroundResource(R.drawable.more_pressed); tv_page_more.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("更多"); bt_add.setVisibility(View.INVISIBLE); break; default: break; } } /** * 设置所有图片暗色和文字 */ private void resetImgAndTextColorAndButton() { bt_page_home.setBackgroundResource(R.drawable.home); bt_page_budget.setBackgroundResource(R.drawable.budget); bt_page_more.setBackgroundResource(R.drawable.more); tv_page_home.setTextColor(Color.rgb(56, 56, 56)); tv_page_budget.setTextColor(Color.rgb(56, 56, 56)); tv_page_more.setTextColor(Color.rgb(56, 56, 56)); } /** * 回退按钮两次退出 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (isQuit == false) { isQuit = true; ToastUtil.showToast(getApplicationContext(), "请按两次回退键退出", 3000); TimerTask task = null; task = new TimerTask() { @Override public void run() { isQuit = false; } }; timer.schedule(task, 2000); } else { finish(); System.exit(0); } } return true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == addActivityRequestCodeOfPage2) { mViewPager.setCurrentItem(1); bt_page_budget.setBackgroundResource(R.drawable.budget_pressed); tv_page_budget.setTextColor(Color.rgb(255, 209, 0)); }else if (requestCode==addActivityRequestCodeOfPage1){ bt_page_home.setBackgroundResource(R.drawable.home_pressed); tv_page_home.setTextColor(Color.rgb(255, 209, 0)); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 最后加入的onActivityResult是对应如下情况,如果在某个Fragment对应进去了其他的Activity时,返回以后导航栏是没有之前的显示的,所以如下就要返回原来的显示。 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == addActivityRequestCodeOfPage2) { mViewPager.setCurrentItem(1); bt_page_budget.setBackgroundResource(R.drawable.budget_pressed); tv_page_budget.setTextColor(Color.rgb(255, 209, 0)); }else if (requestCode==addActivityRequestCodeOfPage1){ bt_page_home.setBackgroundResource(R.drawable.home_pressed); tv_page_home.setTextColor(Color.rgb(255, 209, 0)); } } 1 2 3 4 5 6 7 8 9 10 11 12 第三步,顶部右上角菜单。 之前导入顶部栏的xml文件: <?xml version="1.0" encoding="utf-8"?> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 对应菜单我们使用PopupWindow 。 package views; import ... /** * Created by Chase on 2017/2/23. */ public class TopPopWindow extends PopupWindow { private View mView; private LinearLayout ll_popmenu_record,ll_popmenu_book,ll_popmenu_search; public TopPopWindow(Activity paramActivity, View.OnClickListener paramOnClickListener, int paramInt1, int paramInt2){ mView = LayoutInflater.from(paramActivity).inflate(R.layout.popwindow_topright, null); ll_popmenu_record = (LinearLayout) mView.findViewById(R.id.ll_popmenu_record); ll_popmenu_book = (LinearLayout) mView.findViewById(R.id.ll_popmenu_book); ll_popmenu_search = (LinearLayout) mView.findViewById(R.id.ll_popmenu_search); if (paramOnClickListener != null){ //设置点击监听 ll_popmenu_record.setOnClickListener(paramOnClickListener); ll_popmenu_book.setOnClickListener(paramOnClickListener); ll_popmenu_search.setOnClickListener(paramOnClickListener); setContentView(mView); //设置宽度 setWidth(paramInt1); //设置高度 setHeight(paramInt2); //设置显示隐藏动画 setAnimationStyle(R.style.AnimTools); //设置背景透明 setBackgroundDrawable(new ColorDrawable(0)); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 编写PopupWindow 的xml: <?xml version="1.0" encoding="utf-8"?> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 回到MainActivity: package activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.TextView; import com.chase.cn.money_of_my.R; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import utils.MyFragmentPagerAdapter; import utils.StatusBarUtil; import utils.ToastUtil; import views.TopPopWindow; public class MainActivity extends FragmentActivity implements View.OnClickListener { private ViewPager mViewPager; private MyFragmentPagerAdapter mAdapter; private List fragmentList; //保存界面的view private FrameLayout fl_page_home, fl_page_budget, fl_page_more; private LinearLayout ll_taball; private Button bt_page_home, bt_page_budget, bt_page_more; private Button bt_add; private TextView tv_page_home; private TextView tv_page_budget; private TextView tv_page_more; private TextView tv_top_title; //onkeydown_ private static boolean isQuit = false; private Timer timer = new Timer(); //onResult的码 private static final int addActivityRequestCodeOfPage2 = 0,addActivityRequestCodeOfPage1=1; private TopPopWindow topPopWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StatusBarUtil.setWindowStatusBarColor(this, R.color.colorTitleGray); initViews(); setViewPagerEvent(); initEvents(); initDatas(); } @Override protected void onRestart() { super.onRestart(); } /** * viewPager切换页面的事件 */ private void setViewPagerEvent() { //设置viewpager的page监听换bottom按钮颜色 mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { int currentItem = mViewPager.getCurrentItem(); switch (currentItem) { case 0: resetImgAndTextColorAndButton(); bt_page_home.setBackgroundResource(R.drawable.home_pressed); tv_page_home.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("首页"); bt_add.setVisibility(View.VISIBLE); bt_add.setBackgroundResource(R.drawable.selector_main_top_menu); break; case 1: resetImgAndTextColorAndButton(); bt_page_budget.setBackgroundResource(R.drawable.budget_pressed); tv_page_budget.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("记录"); bt_add.setVisibility(View.VISIBLE); bt_add.setBackgroundResource(R.drawable.selector_add_button); break; case 2: resetImgAndTextColorAndButton(); bt_page_more.setBackgroundResource(R.drawable.more_pressed); tv_page_more.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("更多"); bt_add.setVisibility(View.INVISIBLE); break; default: break; } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } }); } /** * 数据初始化 */ private void initDatas() { //fragment数据源 fragmentList = new ArrayList(); fragmentList.add(new Fragment_tab01()); fragmentList.add(new Fragment_tab02()); fragmentList.add(new Fragment_tab03()); mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList); mViewPager.setAdapter(mAdapter); } /** * 初始化事件 */ private void initEvents() { fl_page_home.setOnClickListener(this); fl_page_budget.setOnClickListener(this); fl_page_more.setOnClickListener(this); bt_add.setOnClickListener(this); } /** * 初始化控件 */ private void initViews() { mViewPager = (ViewPager) findViewById(R.id.vp_mainvp); //底部的布局 fl_page_home = (FrameLayout) findViewById(R.id.fl_page_home); fl_page_budget = (FrameLayout) findViewById(R.id.fl_page_budget); fl_page_more = (FrameLayout) findViewById(R.id.fl_page_more); //底部的按钮 bt_page_home = (Button) findViewById(R.id.bt_page_home); bt_page_budget = (Button) findViewById(R.id.bt_page_budget); bt_page_more = (Button) findViewById(R.id.bt_page_more); //按钮对应文字的颜色 tv_page_home = (TextView) findViewById(R.id.tv_page_home); tv_page_budget = (TextView) findViewById(R.id.tv_page_budget); tv_page_more = (TextView) findViewById(R.id.tv_page_more); //顶部状态栏文字 tv_top_title = (TextView) findViewById(R.id.tv_top_title); ll_taball = (LinearLayout) findViewById(R.id.ll_taball); //记一笔按钮 bt_add = (Button) findViewById(R.id.bt_add); bt_add.setVisibility(View.VISIBLE); } /** * 点击下面的布局按钮事件 * * @param v */ @Override public void onClick(View v) { resetImgAndTextColorAndButton(); switch (v.getId()) { /** * 底部导航栏按钮 */ case R.id.fl_page_home: mViewPager.setCurrentItem(0);//如果首页 切换首页 bt_page_home.setBackgroundResource(R.drawable.home_pressed);//并将按钮颜色点亮 tv_page_home.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("首页"); bt_add.setVisibility(View.VISIBLE); bt_add.setBackgroundResource(R.drawable.selector_main_top_menu); break; case R.id.fl_page_budget: mViewPager.setCurrentItem(1); bt_page_budget.setBackgroundResource(R.drawable.budget_pressed); tv_page_budget.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("记录"); bt_add.setVisibility(View.VISIBLE); bt_add.setBackgroundResource(R.drawable.selector_add_button); break; case R.id.fl_page_more: mViewPager.setCurrentItem(2); bt_page_more.setBackgroundResource(R.drawable.more_pressed); tv_page_more.setTextColor(Color.rgb(255, 209, 0)); tv_top_title.setText("更多"); bt_add.setVisibility(View.INVISIBLE); break; /** * 记一笔按钮 */ case R.id.bt_add: if (mViewPager.getCurrentItem() == 1) { Intent intent_add_activity = new Intent(getApplicationContext(), AddRecorderActivity.class); startActivityForResult(intent_add_activity, addActivityRequestCodeOfPage2); } else { bt_page_home.setBackgroundResource(R.drawable.home_pressed);//并将按钮颜色点亮 tv_page_home.setTextColor(Color.rgb(255, 209, 0)); showTopRightPopMenu(); } break; /** * popwindow引入的方法的onclick的listener引入到this * popwindow的点击事件 */ case R.id.ll_popmenu_record: Intent intent_add_activity = new Intent(getApplicationContext(), AddRecorderActivity.class); startActivityForResult(intent_add_activity,addActivityRequestCodeOfPage1); topPopWindow.dismiss(); break; case R.id.ll_popmenu_book: ToastUtil.showSucceccToast(getApplicationContext(), "success12", 3000); break; case R.id.ll_popmenu_search: ToastUtil.showSucceccToast(getApplicationContext(), "success13", 3000); break; default: break; } } /** * 显示右上角popup菜单 */ private void showTopRightPopMenu() { if (topPopWindow == null) { //(activity,onclicklistener,width,height) topPopWindow = new TopPopWindow(MainActivity.this, this, 360, 290); //监听窗口的焦点事件,点击窗口外面则取消显示 topPopWindow.getContentView().setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { topPopWindow.dismiss(); } } }); } //设置默认获取焦点 topPopWindow.setFocusable(true); //以某个控件的x和y的偏移量位置开始显示窗口 topPopWindow.showAsDropDown(bt_add, 0, 0); //如果窗口存在,则更新 topPopWindow.update(); } /** * 设置所有图片暗色和文字 */ private void resetImgAndTextColorAndButton() { bt_page_home.setBackgroundResource(R.drawable.home); bt_page_budget.setBackgroundResource(R.drawable.budget); bt_page_more.setBackgroundResource(R.drawable.more); tv_page_home.setTextColor(Color.rgb(56, 56, 56)); tv_page_budget.setTextColor(Color.rgb(56, 56, 56)); tv_page_more.setTextColor(Color.rgb(56, 56, 56)); } /** * 回退按钮两次退出 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (isQuit == false) { isQuit = true; ToastUtil.showToast(getApplicationContext(), "请按两次回退键退出", 3000); TimerTask task = null; task = new TimerTask() { @Override public void run() { isQuit = false; } }; timer.schedule(task, 2000); } else { finish(); System.exit(0); } } return true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == addActivityRequestCodeOfPage2) { mViewPager.setCurrentItem(1); bt_page_budget.setBackgroundResource(R.drawable.budget_pressed); tv_page_budget.setTextColor(Color.rgb(255, 209, 0)); }else if (requestCode==addActivityRequestCodeOfPage1){ bt_page_home.setBackgroundResource(R.drawable.home_pressed); tv_page_home.setTextColor(Color.rgb(255, 209, 0)); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 右上角的按钮还添加了功能,在不同的Fragment,它的功能不同。 以上就算安卓模仿微信界面的步骤了
仿蚂蜂窝自由行和慕课网视频欢迎页一.资源准备三个比较短小的视频:视频下载二.开始编写代码1.在项目的res下新建一个raw文件夹,放入准备好的这三个视频2.自定义播放视频的CustomVideoView 在这个自定义View面提供一个播放视频的方法。用户只需要传入播放路径就可以了,并且可一循环播放。package cn.bluemobi.dylan.welcomevideopager; import android.content.Context; import android.media.MediaPlayer; import android.net.Uri; import android.util.AttributeSet; import android.view.View; import android.widget.VideoView; /**  * 可以播放视频的View  * Created by yuandl on 2016-11-10.  */ public class CustomVideoView extends VideoView {     public CustomVideoView(Context context) {         super(context);     }     public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {         super(context, attrs, defStyleAttr);     }     public CustomVideoView(Context context, AttributeSet attrs) {         super(context, attrs);     }     @Override     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {         super.onMeasure(widthMeasureSpec, heightMeasureSpec);         setMeasuredDimension(View.MeasureSpec.getSize(widthMeasureSpec), View.MeasureSpec.getSize(heightMeasureSpec));     }     /**      * 播放视频      *      * @param uri 播放地址      */     public void playVideo(Uri uri) {         if (uri == null) {             throw new IllegalArgumentException("Uri can not be null");         }         /**设置播放路径**/         setVideoURI(uri);         /**开始播放**/         start();         setOnPreparedListener(new MediaPlayer.OnPreparedListener() {             @Override             public void onPrepared(MediaPlayer mp) {                 /**设置循环播放**/                 mp.setLooping(true);             }         });         setOnErrorListener(new MediaPlayer.OnErrorListener() {             @Override             public boolean onError(MediaPlayer mp, int what, int extra) {                 return true;             }         });     } }3.建立没个欢迎页面的Fragment去加载自定义视频View的视图package cn.bluemobi.dylan.welcomevideopager; import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /**  * Created by yuandl on 2016-11-10.  */ public class GuildFragment extends Fragment {     private CustomVideoView customVideoView;     @Nullable     @Override     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {         customVideoView = new CustomVideoView(getContext());         /**获取参数,根据不同的参数播放不同的视频**/         int index = getArguments().getInt("index");         Uri uri;         if (index == 1) {             uri = Uri.parse("android.resource://"   getActivity().getPackageName()   "/"   R.raw.guide_1);         } else if (index == 2) {             uri = Uri.parse("android.resource://"   getActivity().getPackageName()   "/"   R.raw.guide_2);         } else {             uri = Uri.parse("android.resource://"   getActivity().getPackageName()   "/"   R.raw.guide_3);         }         /**播放视频**/         customVideoView.playVideo(uri);         return customVideoView;     }     /**      * 记得在销毁的时候让播放的视频终止      */     @Override     public void onDestroy() {         super.onDestroy();         if (customVideoView != null) {             customVideoView.stopPlayback();         }     } }4.界面布局     view.ViewPager         android:id="@ id/vp"         android:layout_width="match_parent"         android:layout_height="match_parent">view.ViewPager>              View             android:id="@ id/iv1"             android:layout_width="10dp"             android:layout_height="10dp"             android:src="@mipmap/dot_focus" />         View             android:id="@ id/iv2"             android:layout_width="10dp"             android:layout_height="10dp"             android:layout_marginLeft="10dp"             android:src="@mipmap/dot_normal" />         View             android:id="@ id/iv3"             android:layout_width="10dp"             android:layout_height="10dp"             android:layout_marginLeft="10dp"             android:src="@mipmap/dot_normal" />          <Button         android:id="@ id/bt_start"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_centerHorizontal="true"         android:layout_marginBottom="50dp"         android:background="@mipmap/bt_start"         android:textColor="@android:color/white"         android:visibility="gone" /> 5.给界面添加Fragmentpackage cn.bluemobi.dylan.welcomevideopager; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity {     private ViewPager vp;     private ImageView iv1;     private ImageView iv2;     private ImageView iv3;     private Button bt_start;     private List<Fragmentfragments;     private void assignViews() {         vp = (ViewPager) findViewById(R.id.vp);         iv1 = (ImageView) findViewById(R.id.iv1);         iv2 = (ImageView) findViewById(R.id.iv2);         iv3 = (ImageView) findViewById(R.id.iv3);         bt_start = (Button) findViewById(R.id.bt_start);     }     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         assignViews();         initData();         initView();     }     /**      * 初始化数据,添加三个Fragment      */     private void initData() {         fragments = new ArrayList<>();         Fragment fragment1 = new GuildFragment();         Bundle bundle1 = new Bundle();         bundle1.putInt("index", 1);         fragment1.setArguments(bundle1);         fragments.add(fragment1);         Fragment fragment2 = new GuildFragment();         Bundle bundle2 = new Bundle();         bundle2.putInt("index", 2);         fragment2.setArguments(bundle2);         fragments.add(fragment2);         Fragment fragment3 = new GuildFragment();         Bundle bundle3 = new Bundle();         bundle3.putInt("index", 3);         fragment3.setArguments(bundle3);         fragments.add(fragment3);     }     /**      * 设置ViewPager的适配器和滑动监听      */     private void initView() {         vp.setOffscreenPageLimit(3);         vp.setAdapter(new MyPageAdapter(getSupportFragmentManager()));         vp.addOnPageChangeListener(new MyPageChangeListener());     }     /**      * ViewPager适配器      */     private class MyPageAdapter extends FragmentPagerAdapter {         public MyPageAdapter(FragmentManager fm) {             super(fm);         }         @Override         public Fragment getItem(int position) {             return fragments.get(position);         }         @Override         public int getCount() {             return fragments.size();         }     }     /**      * ViewPager滑动页面监听器      */     private class MyPageChangeListener implements ViewPager.OnPageChangeListener {         @Override         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {         }         /**          * 根据页面不同动态改变红点和在最后一页显示立即体验按钮          *          * @param position          */         @Override         public void onPageSelected(int position) {             bt_start.setVisibility(View.GONE);             iv1.setImageResource(R.mipmap.dot_normal);             iv2.setImageResource(R.mipmap.dot_normal);             iv3.setImageResource(R.mipmap.dot_normal);             if (position == 0) {                 iv1.setImageResource(R.mipmap.dot_focus);             } else if (position == 1) {                 iv2.setImageResource(R.mipmap.dot_focus);             } else {                 iv3.setImageResource(R.mipmap.dot_focus);                 bt_start.setVisibility(View.VISIBLE);             }         }         @Override         public void onPageScrollStateChanged(int state) {         }     } }
# RapidDevelop-Android快速开发框架 - 框架持续更新 - 这个框架是从平时项目用的比较多的框架整合而来 - 对本项目感兴趣的可以一起研究喜欢的朋友欢迎star - 同时也欢迎大家的宝贵意见issues - 如果大家对MVP模式的开发 网络爬虫以及缓存策略感兴趣的话可以看看我最新写的[Freebook](https://github.com/80945540/FreeBook) - 邮箱:mychinalance@gmail.com - [API地址](http://mylance.top/index.html) - [下载APK](http://fir.im/LCRapidDevelop) ###[English](README_EN.md) -------- ##功能说明 - 异常崩溃统一管理 - retrofit rxjava okhttp rxcache------------------------------网络请求以及网络缓存 - Demo采用MVP模式开发------------------------------------数据逻辑复用,便于维护升级 - 下拉刷新 上拉加载 及自动加载---------------------------实现监听方便快捷 - RecyclerView设配器------------------------------------------再也不需要写ViewHolder - RecyclerView item加载动画--------------------------------多种动画效果一行代码解决 - 页面状态统一管理 加载 无数据 无网络-------------所有页面均可添加 - 图片显示与缓存 GIF图片显示 - Tab+Fragment快速实现 - 视频播放(仿QQ空间,秒拍等List播放) -------- ##效果图展示 ![下拉刷新](image/image1.gif) ![动画](image/image2.gif) ![](image/image3.gif) ![多布局](image/image4.gif) ![视频播放](image/image5.gif) ![状态页面](image/image6.gif)![状态页面](image/image7.gif) -------------- ##使用说明 导入 lcrapiddeveloplibrary 到项目 在 build.gradle 的 dependencies 添加: dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') .... compile project(':lcrapiddeveloplibrary') } ##轻松实现异常统一管理 MyApplication面初始化就可以了 ``` public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); //初始化异常管理工具 Recovery.getInstance() .debug(true)//关闭后 在错误统一管理页面不显示异常数据 .recoverInBackground(false) .recoverStack(true) .mainPage(WelcomeActivity.class)//恢复页面 .init(this); } } ``` ##轻松实现 状态页面 下拉刷新 自动加载 item动画 首先layout.xml面的编写啦 列表页面基本都是这个套路 ``` <!--ProgressActivity用于状态页的控制 比如加载 网络异常 无数据 适合任何页面--> viewtype.ProgressActivity xmlns:progressActivity="http://schemas.android.com/apk/res-auto" android:id="@+id/progress" android:layout_width="match_parent" android:layout_height="match_parent"> <!--SpringView下拉刷新--> View android:id="@+id/springview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" > View android:id="@+id/rv_list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#eeeeee"/> View> viewtype.ProgressActivity> ``` 然后就是Activity面的编写了 这个例子使用MVP模式编写感兴趣的看我最新写的[Freebook](https://github.com/80945540/FreeBook) ``` public class ListvViewActivity extends AppCompatActivity implements BaseQuickAdapter.RequestLoadMoreListener,SpringView.OnFreshListener,SchoolListView { RecyclerView mRecyclerView; ProgressActivity progress; private Toolbar toolbar; private BaseQuickAdapter mQuickAdapter; private int PageIndex=1; private SpringView springView; private SchoolListPresent present; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listv_view); initView(); } private void initView() { present = new SchoolListPresent(this); mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); springView = (SpringView) findViewById(R.id.springview); //设置下拉刷新监听 springView.setListener(this); //设置下拉刷新样式 springView.setHeader(new RotationHeader(this)); //springView.setFooter(new RotationFooter(this));mRecyclerView内部集成的自动加载 上啦加载用不上 在其他View使用 progress = (ProgressActivity) findViewById(R.id.progress); //设置RecyclerView的显示模式 当前List模式 mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); //如果Item高度固定 增加该属性能够提高效率 mRecyclerView.setHasFixedSize(true); //设置页面为加载.. progress.showLoading(); //设置适配器 mQuickAdapter = new ListViewAdapter(R.layout.list_view_item_layout,null); //设置加载动画 mQuickAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN); //设置是否自动加载以及加载个数 mQuickAdapter.openLoadMore(6,true); //将适配器添加到RecyclerView mRecyclerView.setAdapter(mQuickAdapter); //设置自动加载监听 mQuickAdapter.setOnLoadMoreListener(this); //请求网络数据 present.LoadData(PageIndex,12,false); } //自动加载 @Override public void onLoadMoreRequested() { PageIndex++; present.LoadData(PageIndex,12,true); } //下拉刷新 @Override public void onRefresh() { PageIndex=1; present.LoadData(PageIndex,12,false); } /* * MVP模式的相关状态 * * */ @Override public void showProgress() { progress.showLoading(); } @Override public void hideProgress() { progress.showContent(); } @Override public void newDatas(List newsList) { //进入显示的初始数据或者下拉刷新显示的数据 mQuickAdapter.setNewData(newsList);//新增数据 mQuickAdapter.openLoadMore(10,true);//设置是否可以下拉加载 以及加载条数 springView.onFinishFreshAndLoad();//刷新完成 } @Override public void addDatas(List addList) { //新增自动加载的的数据 mQuickAdapter.notifyDataChangedAfterLoadMore(addList, true); } @Override public void showLoadFailMsg() { //设置加载错误页显示 progress.showError(getResources().getDrawable(R.mipmap.monkey_cry), Constant.ERROR_TITLE, Constant.ERROR_CONTEXT, Constant.ERROR_BUTTON, new View.OnClickListener() { @Override public void onClick(View v) { PageIndex=1; present.LoadData(PageIndex,12,false); } }); } @Override public void showLoadCompleteAllData() { //所有数据加载完成后显示 mQuickAdapter.notifyDataChangedAfterLoadMore(false); View view = getLayoutInflater().inflate(R.layout.not_loading, (ViewGroup) mRecyclerView.getParent(), false); mQuickAdapter.addFooterView(view); } @Override public void showNoData() { //设置无数据显示页面 progress.showEmpty(getResources().getDrawable(R.mipmap.monkey_cry),Constant.EMPTY_TITLE,Constant.EMPTY_CONTEXT); } } ``` ##轻松实现视频列表播放 列表部分和上面的一样就不说了,我这边主要描叙视频播放的部分 是在不懂得可以clone到本地仓库跑一边 item_layout.xml ``` View android:id="@+id/video_list_item_image" android:layout_width="100dp" android:layout_height="70dp" android:src="@mipmap/def_head"/> View android:id="@+id/video_list_item_text_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#666666" android:text="标题" android:textSize="15dp"/> View android:id="@+id/video_list_item_text_context" android:layout_width="wrap_content" android:layout_marginTop="5dp" android:textColor="#999999" android:textSize="13dp" android:text="内容" android:lines="3" android:ellipsize="end" android:layout_height="wrap_content"/> ``` 然后就是adapter面对视频控件的赋值处理 ``` public class VideoLisViewAdapter extends BaseQuickAdapter<VideoListDto> { public VideoLisViewAdapter(int layoutResId, List<VideoListDto> data) { super(layoutResId, data); } public VideoLisViewAdapter(List<VideoListDto> data) { super(data); } public VideoLisViewAdapter(View contentView, List<VideoListDto> data) { super(contentView, data); } @Override protected void convert(BaseViewHolder helper, VideoListDto item) { helper.setText(R.id.video_list_item_text_title,item.getTitle()).setText(R.id.video_list_item_text_context,item.getIntroduction()); //Glide加载图片 并且支持gif动图 Glide.with(mContext) .load(item.getPictureUrl()) .crossFade() .placeholder(R.mipmap.def_head) .into((ImageView) helper.getView(R.id.video_list_item_image)); //对视频的赋值 添加视频播放地址(使用原地址 .mp4之类的 这个要注意)和标题 ((JCVideoPlayerStandard)helper.getView(R.id.video_list_item_playr)).setUp(item.getAppVideoUrl(),item.getTitle()); Glide.with(mContext) .load(item.getPictureUrl()) .crossFade() .placeholder(R.mipmap.main_mini_m) .into((((JCVideoPlayerStandard) helper.getView(R.id.video_list_item_playr)).thumbImageView)); } } ``` ###Tab+Fragment快速实现 还是原来的配方 layout.xml ``` <?xml version="1.0" encoding="utf-8"?> <!--显示头部滑块--> <FrameLayout android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" /> view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 然后就是头部的xml编写了 ``` ``` 完全可以按照自己想要的风格玩 下面表格为 可设置的属性 --------- |attr|描述| |:--|:-------| | stl_indicatorAlwaysInCenter | 如果设置为真,有源标签总是显示在心(如报摊google app),默认的错误| | stl_indicatorWithoutPadding | 如果设置为true,画的指标没有填充选项卡,默认的错误| | stl_indicatorInFront | 画前的指示器下划线,默认的错误| | stl_indicatorInterpolation | 行为的指标:“线性”或“智能”| | stl_indicatorGravity | 图的位置指示器:“底”或“前”或“心”,默认“底”| | stl_indicatorColor | 标志的颜色| | stl_indicatorColors | 多种颜色的指标,可以设置每个选项卡的颜色| | stl_indicatorThickness | 厚度指标| | stl_indicatorWidth | 的宽度指标,默认“汽车”| | stl_indicatorCornerRadius | 圆角半径的指标| | stl_overlineColor | 顶线的颜色| | stl_overlineThickness | 顶线的厚度| | stl_underlineColor | 颜色的底线| | stl_underlineThickness | 厚度的底线| | stl_dividerColor| 颜色之间的分隔器选项卡| | stl_dividerColors| 多种颜色的选项卡之间的分隔器,可以设置每个选项卡的颜色| | stl_dividerThickness | 分频器的厚度| | stl_defaultTabBackground | 背景可拉的每个选项卡。 一般设置StateListDrawable| | stl_defaultTabTextAllCaps | 如果设置为真,所有选项卡标题大写,违约事实| | stl_defaultTabTextColor | 文本的颜色包括默认的选项卡| | stl_defaultTabTextSize | 文本包括默认的选项卡的大小| | stl_defaultTabTextHorizontalPadding| 文本布局填充默认的选项卡包括| | stl_defaultTabTextMinWidth| 最小宽度的标签| | stl_customTabTextLayoutId | 布局ID定义自定义选项卡。 如果你不指定一个布局,使用默认选项卡| | stl_customTabTextViewId | 文本视图ID在一个自定义选项卡布局。 如果你不与customTabTextLayoutId定义,不工作| | stl_distributeEvenly | 如果设置为真,每个选项卡都给出同样的重量,默认的错误| | stl_clickable | 如果设置为false,禁用选择选项卡单击,违约事实| | stl_titleOffset | 如果设置为“auto_center”,间的幻灯片的位置选项卡心将继续。 如果指定一个维度将抵消从左边缘,默认24 dp| | stl_drawDecorationAfterTab | 画装饰(指示器和线)绘图选项卡后,默认的错误| -------- 好了接下来就TabActivity ``` public class TabActivity extends AppCompatActivity { ViewGroup tab; ViewPager viewpager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab); initView(); } private void initView() { tab = (ViewGroup) findViewById(R.id.tab); viewpager = (ViewPager) findViewById(R.id.viewpager); //使用方才定义头部 tab.addView(LayoutInflater.from(this).inflate(R.layout.tab_top_layout, tab, false)); SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab); FragmentPagerItems pages = new FragmentPagerItems(this); //添加Fragment FragmentPagerItem.of("头部显示标题", "建立的fragment","需要传值的可以传Bundle") for (int i=0;i<4;i++) { pages.add(FragmentPagerItem.of("Tab"+i, TabFragment.class)); } FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter( getSupportFragmentManager(), pages); viewpager.setAdapter(adapter); viewPagerTab.setViewPager(viewpager); } } ``` -------- ##特别感谢 - [JieCaoVideoPlayer](https://github.com/lipangit/JieCaoVideoPlayer) - [SpringView](https://github.com/liaoinstan/SpringView) - [SmartTabLayout](https://github.com/ogaclejapan/SmartTabLayout) - [BaseRecyclerViewAdapterHelper](https://github.com/CymChad/BaseRecyclerViewAdapterHelper) - [Recovery](https://github.com/80945540/Recovery)

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧