【问题标题】:How can I make my button to do something in Fragments,ViewPager如何让我的按钮在 Fragments、ViewPager 中执行某些操作
【发布时间】:2012-03-23 11:18:12
【问题描述】:

首先我必须说我英语不好,而且对 Android 编程“完全陌生”。

我想创建一个类似于 Android 主屏幕的应用,它可以左右滑动来查看视图。在我的应用程序上,每个视图都有一个按钮来做某事;现在我已经使用 ViewPager 和 Fragments 完成了滑动部分。当我运行我的应用程序时,它可以顺利地进入左右视图,但我找不到使每个视图中的按钮工作的解决方案。我怎样才能做到这一点?非常感谢您的每一个回答。以下是我的代码(我根据互联网上的示例对其进行了修改)。

这是包含许多片段的主要片段类。

public class LessonsActivity extends FragmentActivity{
/** maintains the pager adapter */
private PagerAdapter mPagerAdapter;

/*
 * (non-Javadoc)
 * 
 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.viewpager_layout);
    // initialsie the pager
    this.initialisePaging();
}

/**
 * Initialise the fragments to be paged
 */
private void initialisePaging() {

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, Lesson_1.class.getName()));
    fragments.add(Fragment.instantiate(this, Lesson_2.class.getName()));
    fragments.add(Fragment.instantiate(this, Lesson_3.class.getName()));
    fragments.add(Fragment.instantiate(this, Lesson_4.class.getName()));
    this.mPagerAdapter = new PagerAdapter(
            super.getSupportFragmentManager(), fragments);
    //
    ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
    pager.setAdapter(this.mPagerAdapter);

}

}

下面这个是我用来显示布局的片段类。

public class Lesson_1 extends Fragment {
/**
 * (non-Javadoc)
 * 
 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
 *      android.view.ViewGroup, android.os.Bundle)
 */
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    if (container == null) {
        // We have different layouts, and in one of them this
        // fragment's containing frame doesn't exist. The fragment
        // may still be created from its saved state, but there is
        // no reason to try to create its view hierarchy because it
        // won't be displayed. Note this is not needed -- we could
        // just run the code below, where we would create and return
        // the view hierarchy; it would just never be used.
        return null;
    }
    return (LinearLayout) inflater.inflate(R.layout.lessons1, container,
            false);

}

}

这是我的 PagerAdapterClass

public class PagerAdapter extends FragmentPagerAdapter {

private List<Fragment> fragments;
/**
 * @param fm
 * @param fragments
 */
public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}
/* (non-Javadoc)
 * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
 */
@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}

/* (non-Javadoc)
 * @see android.support.v4.view.PagerAdapter#getCount()
 */
@Override
public int getCount() {
    return this.fragments.size();
}

}

【问题讨论】:

    标签: android fragment android-viewpager


    【解决方案1】:

    在您的Fragment 中,在onCreateView() 方法中,您将返回特定Fragment 的视图。这意味着你可以操纵它。与其立即返回(膨胀的)视图,不如将其放入变量中并操作该变量,如下所示:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    
        if (container == null) {
            ...
            return null;
        }
    
        LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(R.layout.lessons1,
                        container, false);
    
        // note that we're looking for a button with id="@+id/myButton" in your inflated layout
        // Naturally, this can be any View; it doesn't have to be a button
        Button mButton = (Button) mLinearLayout.findViewById(R.id.myButton);
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // here you set what you want to do when user clicks your button,
                // e.g. launch a new activity
            }
        });
    
        // after you've done all your manipulation, return your layout to be shown
        return mLinearLayout;
    }
    

    【讨论】:

    • 呃...当我创建一个新的 Intent 来启动一个新的活动(其中包含布局)时,当我运行时,它显示为一个对话框,而不是全屏布局。为什么?
    • 不需要View.OnClickListener而不是OnClickListener吗?
    猜你喜欢
    • 2013-06-07
    • 2012-11-15
    • 1970-01-01
    • 2015-09-29
    • 2022-01-16
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多