java - Android/Java 如何给 fragment 添加线性布局?

标签 java android android-layout android-4.0-ice-cream-sandwich

我目前正在开发一款应用,但我需要帮助。我怎么能在你有一个列表的地方,当你按下一个项目时,它就会转到它的描述。可以在这个应用程序上找到一个例子 https://play.google.com/store/apps/details?id=com.actionbarsherlock.sample.fragments&feature=more_from_developer#?t=W251bGwsMSwxLDEwMiwiY29tLmFjdGlvbmJhcnNoZXJsb2NrLnNhbXBsZS5mcmFnbWVudHMiXQ ..

当你按下布局时,我怎样才能得到你得到的东西。

我有标签,但无法获取列表。这是主要的 Activity 代码。

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
 * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
 * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
 * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Create the adapter that will return a fragment for each of the three primary sections
    // of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding tab.
    // We can also use ActionBar.Tab#select() to do this if we have a reference to the
    // Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by the adapter.
        // Also specify this Activity object, which implements the TabListener interface, as the
        // listener for when this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}



@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
 * sections of the app.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return getString(R.string.title_section1).toUpperCase();
            case 1: return getString(R.string.title_section2).toUpperCase();
            case 2: return getString(R.string.title_section3).toUpperCase();
            case 3: return getString(R.string.title_section4).toUpperCase();
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    public DummySectionFragment() {
    }

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        Bundle args = getArguments();
        textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
        return textView;
    }
}

最佳答案

我认为您正在寻找开箱即用的解决方案。我最近不得不做同样的事情,但我没有找到开箱即用的解决方案。我做了什么?我创建了一个带有 alpha 动画的不可见 View ,当用户单击我的 ListView 时,我启动动画并显示包含内容的填充 View 。

通过创建自定义对话框并从 xml 文件扩充 View ,还有其他方法可以做到这一点。

更简单的是,如果您不需要与描述 View 进行交互,那么您可以使用具有修改 View 的 Toast 并设置用户阅读的时间。

关于java - Android/Java 如何给 fragment 添加线性布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12452135/

相关文章:

java - 在您事先不知道输出的情况下测试代码

java - 菜单项要放什么?

android - 与 Flutter 提供程序包相关的错误

java - 如何保存 View 的状态并在用户返回时恢复它?

android - 如何修复 - 错误 : '0f' is incompatible with attribute alpha (attr) float. 错误:链接文件资源失败

java - 使用外部 JAR 构建 Eclipse 项目

java - 一周中的时间点

android - 如何在 Android 中禁用微调器并在单击按钮时启用它?

android - 获取 NFC 标签序列号

android - 这是 android 中 CheckBox 的错误吗?