您好我正在使用第三方库自动滚动和触摸图像滑块中的事件,除了手动滑动图像滑块中的图像外,每件事情都能正常工作。我无法手动滑动图像。 我正在使用的库的名称是 “Trinea /机器人-自动滚动 - 视图 - 寻呼机”
以下是我使用自动滚动视图寻呼机的片段代码: -
public class HomeFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
TextView tv_slider, tv_button;
AutoScrollViewPager mViewPager;
private static int currentPage = 0;
private static int NUM_PAGES = 0;
private static final Integer[] IMAGES= {R.drawable.ecommerce, R.drawable.digital_marketing, R.drawable.explainer, R.drawable.it_services,
R.drawable.mobile_app, R.drawable.seo, R.drawable.software, R.drawable.webdesign};
private ArrayList<Integer> ImagesArray = new ArrayList<Integer>();
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
tv_slider=(TextView)v.findViewById(R.id.tv_slider);
tv_slider.setTypeface(EasyFonts.robotoLight(this.getActivity()));
tv_button=(TextView)v.findViewById(R.id.tv_button);
tv_button.setTypeface(EasyFonts.robotoLight(this.getActivity()));
for(int i=0;i<IMAGES.length;i++)
ImagesArray.add(IMAGES[i]);
mViewPager = (AutoScrollViewPager) v.findViewById(R.id.pager);
mViewPager.setAdapter(new SlidingImage_Adapter(getActivity(),ImagesArray));
mViewPager.setOffscreenPageLimit(1);
CirclePageIndicator indicator = (CirclePageIndicator)
v.findViewById(R.id.indicator);
indicator.setViewPager(mViewPager);
final float density = getResources().getDisplayMetrics().density;
//Set circle indicator radius
indicator.setRadius(3 * density);
NUM_PAGES =IMAGES.length;
mViewPager.startAutoScroll();
mViewPager.setInterval(3000);
mViewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.w("touched","down");
mViewPager.stopAutoScroll();
return true;
//break;
case MotionEvent.ACTION_UP:
Log.w("touched","up");
mViewPager.startAutoScroll();
return true;
}
return true;
}
});
// Pager listener over indicator
indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2) {
switch (pos){
case 0:
tv_slider.setText(getString(R.string.es_content));
break;
case 1:
tv_slider.setText(getString(R.string.dm_content));
break;
case 2:
tv_slider.setText(getString(R.string.ev_content));
break;
case 3:
tv_slider.setText(getString(R.string.it_content));
break;
case 4:
tv_slider.setText(getString(R.string.md_content));
break;
case 5:
tv_slider.setText(getString(R.string.seo_content));
break;
case 6:
tv_slider.setText(getString(R.string.sd_content));
break;
case 7:
tv_slider.setText(getString(R.string.wd_content));
break;
}
}
@Override
public void onPageScrollStateChanged(int pos) {
}
});
return v;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
@Override
public void onResume() {
super.onResume();
mViewPager.startAutoScroll();
}
@Override
public void onStop() {
super.onStop();
mViewPager.stopAutoScroll();
}
}
这是我的适配器类: -
public class SlidingImage_Adapter extends PagerAdapter {
private ArrayList<Integer> IMAGES;
private LayoutInflater inflater;
private Context context;
public SlidingImage_Adapter(Context context, ArrayList<Integer> IMAGES) {
super();
this.context = context;
this.IMAGES=IMAGES;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return IMAGES.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.sliding_images, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
imageView.setImageResource(IMAGES.get(position));
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
这是homefragment.xml文件
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/background"
tools:context="com.vamediabox.vamediaboxapp.activities.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/colorPrimary">
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/rl_slider"
android:gravity="center_horizontal"
app:cardUseCompatPadding="true"
app:cardElevation="4dp"
app:cardCornerRadius="3dp"
app:cardPreventCornerOverlap="false"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="2dp"
android:id="@+id/rl1">
<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_margin="2dp"
android:layout_alignParentTop="true" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/pager"
android:padding="5dip"
app:centered="true"
app:fillColor="@color/colorPrimary"
app:pageColor="@color/view_color"
app:snap="false" />
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="@+id/tv_slider"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textSize="14sp"
android:layout_below="@+id/indicator"
android:textColor="@color/slider_text"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
最后这是sliding_images.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1dip" >
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:scaleType="fitXY" />
</FrameLayout>
答案 0 :(得分:-1)
不确定您是否仍在寻找解决此问题的方法,请检查 AutoScrollViewPager 中的 consumeTouch ,这就是您的问题所在。
您需要确保
getParent().requestDisallowInterceptTouchEvent(true)
正在被召唤。
希望这会有所帮助。
干杯