1. 导入Android studio
使用SlidingMenu的方式导入Android studio不行,不知道为何,过会懂了再写上
2. 代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="slidingmenu.com.haha.MainActivity"><com.viewpagerindicator.TabPageIndicatorandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/TabIndicator_id"android:background="@drawable/base_action_bar_bg_nine"></com.viewpagerindicator.TabPageIndicator><android.support.v4.view.ViewPagerandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/viewPager_id"></android.support.v4.view.ViewPager></LinearLayout>
fragment_item.xml
<LinearLayout 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:orientation="vertical"tools:context="slidingmenu.com.haha.ItemFragment"><!-- TODO: Update blank fragment layout --><TextViewandroid:id="@+id/textview"android:layout_width="fill_parent"android:layout_height="fill_parent"android:textSize="18sp"android:gravity="center" /></LinearLayout>
MainActivity.java
package slidingmenu.com.haha;import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast;import com.viewpagerindicator.TabPageIndicator;public class MainActivity extends AppCompatActivity {private String TAG = "MainActivity";private String[] TITTLe = new String[]{"哈哈","呵呵","嘿嘿","嘻嘻"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//1. 拿到viewpagerViewPager viewPager = findViewById(R.id.viewPager_id);//2. 创建一个适配器myPagerAdapter mPagerAdapter = new myPagerAdapter(getSupportFragmentManager());//3. 将适配器和Viewpager绑定 viewPager.setAdapter(mPagerAdapter);//4. 拿到TabPageIndicatorTabPageIndicator tabPageIndicator = findViewById(R.id.TabIndicator_id);//5. 将ViewPager和TabPageIndicator进行绑定 tabPageIndicator.setViewPager(viewPager);//6. 设置页面改变时候的监听函数tabPageIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {Log.d(TAG, "onPageScrolled");}@Overridepublic void onPageSelected(int position) {Log.d(TAG, "onPageSelected");Toast.makeText(getApplicationContext(), TITTLe[position], Toast.LENGTH_SHORT).show();}@Overridepublic void onPageScrollStateChanged(int state) {Log.d(TAG, "onPageScrollStateChanged");}});}//适配器protected class myPagerAdapter extends FragmentPagerAdapter{public myPagerAdapter(FragmentManager fm) {//Log.d(TAG, "myPagerAdapter");super(fm);}@Overridepublic int getCount() {Log.d(TAG, "getCount");return TITTLe.length;}//创建一个新的界面(通过fragment) @Overridepublic Fragment getItem(int position) {Log.d(TAG, "getItem");ItemFragment fragment = new ItemFragment();Bundle args = new Bundle();args.putString("arg", TITTLe[position]);fragment.setArguments(args);return fragment;}//显示抬头的tabpublic CharSequence getPageTitle(int position) {Log.d(TAG, "getPageTitle");return TITTLe[position % TITTLe.length];}} }
ItemFragment.java
package slidingmenu.com.haha;import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;public class ItemFragment extends Fragment {public ItemFragment() {// Required empty public constructor }public static ItemFragment newInstance(String param1, String param2) {ItemFragment fragment = new ItemFragment();return fragment;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View contextView = inflater.inflate(R.layout.fragment_item, container, false);TextView mTextView = (TextView) contextView.findViewById(R.id.textview);//获取Activity传递过来的参数Bundle mBundle = getArguments();String title = mBundle.getString("arg");mTextView.setText(title);return contextView;}}
资料:
导入:http://blog.csdn.net/CDWLX/article/details/52161075
使用:http://blog.csdn.net/xiaanming/article/details/10766053