- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么我的 ViewPager OnTouchListener
根本不触发!!
下面是我的代码:
我正在使用 ViewPager
自动滑动照片,但是当我点击 ViewPager
时,我需要停止自动滑动,但问题是我的 OnTouchListener
根本不触发。
Java
man_fragment.java
package com.techzone.yallaassouk;
/**
* Created by Yazan on 4/26/2016.
*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import com.techzone.yallaassouk.Adapters.Brands_View_Adapter;
import com.techzone.yallaassouk.Adapters.Categories_View_Adapter;
import com.techzone.yallaassouk.Adapters.ImageSlideAdapter;
import com.techzone.yallaassouk.DataItems.Band;
import com.techzone.yallaassouk.DataItems.Brand;
import com.techzone.yallaassouk.DataItems.Category;
import com.techzone.yallaassouk.Utils.CirclePageIndicator;
import com.techzone.yallaassouk.Utils.PageIndicator;
import com.techzone.yallaassouk.Utils.CheckNetworkConnection;
import com.techzone.yallaassouk.json.GetJSONObject;
import com.techzone.yallaassouk.json.JsonReader;
import org.json.JSONObject;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.List;
public class man_fragment extends Fragment {
public static final String ARG_ITEM_ID = "man_fragment";
private static final long ANIM_VIEWPAGER_DELAY = 5000;
private static final long ANIM_VIEWPAGER_DELAY_USER_VIEW = 10000;
private static final int TabNo = 1;
// UI References
ViewPager BandsViewPager;
PageIndicator mIndicator;
RecyclerView brands_Rec_View;
RecyclerView categories_Rec_View;
//lists
List<Band> bands;
List<Brand> brands;
List<Category> categories;
//tasks
RequestBandsTask bands_task;
RequestBrandsTask brands_task;
RequestCategoriesTask categories_task;
AlertDialog alertDialog;
boolean stopSliding = false;
String message;
//lists adapters
Brands_View_Adapter brands_adapter;
Categories_View_Adapter categories_adapter;
//runnables and handlers
private Runnable animateViewPager;
private Handler handler;
//urls
String brandsurl = "http://yazanallahham-001-site1.ftempurl.com/Brands.svc/json/brands/"+TabNo;
String categoriesurl = "http://yazanallahham-001-site1.ftempurl.com/categories.svc/json/categories/"+TabNo+"/0";
String bandsurl = "http://yazanallahham-001-site1.ftempurl.com/bands.svc/json/bands/0";
//activity
FragmentActivity activity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.man_fragment, container, false);
findViewById(rootView);
mIndicator.setOnPageChangeListener(new PageChangeListener());
BandsViewPager.setOnPageChangeListener(new PageChangeListener());
BandsViewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
stopSliding = true;
break;
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_UP:
// calls when touch release on ViewPager
if (bands != null && bands.size() != 0) {
stopSliding = false;
runnable(bands.size());
handler.postDelayed(animateViewPager,
ANIM_VIEWPAGER_DELAY_USER_VIEW);
}
break;
case MotionEvent.ACTION_MOVE:
// calls when ViewPager touch
if (handler != null && stopSliding == false) {
stopSliding = true;
handler.removeCallbacks(animateViewPager);
}
break;
}
return false;
}
});
// sendRequest();
//init brands recyclerview
brands_Rec_View.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));
brands_Rec_View.addOnItemTouchListener(
new RecyclerItemClickListener(this.getContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
categories_fragment nextFrag= new categories_fragment();
Bundle args = new Bundle();
Brand clickedBrand = (Brand)brands.get(position);
args.putInt("BrandId", clickedBrand.getId());
args.putInt("TabId", TabNo);
args.putString("BrandName", clickedBrand.getEnName());
nextFrag.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.main_content, nextFrag, categories_fragment.ARG_ITEM_ID)
.addToBackStack(categories_fragment.ARG_ITEM_ID)
.commit();
}
})
);
//init categories recyclerview
GridLayoutManager LLM = new GridLayoutManager(activity, 2, LinearLayoutManager.VERTICAL, false){
@Override
public boolean canScrollVertically(){
return false;
}
};
categories_Rec_View.setLayoutManager(LLM);
categories_Rec_View.addOnItemTouchListener(
new RecyclerItemClickListener(this.getContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
//Log.d("categories","test click" + position);
Items_fragment nextFrag= new Items_fragment();
Bundle args = new Bundle();
Category clickedCategory = (Category)categories.get(position);
args.putInt("CategoryId", clickedCategory.getId());
args.putString("CategoryName", clickedCategory.getEnName());
args.putInt("BrandId", 0);
args.putInt("TabId", TabNo);
nextFrag.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.main_content, nextFrag, Items_fragment.ARG_ITEM_ID)
.addToBackStack(Items_fragment.ARG_ITEM_ID)
.commit();
}
})
);
return rootView;
}
private void findViewById(View view) {
BandsViewPager = (ViewPager) view.findViewById(R.id.man_view_pager);
mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
brands_Rec_View = (RecyclerView) view.findViewById(R.id.brands_RView);
categories_Rec_View = (RecyclerView) view.findViewById(R.id.categories_RView);
}
public void runnable(final int size) {
handler = new Handler();
animateViewPager = new Runnable() {
public void run() {
if (!stopSliding) {
if (BandsViewPager.getCurrentItem() == size - 1) {
BandsViewPager.setCurrentItem(0);
} else {
BandsViewPager.setCurrentItem(
BandsViewPager.getCurrentItem() + 1, true);
}
handler.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);
}
}
};
}
@Override
public void onResume() {
if (bands == null) {
sendRequest();
} else {
BandsViewPager.setAdapter(new ImageSlideAdapter(activity, bands,
man_fragment.this));
mIndicator.setViewPager(BandsViewPager);
runnable(bands.size());
//Re-run callback
handler.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);
}
super.onResume();
}
@Override
public void onPause() {
if (bands_task != null)
bands_task.cancel(true);
if (handler != null) {
//Remove callback
handler.removeCallbacks(animateViewPager);
}
super.onPause();
}
private void sendRequest() {
if (CheckNetworkConnection.isConnectionAvailable(activity)) {
bands_task = new RequestBandsTask(activity);
bands_task.execute(bandsurl);
brands_task = new RequestBrandsTask(activity);
brands_task.execute(brandsurl);
categories_task = new RequestCategoriesTask(activity);
categories_task.execute(categoriesurl);
} else {
message = getResources().getString(R.string.no_internet_connection);
showAlertDialog(message, true);
}
}
public void showAlertDialog(String message, final boolean finish) {
alertDialog = new AlertDialog.Builder(activity).create();
alertDialog.setMessage(message);
alertDialog.setCancelable(false);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (finish)
activity.finish();
}
});
alertDialog.show();
}
private class PageChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
if (bands != null) {
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private class RequestBandsTask extends AsyncTask<String, Void, List<Band>> {
private final WeakReference<Activity> activityWeakRef;
Throwable error;
public RequestBandsTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<Band> doInBackground(String... urls) {
try {
JSONObject jsonObject = getJsonObject(urls[0]);
if (jsonObject != null) {
// JSONObject jsonData = jsonObject
// .getJSONObject(TagName.TAG_BRANDS);
// if (jsonObject != null) {
bands = JsonReader.getBands(jsonObject);
// } else {
// message = jsonObject.getString(TagName.TAG_BRANDS);
// }
}
} catch (Exception e) {
e.printStackTrace();
}
return bands;
}
/**
* It returns jsonObject for the specified url.
*
* @param url
* @return JSONObject
*/
public JSONObject getJsonObject(String url) {
JSONObject jsonObject = null;
try {
jsonObject = GetJSONObject.getJSONObject(url);
} catch (Exception e) {
}
return jsonObject;
}
@Override
protected void onPostExecute(List<Band> result) {
super.onPostExecute(result);
if (activityWeakRef != null && !activityWeakRef.get().isFinishing()) {
if (error != null && error instanceof IOException) {
message = getResources().getString(R.string.time_out);
showAlertDialog(message, true);
} else if (error != null) {
message = getResources().getString(R.string.error_occured);
showAlertDialog(message, true);
} else {
bands = result;
if (result != null) {
if (bands != null && bands.size() != 0) {
//for brands_adapter
BandsViewPager.setAdapter(new ImageSlideAdapter(
activity, bands, man_fragment.this));
mIndicator.setViewPager(BandsViewPager);
runnable(bands.size());
handler.postDelayed(animateViewPager,
ANIM_VIEWPAGER_DELAY);
} else {
}
} else {
}
}
}
}
}
private class RequestBrandsTask extends AsyncTask<String, Void, List<Brand>> {
private final WeakReference<Activity> activityWeakRef;
Throwable error;
public RequestBrandsTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<Brand> doInBackground(String... urls) {
try {
JSONObject jsonObject = getJsonObject(urls[0]);
if (jsonObject != null) {
// JSONObject jsonData = jsonObject
// .getJSONObject(TagName.TAG_BRANDS);
// if (jsonObject != null) {
brands = JsonReader.getBrands(jsonObject);
// } else {
// message = jsonObject.getString(TagName.TAG_BRANDS);
// }
}
} catch (Exception e) {
e.printStackTrace();
}
return brands;
}
/**
* It returns jsonObject for the specified url.
*
* @param url
* @return JSONObject
*/
public JSONObject getJsonObject(String url) {
JSONObject jsonObject = null;
try {
jsonObject = GetJSONObject.getJSONObject(url);
} catch (Exception e) {
}
return jsonObject;
}
@Override
protected void onPostExecute(List<Brand> result) {
super.onPostExecute(result);
if (activityWeakRef != null && !activityWeakRef.get().isFinishing()) {
if (error != null && error instanceof IOException) {
message = getResources().getString(R.string.time_out);
showAlertDialog(message, true);
} else if (error != null) {
message = getResources().getString(R.string.error_occured);
showAlertDialog(message, true);
} else {
brands = result;
if (result != null) {
if (brands != null && brands.size() != 0) {
//for brands_adapter
brands_adapter = new Brands_View_Adapter(brands);
brands_Rec_View.setAdapter(brands_adapter);// set adapter on recyclerview
} else {
}
} else {
}
}
}
}
}
private class RequestCategoriesTask extends AsyncTask<String, Void, List<Category>> {
private final WeakReference<Activity> activityWeakRef;
Throwable error;
public RequestCategoriesTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<Category> doInBackground(String... urls) {
try {
JSONObject jsonObject = getJsonObject(urls[0]);
if (jsonObject != null) {
// JSONObject jsonData = jsonObject
// .getJSONObject(TagName.TAG_BRANDS);
// if (jsonObject != null) {
categories = JsonReader.getCategories(jsonObject);
// } else {
// message = jsonObject.getString(TagName.TAG_BRANDS);
// }
}
} catch (Exception e) {
e.printStackTrace();
}
return categories;
}
/**
* It returns jsonObject for the specified url.
*
* @param url
* @return JSONObject
*/
public JSONObject getJsonObject(String url) {
JSONObject jsonObject = null;
try {
jsonObject = GetJSONObject.getJSONObject(url);
} catch (Exception e) {
}
return jsonObject;
}
@Override
protected void onPostExecute(List<Category> result) {
super.onPostExecute(result);
if (activityWeakRef != null && !activityWeakRef.get().isFinishing()) {
if (error != null && error instanceof IOException) {
message = getResources().getString(R.string.time_out);
showAlertDialog(message, true);
} else if (error != null) {
message = getResources().getString(R.string.error_occured);
showAlertDialog(message, true);
} else {
categories = result;
if (result != null) {
if (categories != null && categories.size() != 0) {
//for brands_adapter
categories_adapter = new Categories_View_Adapter(categories);
categories_Rec_View.setAdapter(categories_adapter);// set adapter on recyclerview
categories_Rec_View.getLayoutParams().height = (188*(categories_adapter.getItemCount()+1)+328);
} else {
}
} else {
}
}
}
}
}
}
查看寻呼机适配器
ImageSlideAdapter.java
package com.techzone.yallaassouk.Adapters;
/**
* Created by Yazan on 5/1/2016.
*/
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.ImageView;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import com.techzone.yallaassouk.DataItems.Band;
import com.techzone.yallaassouk.R;
import com.techzone.yallaassouk.TouchImageView;
import com.techzone.yallaassouk.fragment.ProductDetailFragment;
import com.techzone.yallaassouk.man_fragment;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
public class ImageSlideAdapter extends PagerAdapter {
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options;
private ImageLoadingListener imageListener;
FragmentActivity activity;
List<Band> bands;
Fragment homeFragment;
public ImageSlideAdapter(FragmentActivity activity, List<Band> bands,
Fragment homeFragment) {
this.activity = activity;
this.homeFragment = homeFragment;
this.bands = bands;
options = new DisplayImageOptions.Builder()
.showImageOnFail(R.drawable.a1)
.showStubImage(R.drawable.a2)
.showImageForEmptyUri(R.drawable.a3).cacheInMemory()
.cacheOnDisc().build();
imageListener = new ImageDisplayListener();
}
@Override
public int getCount() {
return bands.size();
}
@Override
public View instantiateItem(ViewGroup container, final int position) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.vp_image, container, false);
ImageView mImageView = (ImageView) view
.findViewById(R.id.image_display);
imageLoader.displayImage(
((Band) bands.get(position)).getImageURL(), mImageView,
options, imageListener);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
private static class ImageDisplayListener extends
SimpleImageLoadingListener {
static final List<String> displayedImages = Collections
.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
}
抱歉没有格式化。我是新来的
最佳答案
我找到了导致“ViewPager”不触发“OnTouchListener”的原因
在布局 XML 文件中:
<com.techzone.yallaassouk.ExtendedViewPager
android:id="@+id/man_view_pager"
android:layout_width="fill_parent"
android:layout_height="137dp" />
<com.techzone.yallaassouk.Utils.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="130dp"/>
问题是“CirclePageIndicator”的“paddingTop”被设置为“130dp”,它覆盖了“ViewPager”,它正在接收触摸事件而不是“ViewPager”本身。
谢谢。
关于java - 为什么根本不调用 android ViewPager OnTouchListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37190231/
我目前面临将 ViewPager 放入另一个 ViewPager 中的问题,两者都可滑动。我成功创建了我的第一个 ViewPager,它包含三个页面,中间一个是第二个(内部)ViewPager,其中包
在我的主要 Activity 中,我有一个水平 viewpager。在 viewpager 的一个 fragment 中,我有另一个垂直 viewpager。两者都很好。但是对于水平 viewpage
我想创建一个 ViewPager(包含三个项目),其中每个 View 都是另一个 ViewPager(包含两个项目)。用户然后像这样滑动项目: ViewPager1[0] ViewPager2[0]
如何修复这个错误 setupWithViewPager(android.support.v4.view.ViewPager) in TabLayout cannot br applied to (an
我有一个包含 3 张图片的数组的 viewPage,我想在缩放其中一张时锁定 viewPager,因为当我在右侧滚动时,会出现下一张照片!这是我的代码 enter code here public
我是第一次实现 ViewPager,我遇到了一些问题,因为我收到以下错误: 06-20 10:40:51.366 11377-11377/com.example.ruelas.elite E/Andr
我真的是 android 的新手,如果对我的类(class)作业有任何帮助,我将不胜感激。 我需要做的: 1) 在一个 Activity 中有两个 ViewPagers(未嵌套) 2)两个ViewPa
我正在自定义我的 Android 应用程序以在四个方向上滑动,我正在使用 ViewPager 水平 和 VerticalViewPager 滑动垂直滑动(这就像对 DirectionalViewPag
我需要为平板电脑创建一个 ViewPager,在平板电脑的左侧会有导航按钮供用户选择。例如。 左侧导航将有 4 个图像按钮,每个项目将用户带到不同的教程。每个图像按钮都会加载一个 ViewPager。
我用过Vertical Viewpager在我的显示数据集合的项目中。在单个数据(项目)中,我有更多图像要显示。所以我尝试使用 viewpager。 但是当我水平滚动时,它会阻止垂直 Viewpage
我有一个父级 ViewPager,它的每个页面都包含一个子级 ViewPager。子 ViewPager 可能包含一个 ListView 或一个垂直的 ScrollView。我想将子级 ViewPag
我的任务是创建一个非常不正统的布局,只能通过另一个 viewpager 的第二个 Pane 内的 viewpager 来完成,幸运的是外部 viewpager 需要被锁定所以任务更现实一些,但是意外的
目标是根据屏幕方向(纵向或横向)以不同的布局在屏幕上显示两个 fragment ( fragment A、 fragment B)。 当设备处于纵向模式时,一次仅显示 Frag A 和 Frag B
我想禁用父 viewpager 中的子 viewpager 的滑动。 我目前使用这个自定义子 viewpager public class CustomViewPager extends ViewPa
我有两个 ViewPager - 一个在另一个下面。现在客户希望我们添加扩展底部 ViewPager(具有三个 ListView)以覆盖顶部 ViewPager 的可能性。当我将两个 ViewPage
我用谷歌搜索了这个问题很多天。找到一个解决方案,在Viewpager的开头和结尾添加一个空白View。当viewpager.getcurrentItem()==0时,则重新指向1。在我看来这种做法非常
我从适配器设置标题后,标题未出现在 PagerTabStrip 中 这是我的 Activity public class MainActivity extends AppCompatActivity
我有一个 ViewPager,其中包含多个 TextView,它们具有不同的字体大小。此外,我还获得了增加/减少字体大小的按钮,通过添加其默认大小加上一个名为 STEP 的值(该值通过 inc/dec
我有一个嵌套的 ViewPager,效果非常好。唯一的问题是,一旦子 ViewPager 位于最后一项并且我进一步滚动,父 ViewPager 就会滚动。我不想要这种行为。 我如何实现这一目标? 这是
我想显示一个带有滑动抽屉的 Viewpager。Viewpager 用于滚动图像,我想在该 viewpager 的顶部放置一个滑动抽屉(从顶部到按钮)。 我附上了我的示例代码。如果您对如何实现此屏幕有
我是一名优秀的程序员,十分优秀!