How to open multiple images from sd card in viewpager using fragment? - java

I am new to android, I am making an app which can select and open multiple images from the gallery and can be viewed as slides. Then use paint canvas to draw on the images and record the whole screen.
I have managed to do all the parts except loading multiple images from the gallery into my app.
I can open images from drawable but finding difficulty to open from the gallery.
I am using ViewPager and fragment to open images from drawable, plz let me know a way to open multiple images from the gallery and show in my ViewPager.
page fragment class
package com.example.test2;
public class PageFragment extends Fragment {
private int imageResource;
private Bitmap bitmap;
public static PageFragment getInstance(int resourceID) {
PageFragment f = new PageFragment();
Bundle args = new Bundle();
args.putInt("image_source", resourceID);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageResource = getArguments().getInt("image_source");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_page, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 4;
o.inDither = false;
bitmap = BitmapFactory.decodeResource(getResources(), imageResource, o);
imageView.setImageBitmap(bitmap);
}
#Override
public void onDestroy() {
super.onDestroy();
bitmap.recycle();
bitmap = null;
}
}
viewPagerAdapter class
package com.example.test2;
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<Integer> images;
public ViewPagerAdapter(FragmentManager fm, List<Integer> imagesList) {
super(fm);
this.images = imagesList;
}
#Override
public Fragment getItem(int position) {
return PageFragment.getInstance(images.get(position));
}
#Override
public int getCount() {
return images.size();
}
}
MainActivity
package com.example.test2;
public class MainActivity extends AppCompatActivity {
ImageButton draw;
RelativeLayout relativeLayout;
Paint paint;
View view;
Path path2;
Bitmap bitmap;
Canvas canvas;
private ArrayList<Integer> images;
private BitmapFactory.Options options;
private ViewPager viewPager;
private View btnNext, btnPrev;
private FragmentStatePagerAdapter adapter;
private LinearLayout thumbnailsContainer;
private final static Integer[] resourceIDs = new Integer[]{R.mipmap.image7,R.mipmap.image6,R.mipmap.image5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
images = new ArrayList<>();
//find view by id
viewPager = (ViewPager) findViewById(R.id.view_pager);
thumbnailsContainer = (LinearLayout) findViewById(R.id.container);
btnNext = findViewById(R.id.next);
btnPrev = findViewById(R.id.prev);
btnPrev.setOnClickListener(onClickListener(0));
btnNext.setOnClickListener(onClickListener(1));
setImagesData();
// init viewpager adapter and attach
adapter = new ViewPagerAdapter(getSupportFragmentManager(), images);
viewPager.setAdapter(adapter);
inflateThumbnails();
relativeLayout = (RelativeLayout) findViewById(R.id.layout);
draw= findViewById(R.id.b);
draw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setTitle("");
dialog.setContentView(R.layout.menu);
dialog.show();
Button black = dialog.findViewById(R.id.Black);
Button blue = dialog.findViewById(R.id.Blue);
Button red = dialog.findViewById(R.id.Red);
black.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
color();
paint.setColor(Color.parseColor("black"));
dialog.dismiss();
}
});
blue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
color();
paint.setColor(Color.parseColor("blue"));
dialog.dismiss();
}
});
red.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
color();
paint.setColor(Color.parseColor("red"));
dialog.dismiss();
}
});
}
});
}
private View.OnClickListener onClickListener(final int i) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
if (i > 0) {
//next page
if (viewPager.getCurrentItem() < viewPager.getAdapter().getCount() - 1) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
} else {
//previous page
if (viewPager.getCurrentItem() > 0) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
}
}
};
}
private void setImagesData() {
for (int i = 0; i < resourceIDs.length; i++) {
images.add(resourceIDs[i]);
}
}
private void inflateThumbnails() {
for (int i = 0; i < images.size(); i++) {
View imageLayout = getLayoutInflater().inflate(R.layout.item_image, null);
ImageView imageView = (ImageView) imageLayout.findViewById(R.id.img_thumb);
imageView.setOnClickListener(onChagePageClickListener(i));
options = new BitmapFactory.Options();
options.inSampleSize = 3;
options.inDither = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),images.get(i),options);
imageView.setImageBitmap(bitmap);
//set to image view
imageView.setImageBitmap(bitmap);
//add imageview
thumbnailsContainer.addView(imageLayout);
}
}
private View.OnClickListener onChagePageClickListener(final int i) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(i);
}
};
}
public void color()
{
view = new SketchSheetView(MainActivity.this);
paint = new Paint();
path2 = new Path();
relativeLayout.addView(view, new ViewGroup.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(6);
}
class SketchSheetView extends View {
public SketchSheetView(Context context) {
super(context);
bitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_4444);
canvas = new Canvas(bitmap);
this.setBackgroundColor(Color.TRANSPARENT);
}
private ArrayList<DrawingClass> DrawingClassArrayList = new ArrayList<DrawingClass>();
#Override
public boolean onTouchEvent(MotionEvent event) {
DrawingClass pathWithPaint = new DrawingClass();
canvas.drawPath(path2, paint);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
path2.moveTo(event.getX(), event.getY());
path2.lineTo(event.getX(), event.getY());
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
path2.lineTo(event.getX(), event.getY());
pathWithPaint.setPath(path2);
pathWithPaint.setPaint(paint);
DrawingClassArrayList.add(pathWithPaint);
}
invalidate();
return true;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (DrawingClassArrayList.size() > 0) {
canvas.drawPath(
DrawingClassArrayList.get(DrawingClassArrayList.size() - 1).getPath(),
DrawingClassArrayList.get(DrawingClassArrayList.size() - 1).getPaint());
}
}
}
public class DrawingClass {
Path DrawingClassPath;
Paint DrawingClassPaint;
private Path getPath() {
return DrawingClassPath;
}
private void setPath(Path path) {
this.DrawingClassPath = path;
}
private Paint getPaint() {
return DrawingClassPaint;
}
private void setPaint(Paint paint) {
this.DrawingClassPaint = paint;
}
}
}

