Android的碎片有标签和Viewpager [英] Android Fragments with Tabs and Viewpager
问题描述
我们正在建立一个应用程序如上图已嵌套片段。
- 标签拥有 - 详细信息选项卡和地图标签
- 详细信息选项卡将有一个幻灯片 - 就像查看页面滑块和信息低于这将是滚动
- 地图选项卡,将显示该地图。
我已implmented的选项卡和如上面所看到的映射以及所述滑块。现在我很困惑,我怎么可以添加滑块下方的内容,这将使详细信息选项卡滚动。
我曾尝试?
在单击详细信息选项卡中的片段会尽量抬高它里面的两个碎片布局。
AndroidTabLayoutActivity.java
包com.mink7.abs;
进口com.viewpagerindicator.CirclePageIndicator;
进口android.app.TabActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v4.view.ViewPager;
进口android.widget.TabHost;
进口android.widget.TabHost.TabSpec;
导入了java.util.Random;
进口android.support.v4.app.FragmentTabHost;
进口com.viewpagerindicator.PageIndicator;
进口android.support.v4.app.FragmentActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.Toast;
公共类AndroidTabLayoutActivity扩展TabActivity {
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
// FragmentTabHost tabHost;
的setContentView(R.layout.main);
// tabHost =(FragmentTabHost)findViewById(R.id.tabMode);
TabHost tabHost = getTabHost();
/ *
* mAdapter =新TestFragmentAdapter(getSupportFragmentManager());
*
* mPager =(ViewPager)findViewById(R.id.pager);
* mPager.setAdapter(mAdapter);
*
* mIndicator =(CirclePageIndicator)findViewById(R.id.indicator);
* mIndicator.setViewPager(mPager);
* /
//标签的照片
则tabspec photospec = tabHost.newTabSpec(细则);
photospec.setIndicator(详细信息,
。getResources()getDrawable(R.drawable.icon_photos_tab));
意图photosIntent =新的意图(这一点,DetailsActivity.class);
photospec.setContent(photosIntent);
//标签的歌曲
则tabspec songspec = tabHost.newTabSpec(地图);
//设置标题和图标标签
songspec.setIndicator(地图,
。getResources()getDrawable(R.drawable.icon_songs_tab));
意图songsIntent =新的意图(这一点,MapsActivity.class);
songspec.setContent(songsIntent);
//标签的影片
/ *
*则tabspec videospec = tabHost.newTabSpec(视频);
* videospec.setIndicator(视频,
* getResources()getDrawable(R.drawable.icon_videos_tab))。意图
* videosIntent =新的意图(这一点,VideosActivity.class);
* videospec.setContent(videosIntent);
* /
//将所有则tabspec到TabHost
tabHost.addTab(photospec); //将照片添加标签
tabHost.addTab(songspec); //将歌曲添加标签
// tabHost.addTab(videospec); //将视频添加标签
}
}
DetailsActivity.java
包com.mink7.abs;
进口android.os.Bundle;
进口android.support.v4.view.ViewPager;
进口com.viewpagerindicator.CirclePageIndicator;
公共类DetailsActivity扩展BaseSampleActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.place_details_layout);
mAdapter =新TestFragmentAdapter(getSupportFragmentManager());
mPager =(ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator =(CirclePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
}
BaseSampleActivity.java
包com.mink7.abs;
导入了java.util.Random;
进口com.viewpagerindicator.PageIndicator;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.view.ViewPager;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.Toast;
公共抽象类BaseSampleActivity扩展FragmentActivity {
私有静态最后随机RANDOM =新的随机();
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
// FragmentTabHost mTabHost;
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.random:
最终诠释页= RANDOM.nextInt(mAdapter.getCount());
Toast.makeText(这一点,更改到页面+页,Toast.LENGTH_SHORT);
mPager.setCurrentItem(页);
返回true;
案例R.id.add_page:
如果(mAdapter.getCount()小于10){
mAdapter.setCount(mAdapter.getCount()+ 1);
mIndicator.notifyDataSetChanged();
}
返回true;
案例R.id.remove_page:
如果(mAdapter.getCount()→1){
mAdapter.setCount(mAdapter.getCount() - 1);
mIndicator.notifyDataSetChanged();
}
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
注意碎片由于Android 4.2或最新的兼容库支持嵌套。 previously它根本不支持。至于下面的内容 - 只是把他们这一切在一个多个容器
We are building an App as shown above that has nesting of Fragments.
- Tabs Featuring - Details Tab and MAps Tab
- Details Tab will have a slideshow - like the View Page Slider and information below that which will be scrollable.
- Maps Tab which will display the maps.
I have implmented the tabs and maps as well as the Slider as seen above. Now i am confused how i can add content below the Slider which will make the Details Tab scrollable.
What i have tried ?
On CLicking the Details tab the Fragment will try to inflate two Fragment layouts inside it.
AndroidTabLayoutActivity.java
package com.mink7.abs;
import com.viewpagerindicator.CirclePageIndicator;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import java.util.Random;
import android.support.v4.app.FragmentTabHost;
import com.viewpagerindicator.PageIndicator;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// FragmentTabHost tabHost;
setContentView(R.layout.main);
// tabHost = (FragmentTabHost) findViewById(R.id.tabMode);
TabHost tabHost = getTabHost();
/*
* mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
*
* mPager = (ViewPager) findViewById(R.id.pager);
* mPager.setAdapter(mAdapter);
*
* mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
* mIndicator.setViewPager(mPager);
*/
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Details");
photospec.setIndicator("Details",
getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, DetailsActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Maps");
// setting Title and Icon for the Tab
songspec.setIndicator("Maps",
getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, MapsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
/*
* TabSpec videospec = tabHost.newTabSpec("Videos");
* videospec.setIndicator("Videos",
* getResources().getDrawable(R.drawable.icon_videos_tab)); Intent
* videosIntent = new Intent(this, VideosActivity.class);
* videospec.setContent(videosIntent);
*/
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
// tabHost.addTab(videospec); // Adding videos tab
}
}
DetailsActivity.java
package com.mink7.abs;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import com.viewpagerindicator.CirclePageIndicator;
public class DetailsActivity extends BaseSampleActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.place_details_layout);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
}
BaseSampleActivity.java
package com.mink7.abs;
import java.util.Random;
import com.viewpagerindicator.PageIndicator;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public abstract class BaseSampleActivity extends FragmentActivity {
private static final Random RANDOM = new Random();
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
//FragmentTabHost mTabHost;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.random:
final int page = RANDOM.nextInt(mAdapter.getCount());
Toast.makeText(this, "Changing to page " + page, Toast.LENGTH_SHORT);
mPager.setCurrentItem(page);
return true;
case R.id.add_page:
if (mAdapter.getCount() < 10) {
mAdapter.setCount(mAdapter.getCount() + 1);
mIndicator.notifyDataSetChanged();
}
return true;
case R.id.remove_page:
if (mAdapter.getCount() > 1) {
mAdapter.setCount(mAdapter.getCount() - 1);
mIndicator.notifyDataSetChanged();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
Be aware Fragments nesting is supported since Android 4.2 or latest compatibility libraries. Previously it was simply not supported. As for content below - just put them it all in one more container
这篇关于Android的碎片有标签和Viewpager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!