我想在我的 ViewPager 中设置片段以左右滑动每个片段。我还想将 Fragments 连接到 Container 项目。请帮帮我,我很困惑。如果您需要更多详细信息,请发表评论或开始聊天。
以下是代码的详细信息:
ViewPager 容器实现,允许周围页面可见(尽管不是交互式的)。PagerContainer 拥有一个 ViewPager(在这种情况下居中),将 clipChildren() 设置为 false,并且有一些额外的代码来重绘自身和向前触摸:
这是名为 Hadees 的 PagerActivity:
//package com.example.islamiyat;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* PagerActivity: A Sample Activity for Hadees1
*
* @param
*/
public class Hadees extends Activity {
Hadees1 mContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hadees);
mContainer = (Hadees1) findViewById(R.id.pager_container);
ViewPager pager = mContainer.getViewPager();
PagerAdapter adapter = new MyPagerAdapter();
pager.setAdapter(adapter);
// Necessary or the pager will only have one extra page to show
// make this at least however many pages you can see
pager.setOffscreenPageLimit(adapter.getCount());
// A little space between pages
pager.setPageMargin(15);
// If hardware acceleration is enabled, you should also remove
// clipping on the pager for its children.
pager.setClipChildren(false);
}
// Nothing special about this adapter, just throwing up colored views for
// demo
private class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return 40;
}
@Override
public Object instantiateItem(ViewGroup container, int position){
TextView tv = new TextView(Hadees.this);
tv.setText("Hadees " + position);
tv.setGravity(Gravity.CENTER);
tv.setBackgroundColor(Color.BLUE);
;
((ViewPager) container).addView(tv, 0);
return tv;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}}}
这是名为 Hadees1 的 PagerContainer.java:
import android.content.Context;
import android.graphics.Point;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
public class Hadees1 extends FrameLayout implements ViewPager.OnPageChangeListener {
private ViewPager mpager;
boolean mNeedsRedraw = false;
public Hadees1(Context context) {
super(context);
init();
}
public Hadees1(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Hadees1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
// Disable clipping of children so non-selected pages are visible
setClipChildren(false);
// Child clipping doesn't work with hardware acceleration in Android
// 3.x/4.x
// You need to set this value here if using hardware acceleration in an
// application targeted at these releases.
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
@Override
protected void onFinishInflate() {
try{
mpager = (ViewPager) getChildAt(0);
mpager.setOnPageChangeListener(this);
} catch (Exception e) {
throw new IllegalStateException("The root child of PagerContainer must be a ViewPager");
}
}
public ViewPager getViewPager() {
return mpager;
}
private Point mCenter = new Point();
private Point mInitialTouch = new Point();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCenter.x = w / 2;
mCenter.y = h / 2;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
//We capture any touches not already handled by the ViewPager
// to implement scrolling from a touch outside the pager bounds.
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mInitialTouch.x = (int)ev.getX();
mInitialTouch.y = (int)ev.getY();
default:
ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y);
break;
}
return mpager.dispatchTouchEvent(ev);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//Force the container to redraw on scrolling.
//Without this the outer pages render initially and then stay static
if (mNeedsRedraw) invalidate();
}
@Override
public void onPageSelected(int possition ){
}
@Override
public void onPageScrollStateChanged(int state) {
mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE);
}
}
这是 XML 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.islamiyat.Hadees1
android:id="@+id/pager_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCC" >
<android.support.v4.view.ViewPager
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal" />
</com.example.islamiyat.Hadees1>
</RelativeLayout>