I'm not sure it's possible directly since to reach the gallery, you need to call an intent.
I suggest you to launch a method to retrieve photos in the gallery, following this example: enter link description here, to save all images into a list, and to display pictures in viewpager like you've aldready done.

Related

add next, prev buttons in slider image made by picasso and viewpager

I've made a image slider with help of viewpager and picasso, now i want to add next, prev button on the image, how can i do that?
I saw different answers of this but all of them are showing error
This is main activity where image slider is shown:
public class MainActivity extends AppCompatActivity {
private int[] imageUrls = new int[]{
R.drawable.after_cookie,
R.drawable.before_cookie
};
private WebBackForwardList viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.view_pager);
ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
}
}
This is ViewPageAdapter:
public class ViewPageAdapter extends PagerAdapter {
private Context context;
private int[] imageUrls;
ViewPageAdapter(Context context, int[] imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
}
#Override
public int getCount() {
return imageUrls.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
Picasso.get()
.load(imageUrls[position])
.fit()
.centerCrop()
.into(imageView);
container.addView(imageView);
return imageView;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
}
add this method to your code
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
then use this for next button
Button NextButton = (Button)findViewById(R.id.button1);
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(getItem(+1), true); //getItem(+1) for next
}
});
and use this for the previous button
Button PreviousButton = (Button)findViewById(R.id.button2);
PreviousButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(getItem(-1), true); //getItem(-1) for previous
}
});
it will work fine ;)

Go to the item selected on the RecyclerView

Problem: I want to go to the user's selected item
But I can not do it Selected position.
How can I get my position and stay in place???
I tried many ways but it was fruitless
Does my code have problems?
Any way to your opinion to help me
enter image description here
Code Adapter :
public class Adapter_Badan extends RecyclerView.Adapter<Adapter_Badan.ViewHolder> {
private Context context;
private List<Modell_Badan> modells;
public Adapter_Badan(Context context, List<Modell_Badan> modells) {
this.modells = modells;
this.context = context;
}
#NonNull
#Override
public Adapter_Badan.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new Adapter_Badan.ViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_badan, parent, false));
}
#Override
public void onBindViewHolder(#NonNull final Adapter_Badan.ViewHolder holder, final int position) {
final Modell_Badan modell = modells.get(position);
holder.title.setText(modell.getTitle());
holder.content.setText(modell.getContent());
Picasso.get().load(modell.getImage()).into(holder.image1);
Picasso.get().load(modell.getImage2()).into(holder.image2);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent= new Intent(context,Badan.class);
intent.putExtra(Modell.ID,modell.getId());
intent.putExtra(Modell.TITLE,modell.getTitle());
intent.putExtra(Modell.CONTENT,modell.getContent());
intent.putExtra(Modell.IMAGE1,modell.getImage());
intent.putExtra(Modell.IMAGE12,modell.getImage2());
intent.putExtra("id", "1");
context.startActivity(intent);
((Activity)context).finish();
((Activity)context).overridePendingTransition(R.anim.alpha_f,R.anim.alpha_o);
}
}, 20);
}
});
}
#Override
public int getItemCount() {
return modells.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView image1;
private ImageView image2;
private TextView title;
private TextView content;
private RelativeLayout relativeLayout;
public ViewHolder(View itemView) {
super(itemView);
image1 = (ImageView) itemView.findViewById(R.id.image_badan);
image2 = (ImageView) itemView.findViewById(R.id.image2_badan);
title = (TextView) itemView.findViewById(R.id.title_badan);
content = (TextView) itemView.findViewById(R.id.content_badan);
}
}
But after the item is selected, the position is deleted
Can I prove the item's position?
Code Badan :
public class Badan extends AppCompatActivity {
private SharedP_Thems sharedP_thems;
private ImageView image_layout, image_layout2;
private TextView title_, content_;
private SharedPerfern sharedPerfern;
private Shared_Modell shared_modell;
private Adapter_Badan adapter_badan;
int a;
#Override
protected void onCreate(Bundle savedInstanceState) {
sharedPerfern = new SharedPerfern(this);
shared_modell = sharedPerfern.getShared();
sharedP_thems = new SharedP_Thems(this);
if (sharedP_thems.getLoad().equals(true))
setTheme(R.style.dark_them);
else setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_badan);
setUpToolbarLayout();
setUpView();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recy_badanw);
Adapter_Badan adapter_badan=new Adapter_Badan(this, Data_FackeGen.getModellBadan(this));
recyclerView.setLayoutManager( new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.HORIZONTAL));
recyclerView.setAdapter(adapter_badan);
getInten();
title_.setTextSize(shared_modell.getFonSize());
content_.setTextSize(shared_modell.getFonSize());
}
private void setUpToolbarLayout() {
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.tollbar_badab);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private void setUpView() {
image_layout = (ImageView) findViewById(R.id.image_badan2w);
image_layout2 = (ImageView) findViewById(R.id.image2_badanw);
title_ = (TextView) findViewById(R.id.title_badanw);
content_ = (TextView) findViewById(R.id.content_badanw);
}
private void getInten() {
Intent intent = getIntent();
int id = intent.getIntExtra(Modell.ID, 0);
String titel = intent.getStringExtra(Modell.TITLE);
String content = intent.getStringExtra(Modell.CONTENT);
int image1 = intent.getIntExtra(Modell.IMAGE1, 2);
int image2 = intent.getIntExtra(Modell.IMAGE12, 2);
Picasso.get().load(image1).into(image_layout);
Picasso.get().load(image2).into(image_layout2);
title_.setText(titel);
content_.setText(content);
}
}
Try moving the following code inside your click listener:
Modell_Badan modell = modells.get(position);
holder.title.setText(modell.getTitle());
holder.content.setText(modell.getContent());
Picasso.get().load(modell.getImage()).into(holder.image1);
Picasso.get().load(modell.getImage2()).into(holder.image2);
The problem was solved
I used the following method and responded
#Override
protected void onPause() {
super.onPause();
bundle = new Bundle();
Parcelable parcelable = recyclerView.getLayoutManager().onSaveInstanceState();
bundle.putParcelable(KEY_RECYCLER_STATE, parcelable);
}
#Override
protected void onResume()
{
super.onResume();
// restore RecyclerView state
if (bundle != null) {
Parcelable parcelable = bundle.getParcelable(KEY_RECYCLER_STATE);
recyclerView.getLayoutManager().onRestoreInstanceState(parcelable);
}
}

