ActionBar实现下拉式导航
扫描二维码
随时随地手机看文章
利用Actionbar同样可以很轻松的实现下拉式的导航方式,相比与tab的方式下拉式的导航方式更类型于html中的
01 |
package ccom.app.main; |
02 |
03 |
import android.annotation.SuppressLint; |
04 |
import android.app.Fragment; |
05 |
import android.content.Context; |
06 |
import android.os.Bundle; |
07 |
import android.view.LayoutInflater; |
08 |
import android.view.View; |
09 |
import android.view.ViewGroup; |
10 |
import android.view.ViewGroup.LayoutParams; |
11 |
import android.widget.TextView; |
12 |
13 |
@SuppressLint("NewApi") |
14 |
public class MyFragment extends Fragment { |
15 |
16 |
@Override |
17 |
public View onCreateView(LayoutInflater inflater, ViewGroup container, |
18 |
Bundle savedInstanceState) { |
19 |
20 |
Context context = this.getActivity(); |
21 |
22 |
TextView tv = new TextView(context); |
23 |
24 |
Bundle arc = this.getArguments(); |
25 |
26 |
int tabs=arc.getInt("key"); |
27 |
|
28 |
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, |
29 |
LayoutParams.WRAP_CONTENT)); |
30 |
31 |
tv.setText("hello actionbar "+tabs); |
32 |
33 |
return tv; |
34 |
35 |
} |
36 |
37 |
} |
main.xml:
01 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
02 |
xmlns:tools="http://schemas.android.com/tools" |
03 |
android:layout_width="match_parent" |
04 |
android:layout_height="match_parent" |
05 |
tools:context=".Main" > |
06 |
07 |
<LinearLayout |
08 |
android:id="@+id/content" |
09 |
android:layout_width="match_parent" |
10 |
android:layout_height="match_parent" |
11 |
android:orientation="vertical" /> |
12 |
13 |
</RelativeLayout> |
自定义的用于显示textview的mytextview.xml:
01 |
<?xml version="1.0" encoding="utf-8"?> |
02 |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" |
03 |
android:id="@+id/text1" |
04 |
android:textColor="#fff" |
05 |
android:background="#696969" |
06 |
android:layout_width="60sp" |
07 |
android:layout_height="match_parent" |
08 |
android:textAppearance="?android:attr/textAppearanceListItemSmall" |
09 |
android:gravity="center_vertical" |
10 |
android:paddingStart="?android:attr/listPreferredItemPaddingStart" |
11 |
android:minHeight="?android:attr/listPreferredItemHeightSmall" |
12 |
/> |
Main.java
01 |
package ccom.app.main; |
02 |
03 |
import android.annotation.SuppressLint; |
04 |
import android.app.ActionBar; |
05 |
import android.app.Activity; |
06 |
import android.app.FragmentTransaction; |
07 |
import android.os.Bundle; |
08 |
import android.widget.ArrayAdapter; |
09 |
10 |
@SuppressLint("NewApi") |
11 |
public class Main extends Activity implements ActionBar.OnNavigationListener { |
12 |
13 |
ActionBar actionBar = null; |
14 |
15 |
@Override |
16 |
protected void onCreate(Bundle savedInstanceState) { |
17 |
super.onCreate(savedInstanceState); |
18 |
setContentView(R.layout.main); |
19 |
20 |
actionBar = this.getActionBar(); |
21 |
22 |
actionBar.setDisplayShowTitleEnabled(true); |
23 |
24 |
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); |
25 |
26 |
actionBar.setListNavigationCallbacks(new ArrayAdapter(Main.this, |
27 |
R.layout.mytextview, R.id.text1, new String[] { "tab1", "tab2", |
28 |
"tab3" }), this); |
29 |
30 |
} |
31 |
32 |
@Override |
33 |
public boolean onNavigationItemSelected(int itemPosition, long itemId) { |
34 |
35 |
MyFragment mf = new MyFragment(); |
36 |
Bundle bundle = new Bundle(); |
37 |
bundle.putInt("key", itemPosition + 1); |
38 |
mf.setArguments(bundle); |
39 |
40 |
FragmentTransaction action = this.getFragmentManager() |
41 |
.beginTransaction(); |
42 |
action.replace(R.id.content, mf); |
43 |
action.commit(); |
44 |
return true; |
45 |
} |
46 |
47 |
} |
实现的效果如图:





