ActionBar Tab ListFragment di Android
Hi Guys!Dalam tutorial ini kita belajar tentang Aksi Bar Tab di android. Action
bar diperkenalkan dari tingkat API 11.
Dalam posting ini saya akan menjelaskan
cara membuat tab dalam
aksi bar dengan fragmen. Hasil akhir ditampilkan
di bawah ini di mana pengguna
dapat bergerak di antara tab.
Setelah langkah-langkah ini kita harus membuat tab kami dan
menambahkannya ke panel tindakan. Dalam contoh kita membuat tiga tab yang berbeda. Apa yang kita inginkan adalah ketika pengguna menyentuh salah satu tab
perubahan konten UI. Untuk mencapainya kita
perlu dua hal: Fragmen yang mengisi UI ketika
pengguna mengubah tab sesuai dengan tab
yang dipilih Seorang pendengar yang mendapat pemberitahuan bila pengguna berinteraksi dengan tab Jadi kita menggunakan Daftar Fragmen untuk menampilkan daftar konten ketika pengguna menyentuh
pada tab. Jadi melihat video yang sama.Langsunsaja
lihat source code lengkapnya.
main_activity.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
/>
list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
MainActivity.java
package com.olbers.actionbartab;
import com.sunil.actionbartab.R;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the
action bar.
final ActionBar actionBar
= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// For each of the
sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setText("My
Friends").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.title_section2).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.title_section3).setTabListener(this));
}
@Override
public void
onRestoreInstanceState(Bundle savedInstanceState) {
if
(savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
@Override
public void
onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM,
getActionBar().getSelectedNavigationIndex());
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void
onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void
onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
/**
* On
first tab we will show our list
*/
if (tab.getPosition() == 0) {
MyFriendsListFragment simpleListFragment = new
MyFriendsListFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, simpleListFragment).commit();
}
else if (tab.getPosition() == 1) {
AndroidList androidlidt = new AndroidList();
getSupportFragmentManager().beginTransaction().replace(R.id.container,
androidlidt).commit();
}
else {
AndroidVersionList androidversionlist = new
AndroidVersionList();
getSupportFragmentManager().beginTransaction().replace(R.id.container,
androidversionlist).commit();
/*Fragment fragment
= new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() +
1);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction().replace(R.id.container,
fragment).commit();*/
}
}
@Override
public void
onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A dummy fragment
representing a section of the app, but that simply displays dummy text.
*/
public static class DummySectionFragment
extends Fragment {
public
DummySectionFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
TextView
textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args =
getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
}
MyFriendsListFragment.java
package com.olbers.actionbartab;
import com.sunil.actionbartab.R;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MyFriendsListFragment extends ListFragment {
private String myfriends[];
public
MyFriendsListFragment() {
myfriends = new String[] {
"Isaac Da
Costa",
"Jhacko
Franklin",
"Wellem",
"Fardi",
"Android",
"Web",
};
}
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
ListAdapter
listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, myfriends);
setListAdapter(listAdapter);
}
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
return
inflater.inflate(R.layout.list_fragment, container, false);
}
@Override
public void onListItemClick(ListView
list, View v, int position, long id) {
Toast.makeText(getActivity(),
getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
}
AndroidList.java
package com.olbers.actionbartab;
import com.sunil.actionbartab.R;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class AndroidList extends ListFragment{
private String myandroidphone[];
public AndroidList() {
myandroidphone = new String[] {
"Samsung
Gallexy S",
"Sony
Experia",
"Nokia",
"Google",
"4shared",
};
}
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
ListAdapter
listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, myandroidphone);
setListAdapter(listAdapter);
}
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
return
inflater.inflate(R.layout.list_fragment, container, false);
}
@Override
public void
onListItemClick(ListView list, View v, int position, long id) {
Toast.makeText(getActivity(),
getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
}
AndroidVersionList.java
package com.olbers.actionbartab;
import com.sunil.actionbartab.R;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class AndroidVersionList extends ListFragment{
private String myandroidversions[];
public AndroidVersionList()
{
myandroidversions= new String[] {
"Jelly
Bean",
"ADT",
"Hellio",
"Compiler",
"ADB",
};
}
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
ListAdapter
listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, myandroidversions);
setListAdapter(listAdapter);
}
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
return inflater.inflate(R.layout.list_fragment, container, false);
}
@Override
public void
onListItemClick(ListView list, View v, int position, long id) {
Toast.makeText(getActivity(),
getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
}
Run aplikasi
Selamat Mencoba..! Salam Sukses
Posting Komentar
Tinggalkan komentar anda untuk setiap post yang mungkin bagi anda sangat bermanfaat.