Force close app on recyclerview Scroll

When I scroll on Recyclerview in my app it closes.
Here is a screenshot:
The main page of app- when i scroll the reyclerview of main page my app was force close:
This is my adapter:
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> {
private ArrayList<SectionDataModel> dataList;
private Context mContext;
private ImageView bg;
private boolean isLang;
public RecyclerViewDataAdapter(Context context, ArrayList<SectionDataModel> dataList) {
this.dataList = dataList;
this.mContext = context;
}
#Override
public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
ItemRowHolder mh = new ItemRowHolder(v);
return mh;
}
#Override
public void onBindViewHolder(final ItemRowHolder itemRowHolder, int i) {
final String sectionName = dataList.get(i).getHeaderTitle();
final String sectionSub = dataList.get(i).getHeaderSubTitle();
final int sectionhesder = dataList.get(i).getHeader();
final int bgd = dataList.get(i).getPhoto();
final int bgg = dataList.get(i).getBg();
ArrayList singleSectionItems = dataList.get(i).getAllItemsInSection();
itemRowHolder.itemSubTitle.setText(sectionSub);
itemRowHolder.itemTitle.setText(sectionName);
itemRowHolder.bg.setImageResource(bgd);
itemRowHolder.bg.setBackgroundResource(bgg);
if (isLang = Locale.getDefault().getLanguage().equals("fa")) {
itemRowHolder.bg.setScaleType(ImageView.ScaleType.FIT_START);
} else {
itemRowHolder.bg.setScaleType(ImageView.ScaleType.FIT_END);
}
SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, singleSectionItems);
itemRowHolder.recycler_view_list.setHasFixedSize(true);
itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);
itemRowHolder.recycler_view_list.setNestedScrollingEnabled(false);
/* itemRowHolder.recycler_view_list.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
//Allow ScrollView to intercept touch events once again.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle RecyclerView touch events.
v.onTouchEvent(event);
return true;
}
});*/
itemRowHolder.btnMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, TopicActivity.class);
intent.putExtra("topicn",sectionName.toString());
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
mContext.startActivity(intent);
}
});
/* Glide.with(mContext)
.load(feedItem.getImageURL())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.error(R.drawable.bg)
.into(feedListRowHolder.thumbView);*/
}
#Override
public int getItemCount() {
return (null != dataList ? dataList.size() : 0);
}
public class ItemRowHolder extends RecyclerView.ViewHolder {
protected RelativeLayout itemHeader;
protected TextView itemTitle;
protected TextView itemSubTitle;
protected RecyclerView recycler_view_list;
protected Button btnMore;
public ImageView bg;
public ItemRowHolder(View view) {
super(view);
this.itemTitle = (TextView) view.findViewById(R.id.itemTitle);
this.itemSubTitle = (TextView) view.findViewById(R.id.itemsub);
this.itemHeader = (RelativeLayout) view.findViewById(R.id.header);
this.recycler_view_list = (RecyclerView) view.findViewById(R.id.recycler_view_list);
this.btnMore= (Button) view.findViewById(R.id.btnMore);
this.bg= (ImageView) view.findViewById(R.id.bg);
}}}

OnClickListener in PagerAdapter

