Answer the question
In order to leave comments, you need to log in
How to assign event handlers in a Widget Tab application?
Hello. I am very young in mobile development. I'm trying to deal with Tabbed Activity. Each tab is divided into a class that is responsible for it and for the xml representation. For example, the QRScaner.java file is responsible for the "Scanner" tab:
package com.savelitomak.qrreaderprototype;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class QRScaner extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.qr_scaner, container, false);
return rootView;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.savelitomak.qrreaderprototype.MainActivity$PlaceholderFragment">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="@string/button_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/button"
android:background="@color/colorPrimary"
android:textColor="@android:color/white"/>
</RelativeLayout>
package com.savelitomak.qrreaderprototype;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
QRScaner qrScaner = new QRScaner();
return qrScaner;
case 1:
RawList rawList = new RawList();
return rawList;
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Scanner";
case 1:
return "Sounds";
}
return null;
}
}
}
Answer the question
In order to leave comments, you need to log in
A simple solution - look for a button inside the created fragment and process the click there.
More confusing, if you need to catch a click in the activity:
- write implement OnClickListener in the activity
- throw it into the fragment as an object of the OnClickListener class
- find the button in the fragment and hang it with the passed listener as a handler
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question