首先,让我说在编程方面我是一个极端的新手。我将教程中的代码复制并粘贴到我的 android 项目中,该项目包含 4 个可以水平滑动的片段。我尝试添加第 5 个片段(Fragment5.java & fragment_5.xml)。我没有收到任何错误,但是当我运行项目时,第 5 个片段根本没有出现!
注意:我在这个项目中使用了 JakeWharton Android-ViewPagerIndicator 库。
pg6.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res/com.project.viewswipingtest">
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="#000000"
android:textColor="#99C224"
app:footerColor="#99C224"
app:footerLineHeight="1dp"
app:footerIndicatorStyle="underline"
app:footerIndicatorHeight="3dp"
app:selectedColor="#99C224"
app:selectedBold="true"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
片段_5.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:gravity="center"
android:text="Fragment 5"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
片段5.java:
package com.project.viewswipingtest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment5 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_5, null);
}
}
Page6.java:
package com.project.viewswipingtest;
import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class Page6 extends FragmentActivity {
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pg6);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
}
测试片段.java:
package com.project.viewswipingtest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TestFragment extends Fragment{
private static final String KEY_CONTENT = "TestFragment:Content";
public static TestFragment newInstance(String content){
TestFragment fragment = new TestFragment();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 20; i++){
builder.append(content).append(" ");
}
builder.deleteCharAt(builder.length() -1);
fragment.mContent = builder.toString();
return fragment;
}
private String mContent = "???";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)){
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setText(mContent);
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(text);
return layout;
}
public void onSavedInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}
TestFragmentAdapter.java:
package com.project.viewswipingtest;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.viewpagerindicator.IconPagerAdapter;
public class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter{
protected static final String[] CONTENT = new String[] {
"This", "Is", "A", "Test"
};
private int mCount = CONTENT.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getIconResId(int index) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment1();
switch(position){
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
case 4:
fragment = new Fragment5();
break;
}
return fragment;
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position){
String title = "";
switch(position){
case 0:
title = "Intro";
break;
case 1:
title = "Step 1";
break;
case 2:
title = "Step 2";
break;
case 3:
title = "Step 3";
break;
case 4:
title = "Step 4";
break;
}
return title;
}
public void setCount(int count){
if (count > 0 && count < 10){
mCount = count;
notifyDataSetChanged();
}
}
}