I have an Activity that uses a ViewPager, within this ViewPager is a TouchImageView (an ImageView that detects gestures and zooms/moves accordingly).
Now I want to change some things in the UI whenever the image is clicked, but because the TouchImageView is declared in the PagerAdapter I can't reach it and if I would setup something on the side of the PagerAdapter I couldn't reach the UI components I want to change (I would also like to use the same adapter somewhere else in my app when I got this working). I've tried setting up an onClickListener on the ViewPager itself, but that doesn't trigger. Now I really don't know what to do.
Here is the adapter:
public class FullScreenImageAdapter extends PagerAdapter {
private ArrayList<MediaFile> images;
public FullScreenImageAdapter( ArrayList<MediaFile> images) {
this.images = images;
}
#Override
public int getCount() {
return images.size();
}
#Override
public View instantiateItem(ViewGroup container, int position) {
TouchImageView img = new TouchImageView(container.getContext());
Bitmap bitmap = BitmapFactory.decodeFile(images.get(position).getPath());
img.setImageBitmap(bitmap);
container.addView(img, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT);
return img;
}
#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;
}
}
And here is the method that interacts with it (in the ProjectOverviewActivity class):
private void viewPicture(final int position) {
View decorView = getWindow().getDecorView();
statusBarVisibility = decorView.getSystemUiVisibility();
// Hide the status bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
FullScreenImageAdapter imageAdapter = new FullScreenImageAdapter(mediaFiles);
final ViewPager viewPager = (ViewPager) findViewById(R.id.image_pager);
viewPager.setAdapter(imageAdapter);
viewPager.setCurrentItem(position);
View fancyBox = findViewById(R.id.fancyBox);
fancyBox.setVisibility(View.VISIBLE);
viewPager.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View btnClose = findViewById(R.id.fancyBoxBtnClose);
View btnPrev = findViewById(R.id.fancyBoxBtnPrev);
View btnNext = findViewById(R.id.fancyBoxBtnNext);
if (overlayButtonsVisible) {
btnClose.setVisibility(View.GONE);
btnPrev.setVisibility(View.GONE);
btnNext.setVisibility(View.GONE);
} else {
btnClose.setVisibility(View.VISIBLE);
if (viewPager.getCurrentItem() > 0) {
btnPrev.setVisibility(View.VISIBLE);
}
if (viewPager.getCurrentItem() < (mediaFiles.size() - 1)) {
btnNext.setVisibility(View.VISIBLE);
}
}
overlayButtonsVisible = !overlayButtonsVisible;
}
});
View fancyBoxBtnClose = findViewById(R.id.fancyBoxBtnClose);
final View fancyBoxBtnPrev = findViewById(R.id.fancyBoxBtnPrev);
final View fancyBoxBtnNext = findViewById(R.id.fancyBoxBtnNext);
fancyBoxBtnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View decorView = getWindow().getDecorView();
// Show the status bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
decorView.setSystemUiVisibility(statusBarVisibility);
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, statusBarVisibility);
}
findViewById(R.id.fancyBox).setVisibility(View.GONE);
setViewAndChildrenEnabled(findViewById(R.id.layoutProjectOverview), true);
overlayButtonsVisible = false;
}
});
fancyBoxBtnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
if (viewPager.getCurrentItem() == 0) {
fancyBoxBtnPrev.setVisibility(View.GONE);
}
}
});
fancyBoxBtnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
if (viewPager.getCurrentItem() == (mediaFiles.size() - 1)) {
fancyBoxBtnNext.setVisibility(View.GONE);
}
}
});
}
You can try this
Update:
I think you should create your custom ViewPager with GestureDetector
Example:
public class ClickableViewPager extends ViewPager {
private OnItemClickListener mOnItemClickListener;
public ClickableViewPager(Context context) {
super(context);
setup();
}
public ClickableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
setup();
}
private void setup() {
final GestureDetector tapGestureDetector = new GestureDetector(getContext(), new TapGestureListener());
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
tapGestureDetector.onTouchEvent(event);
return false;
}
});
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onItemClick(int position);
}
private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if(mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(getCurrentItem());
}
return true;
}
}
}
and then in your activity
ClickableViewPager viewPager = (ClickableViewPager) findViewById(R.id.viewPager);
viewPager.setOnItemClickListener(new ClickableViewPager.OnItemClickListener() {
#Override
public void onItemClick(int position) {
// HADNLE YOUR CLICKS
}
});
SOURCE

