使用标签式活动时,我应该在哪里放置代码?

时间:2016-12-19 08:34:32

标签: java android

我在android中使用制表符时遇到了相当烦人的问题。我不确定它们是如何工作的,我遇到的问题是知道在哪里放置代码的逻辑。例如,在第一个选项卡上我想应用一个日历,第二个选项卡上传一个文件,例如我将这个逻辑放在哪里,我有3 xml个片段,到目前为止我一直试图编写逻辑由onCreateView。如果有人能够解释标签活动是如何工作的,我将永远感激我使用默认的android设置进行选项卡式活动,并添加了3个片段。

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {

                if(getArguments().getInt(ARG_SECTION_NUMBER)==1) {

                    View rootView = inflater.inflate(R.layout.fragment_message, container, false);

                    return rootView;

                }
else if(getArguments().getInt(ARG_SECTION_NUMBER)==2){
                    View rootView = inflater.inflate(R.layout.fragment_read, container, false);
                    TextView textView= (TextView)rootView.findViewById(R.id.textView_two);
                    textView.setText("Working");

                    return rootView;
                }
else{
                    View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
                 // would I put my logic here?

                    return rootView;
                }
            }
        }

更新2 所以现在我正在尝试按下按钮并在其中一个片段上显示吐司,按钮没有响应,并且没有出现Toasts。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView=inflater.inflate(R.layout.fragment_calendar, container, false);

    //final ActionBar actionBar = getSupportActionBar();
    //actionBar.setDisplayHomeAsUpEnabled(false);
    //actionBar.setTitle(null);
    //Toast.makeText(getActivity(),"AKHBKH",Toast.LENGTH_LONG).show();
    Button button1=(Button)rootView.findViewById(R.id.button_test1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i("","");
            Toast.makeText(getActivity(),"ddfdf",Toast.LENGTH_LONG).show();
        }
    });

PS 我在Toast上尝试了所有这些     getActivity()     getActivity()。getApplicationContext()     getBaseContext()     的getContext()

2 个答案:

答案 0 :(得分:0)

最初创建一个活动MainActivity。 您必须创建3个片段。从FragmentActivity扩展您的活动并实现ActionBar.TabListener

参考本教程: http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

答案 1 :(得分:0)

您可以使用三个片段,并且您要实现的所有逻辑应分别位于每个片段中。使用MainActivity查看所有片段。在这里,我将分享我的代码。

MainActivity

    public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}


private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new Admin(), "ADMIN");
    adapter.addFragment(new Faculty(), "FACULTY");
    adapter.addFragment(new Student(), "STUDENT");
    viewPager.setAdapter(adapter);
}

//----------------------------------------------------------------------------------------------
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

@Override
public void onBackPressed(){
    //Do Here what ever you want do on back press;

}

}

我将分享任何一个片段的代码:Admin

    public class Admin extends Fragment {
EditText aid;
EditText apassword;
Button submit1,changepass;

String id = null, pass = null;
String table = null;


public Admin() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView=inflater.inflate(R.layout.fragment_admin, container, false);


    aid=(EditText)rootView.findViewById(R.id.admin_id);
    apassword=(EditText)rootView.findViewById(R.id.admin_password);
    changepass=(Button)rootView.findViewById(R.id.changePass);
    submit1=(Button)rootView.findViewById(R.id.submit1);

    submit1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            submit1();
        }
    });

    changepass.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in=new Intent(getActivity(),ChangePassword.class);
            startActivity(in);
        }
    });
    // Inflate the layout for this fragment
    return rootView;

    }


//For Admin login Authentication
public void submit1() {
    id = aid.getText().toString();
    pass = apassword.getText().toString();
    Log.d("id", id);
    Log.d("pass", pass);
    table="admin";
   // new ExecuteTask().execute(id, pass, table);
}}