1
我有一個應用程序,我正在使用ViewPager在兩個片段之間滑動。我可以完美地在兩個碎片之間滑動;但現在我只需要滑動片段的一半。怎麼做?以下是我已經試過:在使用ViewPager時只滑動一半片段
這是我的MainActivity
public class MainActivity extends FragmentActivity
{
MyPageAdapter pageAdapter;
Context c;
int[] pics = {R.drawable.ic_launcher};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c = MainActivity.this;
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), c, pics);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
}
PageAdapter.java
class MyPageAdapter extends FragmentPagerAdapter {
Context c;
int[] pics;
Fragment fragment;
public MyPageAdapter(android.support.v4.app.FragmentManager fm, Context c, int[] pics) {
super(fm);
this.c =c;
this.pics = pics;
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:return new MyFragment();
case 1: return new NewFragment();
default: return new MyFragment();
}
}
@Override
public int getCount() {
return 2;
}
}
MyFragment.java
public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static final MyFragment newInstance(String message,int a)
{
MyFragment f = new MyFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.textView);
messageTextView.setText("hi");
v.setBackgroundColor(Color.parseColor("#ff0000"));
return v;
}
}
NewFragment.java
public class NewFragment extends Fragment {
public static final MyFragment newInstance(String message,int a)
{
MyFragment f = new MyFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.new_fragment, container, false);
Button b1 = (Button) v.findViewById(R.id.b1);
v.setBackgroundColor(Color.parseColor("#0000ff"));
return v;
}
}
但它會顯示這兩個一半的網頁,我們甚至不能滑動 – user3201291
然後我不明白你正確的問題。 「你需要只滑動一半片段」是什麼意思? – katzoft
我的意思是必須顯示整個第一片段,但是當我滑動時,第一片的一半滑出,第二片片段的一半滑入 – user3201291