如何使我的按钮在Fragments,ViewPager中执行某些操作

时间:2012-03-23 11:18:13

标签: android fragment android-viewpager

首先,我必须说我不擅长英语和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();
}

}

2 个答案:

答案 0 :(得分:24)

在您的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;
}

答案 1 :(得分:1)

public class EmpresasFragment extends Fragment 
{   
    private LinearLayout lLayoutFrgEmpresas;
    private Button btnNewEmpresa;
    private Button btnMapEmpresas;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {           
        lLayoutFrgEmpresas=(LinearLayout) inflater.inflate(R.layout.fragment_empresas, container, false);
        btnNewEmpresa=(Button) lLayoutFrgEmpresas.findViewById(R.id.frEmp_nuevaEmpresa);
        btnNewEmpresa.setOnClickListener(new View.OnClickListener() 
        {               
            @Override
            public void onClick(View arg0) 
            {
                Log.d(getClass().getCanonicalName(),"Se ha presionado el botton de nueva empresa");
            }
        });
        return lLayoutFrgEmpresas; 
    }
}