我正在尝试编写一个包含3个幻灯片的Viewpager,然后在从顶部拖动到底部之后会出现另一个布局。所以,我正在使用< SlidingDrawer> 来设计布局。但问题是整个布局都被卡住了。 viewpager根本没有移动,SlidingDrawer也无法正常工作。下面我提到了我的代码:
MainActivity.java
package com.example.graphsexample;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
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.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
PagerContainer mContainer;
LinearLayout viewLnr;
ImageView viewImg;
ViewPager pager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContainer = (PagerContainer) findViewById(R.id.pager_container);
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(0);
//If hardware acceleration is enabled, you should also remove
// clipping on the pager for its children.
pager.setClipChildren(false);
pager.setCurrentItem(0);
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Page - "+arg0, Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
//Nothing special about this adapter, just throwing up colored views for demo
private class MyPagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
viewLnr = new LinearLayout(getApplicationContext());
viewLnr.setBackgroundColor(Color.parseColor("#33333333"));
viewLnr.setGravity(Gravity.CENTER);
viewImg = new ImageView(MainActivity.this);
viewImg.setPadding(0, 50, 0, 0);
viewImg.setImageResource(R.drawable.ic_launcher);
viewImg.setBackgroundResource(R.drawable.white_bg);
viewLnr.addView(viewImg);
container.addView(viewLnr);
return viewLnr;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public int getCount() {
return 3;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
}
}
activity_main.xml中
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lt"
android:background="#33333333">
<SlidingDrawer
android:id="@+id/slidingDrawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:content="@+id/content"
android:handle="@+id/pager_container"
android:orientation="vertical">
<com.example.graphsexample.PagerContainer
android:id="@+id/pager_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:background="#141349">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal" />
</com.example.graphsexample.PagerContainer>
<ImageView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:rotation="180"
android:src="@drawable/love" />
</SlidingDrawer>
</FrameLayout >
每当我尝试在SlidingDrawer中声明它时,viewpager都无法正常工作,所以请让我知道一些好的解决方案。谢谢你的帮助!