I am new to Android development and am struggling a bit with sliding tab layouts / view pagers.
I would like to replace the content of my viewPager, which currently contains a table, with a simple textview whose value changes based on the tab selected.
I understand this may also entail putting a new fragment into the view, but I am also at a bit of a loss for how to do so. Any help / input would be greatly appreciated!
My class:
public class SecurityActivity extends BaseActivity implements DevicesFragment.OnFragmentInteractionListener {
private static final int NUM_TABS = 4;
DevicesFragmentAdapter fragmentAdapter;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_security);
setTitle(R.string.security_activity_title);
fragmentAdapter = new DevicesFragmentAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(fragmentAdapter);
SlidingTabLayout stl = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
stl.setCustomTabView(R.layout.tab_devices, R.id.tab_title);
stl.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
int color;
switch (position) {
case 0:
color = ContextCompat.getColor(SecurityActivity.this, R.color.blue);
break;
case 1:
color = ContextCompat.getColor(SecurityActivity.this, R.color.green);
break;
case 2:
color = ContextCompat.getColor(SecurityActivity.this, R.color.yellow);
break;
case 3:
color = ContextCompat.getColor(SecurityActivity.this, R.color.orange);
break;
default:
color = ContextCompat.getColor(SecurityActivity.this, R.color.blue);
}
return color;
}
@Override
public int getDividerColor(int position) {
return ContextCompat.getColor(SecurityActivity.this, R.color.blue);
}
});
stl.setViewPager(viewPager);
}
public void onFragmentInteraction(Uri uri) {
}
public class DevicesFragmentAdapter extends FragmentPagerAdapter {
public DevicesFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
String title;
switch (position) {
case 0:
title = getString(R.string.security_level_0);
break;
case 1:
title = getString(R.string.security_level_1);
break;
case 2:
title = getString(R.string.security_level_2);
break;
case 3:
title = getString(R.string.security_level_3);
break;
default:
title = "";
}
return title;
}
@Override
public Fragment getItem(int position) {
ProtectedDevice.Type targetType = ProtectedDevice.Type.fromRawValue(position);
ProtectedDevice[] devices = getDevicesOfType(targetType);
DevicesFragment fragment = DevicesFragment.newInstance(null, null);
DevicesAdapter adapter = new DevicesAdapter(SecurityActivity.this, devices);
fragment.setListAdapter(adapter);
return fragment;
}
@Override
public int getCount() {
return NUM_TABS;
}
private ProtectedDevice[] getDevicesOfType(ProtectedDevice.Type type) {
ProtectedDevice[] allDevices = SessionManager.getInstance().getDeviceInfo().getProtectedDevices();
if (type == ProtectedDevice.Type.None) {
return allDevices;
}
ArrayList<ProtectedDevice> result = new ArrayList<>();
for (ProtectedDevice d: allDevices) {
if (d.getType() == type) {
result.add(d);
}
}
ProtectedDevice[] arr = new ProtectedDevice[result.size()];
return result.toArray(arr);
}
}
}
My layout:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.xxx.xxx.activity.DevicesActivity"
android:background="@drawable/bg_gradient">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_lock" />
<View
android:id="@+id/horizontalRule"
android:layout_width="0dp"
android:layout_height="2dp"
android:background="@color/green"
android:layout_below="@id/imageView"
android:layout_alignStart="@id/imageView"
android:layout_alignEnd="@id/imageView"
android:layout_marginTop="5dp" />
<com.xxx.xxx.view.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/horizontalRule"
android:layout_marginTop="10dp" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_alignParentEnd="true"
android:layout_below="@+id/sliding_tabs"
/>
</RelativeLayout>
Aucun commentaire:
Enregistrer un commentaire