How to change Tabs of a TabLayout from Navigation Drawer

Currently I'm changing Tabs from NavigationDrawer but it's not a right way and it takes too long to change tabs from NavigationDrawer becuase I'm replacing Main Fragment of Tabs each time and I don't want it. For more
NavigationDrawerAdapter.java
public class NavDrawerListAdapter extends BaseAdapter {
public Context context;
public MainActivity activity;
public MainTabFragment fragment;
int currentSelectedPostion;
LayoutInflater mInflater;
TextView tv_signup;
SharedPreferences share;
boolean cbc = false;
String fis = "", las = "";
int group_id = 0;
private String[] titles;
private int[] images;
private int[] selectedposition;
public NavDrawerListAdapter(Context context, int[] selectedposition, boolean cb, String fis, String las) {
this.context = context;
this.images = images;
this.cbc = cb;
this.fis = fis;
this.las = las;
share = context.getSharedPreferences("sharePref", 0);
group_id = share.getInt("group_id", 0);
this.selectedposition = selectedposition;
mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return 1;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(com.cws.advisorymandi.R.layout.drawer_list_item, null);
}
LinearLayout ll_contact_us = (LinearLayout) convertView.findViewById(com.cws.advisorymandi.R.id.contact_item);
RelativeLayout ll_login = (RelativeLayout) convertView.findViewById(com.cws.advisorymandi.R.id.signup_item);
LinearLayout ll_equity = (LinearLayout) convertView.findViewById(com.cws.advisorymandi.R.id.equity_item);
LinearLayout ll_indices = (LinearLayout) convertView.findViewById(com.cws.advisorymandi.R.id.indices_item);
if (cbc) {
tv_signup.setText("Welcome " + fis + " " + las);
tv_logout.setVisibility(View.VISIBLE);
tv_edit.setVisibility(View.VISIBLE);
}
ll_equity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.getInstance().displayView(3, 0, 0);
}
});
ll_indices.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.getInstance().displayView(3, 0, 1);
}
});
});
return convertView;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
public ListView mDrawerList;
NavDrawerListAdapter adapter;
public static MainActivity getInstance() {
return sMainActivity;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.cws.advisorymandi.R.layout.activity_main)
mDrawerList = (ListView) findViewById(com.cws.advisorymandi.R.id.list_slidermenu);
adapter = new NavDrawerListAdapter(getApplicationContext(), selectedposition, cb, firstName, lastName);
mDrawerList.setAdapter(adapter);
}
/**
* Diplaying fragment view for selected nav drawer list item
*/
public int displayView(int position, int position2, int position3) {
// update the main content by replacing fragments
first_position = position;
second_position = position2;
third_position = position3;
fragment = null;
switch (position) {
case 0:
sharedPreferences = getSharedPreferences("sharePref", 0);
cb = sharedPreferences.getBoolean("ConfirmLogin", false);
if (cb) {
} else new Handler().postDelayed(new Runnable() {
#Override
public void run() {
fragment = new LoginActivity();
changeFragments3();
}
}, 150);
mDrawerLayout.closeDrawer(mDrawerList);
break;
case 3:
// fragment = new MainTabFragment();
/* new Handler().postDelayed(new Runnable() {
#Override
public void run() {*/
fragment = new MainTabFragment();
changeFragments();
/*}
}, 150);
mDrawerLayout.closeDrawer(mDrawerList);*/
break;
}
return 0;
}
}
Note: I created each drawer item without using an array or arrayList so onItemClickListener isn't also working in it.

Categories

Resources