gpt4 book ai didi

带有选项卡和 Viewpager 的 Android fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:40 27 4
gpt4 key购买 nike

enter image description here

我们正在构建一个应用程序,如上所示,它嵌套了 fragment 。

  1. 特色标签 - 详细信息标签和 MAps 标签
  2. “详细信息”选项卡将有一个幻灯片 - 类似于“查看页面 slider ”和下方可滚动的信息。
  3. 将显示 map 的“ map ”选项卡。

enter image description here

我已经实现了选项卡和 map 以及 slider ,如上所示。现在我很困惑如何在 slider 下方添加内容,这将使详细信息选项卡可滚动。

我尝试了什么?

在单击“详细信息”选项卡时,Fragment 将尝试在其中扩充两个 Fragment 布局。

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);
}
}

最佳答案

请注意,从 Android 4.2 或最新的兼容库开始支持 Fragments 嵌套。以前它根本不受支持。至于下面的内容- 把它们都放在一个容器里就可以了

关于带有选项卡和 Viewpager 的 Android fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13625048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com