我需要在选项卡活动中添加一个“滑动视图”。在下面的xml代码中,我创建了六个选项卡,分别是主页、博客、音频、视频、更多和画廊。
这些选项卡已经被成功创建。但我不知道如何为这六个选项卡添加滑动功能。我需要添加从右向左和从左向右的滑动。
layout_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/bottom_bar"
android:layout_weight="0" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/tab_home"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_video"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_audio"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:id="@+id/tab_blog"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:id="@+id/tab_gal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
<FrameLayout
android:id="@+id/tab_more"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</FrameLayout>
<com.sit.gems.util.AppPromoPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
</LinearLayout>
AppPromoPager.java:
这是一个AppPromo分页器。在这里,我使用了ViewPager。
package com.sit.gems.util;
import java.net.URLDecoder;
import java.util.List;
import android.content.Context;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import com.gems.android.R;
import com.sit.gems.app.AppData;
import com.sit.gems.util.ImageCache.ImageCacheParams;
public class AppPromoPager extends RelativeLayout implements
ViewPager.OnPageChangeListener {
//private PromoPageIndicator indicator;
private PromoPagerListener listener;
private Handler trackerHandler = new Handler();
private ImageFetcher mImageFetcher;
public AppPromoPager(Context context) {
super(context);
}
public AppPromoPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AppPromoPager(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void init(FragmentActivity context, List<String> promotions) {
ImageCacheParams cacheParams = new ImageCacheParams(context, AppConstants.IMAGE_CACHE_DIR);
mImageFetcher = new ImageFetcher(context, AppData.getScreenWidth(),(AppData.getScreenWidth()/2));
mImageFetcher.setLoadingImage(R.drawable.ic_launcher);
mImageFetcher.addImageCache(context.getSupportFragmentManager(), cacheParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
ViewPager pager = new ViewPager(context);
ViewPagerAdapter adapter = new ViewPagerAdapter(context, promotions);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
pager.setOnPageChangeListener(this);
addView(pager, params);
//indicator = new PromoPageIndicator(context);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.setMargins(0, 0, 0, 3);
//addView(indicator, params1);
//if (promotions.size() > 1)
// indicator.addCircleImage(promotions.size());
/*pager.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE
&& scrollView != null) {
scrollView.requestDisallowInterceptTouchEvent(true);
}
return false;
}
});*/
}
class ViewPagerAdapter extends PagerAdapter {
Context activity;
List<String> promotions;
public ViewPagerAdapter(Context context, List<String> promotions) {
this.promotions = promotions;
activity = context;
}
public int getCount() {
return promotions.size();
}
public Object instantiateItem(View collection, final int position) {
final ImageView view = new ImageView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
view.setScaleType(ScaleType.CENTER);
String imageUrl = promotions.get(position);
if (imageUrl != null && imageUrl.length() > 0) {
setImage(view, imageUrl);
}
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.selectedPromo(promotions.get(position),position);
}
});
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
//listener.selectedPosition(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
listener.selectedPosition(position);
}
public interface PromoPagerListener {
public void selectedPromo(String offer,int postion);
public void selectedPosition(int postion);
}
public void setPromoPagerListener(PromoPagerListener listener) {
this.listener = listener;
}
private void setImage(final ImageView view, final String imageUrl) {
trackerHandler.post(new Runnable() {
@Override
public void run() {
mImageFetcher.loadImage(URLDecoder.decode(imageUrl),view);
}
});
}
}
我以为通过viewpager的帮助,我可以获得一个滑动视图。
ViewPager被添加到AppPromoPager中。
我将
AppPromoPager
添加到layout_home.xml
文件中。但是到目前为止,我仍然没有获得滑动视图。有人能帮我吗?谢谢。