我想在我的应用程序中使用图像滑块!
经过长时间的搜索,我发现我必须使用ViewPager
才能完成工作!
选择图像并左右滑动以在滑块中显示上一张和下一张图像时,我需要使Viewpager
变为全屏显示。
这是我的代码:
FullScreenFragment.java
package com.mustafataj.zoalvision.checkmyride.fragments;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import com.mustafataj.zoalvision.checkmyride.R;
import com.mustafataj.zoalvision.checkmyride.adapter.SlidingImage_Adapter;
import com.viewpagerindicator.CirclePageIndicator;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import butterknife.ButterKnife;
public class FullScreenFragment extends Fragment implements Runnable {
View v;
ViewPager pager;
Button share;
private static final Integer[] IMAGES= {R.drawable.demo_car_thumb_4,R.drawable.demo_car_thumb_3,R.drawable.demo_car_thumb_2,R.drawable.demo_car_thumb_1};
private ArrayList<Integer> ImagesArray = new ArrayList<Integer>();
public FullScreenFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_full_screen, container, false);
//pager = v.findViewById(R.id.pager);
share = v.findViewById(R.id.share);
// pager.setAdapter(new SlidingImage_Adapter(getActivity(),ImagesArray));
init();
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
File f = new File(Environment.getExternalStorageDirectory()+File.separator +"thumb.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/temp/jpg"));
startActivity(Intent.createChooser(share,"Share Image to ..."));
}
});
return v;
}
private void init() {
for(int i=0;i<IMAGES.length;i++)
ImagesArray.add(IMAGES[i]);
pager = (ViewPager) v.findViewById(R.id.pager);
pager.setAdapter(new SlidingImage_Adapter(getActivity(),ImagesArray));
}
@Override
public void onDestroy () {
super.onDestroy();
if (Build.VERSION.SDK_INT > 10) {
unregisterSystemUiVisibility();
}
exitFullscreen(getActivity());
}
public static boolean isImmersiveAvailable() {
return android.os.Build.VERSION.SDK_INT >= 19;
}
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
_handler.removeCallbacks(this);
_handler.postDelayed(this, 300);
} else {
_handler.removeCallbacks(this);
}
}
public void onKeyDown(int keyCode) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
_handler.removeCallbacks(this);
_handler.postDelayed(this, 500);
}
}
@Override
public void onStop() {
_handler.removeCallbacks(this);
super.onStop();
}
@Override
public void run() {
setFullscreen();
}
public void setFullscreen() {
setFullscreen(getActivity());
}
public void setFullscreen(Activity activity) {
if (Build.VERSION.SDK_INT > 10) {
int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN;
if (isImmersiveAvailable()) {
flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
} else {
activity.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
public void exitFullscreen(Activity activity) {
if (Build.VERSION.SDK_INT > 10) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
} else {
activity.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
private Handler _handler = new Handler();
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void registerSystemUiVisibility() {
final View decorView = getActivity().getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
setFullscreen();
}
}
});
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void unregisterSystemUiVisibility() {
final View decorView = getActivity().getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(null);
}}
这是我的适配器类:
SlidingImage_Adapter.java:
package com.mustafataj.zoalvision.checkmyride.adapter;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.mustafataj.zoalvision.checkmyride.R;
import java.util.ArrayList;
/**
* Created by Shashoug on 02/07/18.
*/
public class SlidingImage_Adapter extends PagerAdapter {
private ArrayList<Integer> IMAGES ;
private LayoutInflater inflater;
private Context context;
public SlidingImage_Adapter(Context context,ArrayList<Integer> IMAGES) {
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_image_layout, 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;
}}
这是我的fragment_full_screen.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"
android:background="@color/ms_black" tools:context="com.mustafataj.zoalvision.checkmyride.fragments.FullScreenFragmen">
<Button
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:backgroundTint="#ED6326"
android:background="@drawable/ic_share"
android:id="@+id/share"
android:layout_width="30dp"
android:layout_height="30dp" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
Full_screen_viewer.xml:
<FrameLayout 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"
tools:context="com.mustafataj.zoalvision.checkmyride.fragments.FullScreenFragment">
<fragment
class="com.mustafataj.zoalvision.checkmyride.fragments.FullScreenFragment"
android:id="@+id/titles"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
当我单击以显示全屏片段时,没有弹出窗口且没有任何错误? 如何解决? 如果有什么办法可以实现我的想法,请随时指导我。 在此先感谢:)