Android Arsenal NavigationTabBar setting fragments or Activity for models - java

NavigationTabBar View Pager with Tab.
Github Link
I want to set Fragments in the viewpager but I'm not getting any far with what I'm doing might be because I'm naive in Android. Though I'm attaching code they used in exapmle application.Though I know ViwPager will handle the job. Can any one help me out where I have to call the Fragment and What code I should put in Fragment Class.
final ViewPager viewPager = (ViewPager) findViewById(R.id.vp_horizontal_ntb);
viewPager.setAdapter(new PagerAdapter() {
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view.equals(o);
}
#Override
public void destroyItem(final View container, final int position, final Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
Log.d("Page ", Integer.toString(position));
final View view = LayoutInflater.from(
getBaseContext()).inflate(R.layout.item_vp, null, false);
final TextView txtPage = (TextView)view.findViewById(R.id.txt_vp_item_page);
txtPage.setText(String.format("Page #%d", position));
container.addView(view);
return view;
}
});

Related

How do I place my image adapter (it is an infinite cycler view) in a fragment?

I'm setting up an image adapter in my fragments. I recently learn the usefulness of Fragments and Therefore I am trying to switch from the traditional activities to a central activity with multiple fragments. But I cant get to display my images' infinite cycler view.
I have tried reading answers from previous questions here on Stack overflow (ImageAdapter cannot be applied to fragment class and ImageAdapter cannot be applied to a Fragment Class) but I didnt really understand anything perhaps because I am a beginner in android studio with no educational background in coding. I have also tried youtube and everywhere.
I havent found tutorials to do this, I understand that fragments is relatively new in android studio
This is the fragment I am trying to switch to
public TrendingFragment() {
// Required empty public constructor
}
private static final String TAG = "Trending";
HorizontalInfiniteCycleViewPager viewPager;
List<TrendingHolder> TrendingList = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragme
View view = inflater.inflate(R.layout.fragment_trending, container, false);
initData();
viewPager = view.findViewById(R.id.view_pager);
viewPager.setAdapter(new TrendingAdapter(getActivity()));
return view;
}
private void initData() {
//Adding the Images on board
TrendingList.add(new TrendingHolder(R.drawable.ad_test));
TrendingList.add(new TrendingHolder(R.drawable.burger_test));
TrendingList.add(new TrendingHolder(R.drawable.italian_test));
TrendingList.add(new TrendingHolder(R.drawable.pizza_test));
}
}
This is my adapter class
public class TrendingAdapter extends PagerAdapter {
Context context;
List<TrendingHolder> TrendingList;
private static final String TAG = "TrendingAdapter";
public TrendingAdapter(Context context, List<TrendingHolder> trendingList) {
this.context = context;
this.TrendingList = trendingList;
}
#Override
public int getCount() {
return TrendingList.size();
}
#Override
public boolean isViewFromObject(View view, #NonNull Object o){
return view.equals(o);
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View)object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
//Inflate View
View view = LayoutInflater.from(context).inflate(R.layout.card_item, container, false);
//View
ImageView trending_image = (ImageView)view.findViewById(R.id.trending_holder);
//Set Data
trending_image.setImageResource(TrendingList.get(position).getImage());
//set On Event Click
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//shop activity comes here
Log.d(TAG, "onClick: added to Cart");
}
});
container.addView(view);
return view;
}
}
This is my model for the images
public class TrendingHolder {
private int image;
public TrendingHolder(){
}
public TrendingHolder(int image) {
this.image = image;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
I am getting this error TrendingAdapter( ) 
in TrendingAdapter cannot be applied
to
(androidx.fragment.app.FragmentActivity) in my Trending Fragment
It seems like missing constructor parameter. The constructor of TrendingAdapter accepts 2 parameters public TrendingAdapter(Context context, List<TrendingHolder> trendingList). But you pass 1 parameter viewPager.setAdapter(new TrendingAdapter(getActivity()));.

Drag and drop from custom slider doesn't work

I'm trying to make a drag and drop from a custom slider.
The slider is in a viewPager and is shows Imageviews.
I don't know witch ID I should set to my longClickListener.
I tried the Imagview (swip_image_view) but it return null.
& it doesn't work with the viewpager.
Why is my longClickListener not working?
public class Activity_Kind extends Activity {
ViewPager viewPager;
CustomHorizontalSwip customHorizontalSwip;
private GridView gridDieren;
private ImageView bus;
private ImageView slider;
private TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kies_dier);
gridDieren = (GridView) findViewById(R.id.gridSelectDieren);
text = (TextView) findViewById(R.id.kiesdiertext);
bus = (ImageView) findViewById(R.id.imageBus);
viewPager = (ViewPager) findViewById(R.id.viewPager);
customHorizontalSwip = new CustomHorizontalSwip(this);
viewPager.setAdapter(customHorizontalSwip);
viewPager.setLongClickable(true);
viewPager.setOnLongClickListener(longClickListener);
//slider = (ImageView) findViewById(R.id.swip_image_view);
//slider.setOnLongClickListener(longClickListener);
bus.setOnDragListener(dragListener);
}
View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder myShadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, myShadowBuilder, v, 0);
return true;
}
};
View.OnDragListener dragListener = new View.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
int dragEvent = event.getAction();
switch (dragEvent) {
case DragEvent.ACTION_DRAG_ENTERED:
final View view = (View) event.getLocalState(); //Krijgt alle informatie van die view die wordt gedraged
if (view.getId() == R.id.imageBus);
text.setText("Werkt");
//gridDieren.addView(view);
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
case DragEvent.ACTION_DROP:
break;
}
return true;
}
};
}
public class CustomHorizontalSwip extends PagerAdapter {
private int [] imageResources ={R.drawable.beer,R.drawable.bever, R.drawable.kip, R.drawable.leeuw, R.drawable.nijlpaard, R.drawable.poes
,R.drawable.ezel,R.drawable.hond, R.drawable.ooievaar, R.drawable.varken, R.drawable.wormpje};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomHorizontalSwip(Context c) {
ctx=c;
}
#Override
public int getCount() {
return imageResources.length;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView=layoutInflater.inflate(R.layout.custom_swipe_dieren,container,false);
ImageView imageView=(ImageView) itemView.findViewById(R.id.swip_image_view);
imageView.setImageResource(imageResources[position]);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==object);
}
}
Your image swip_image_view is inside layout custom_swipe_dieren.xml. You should set onLongClickListener inside CustomHorizontalSwip -> instantiateItem() method like this :
public class CustomHorizontalSwip extends PagerAdapter {
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView=layoutInflater.inflate(R.layout.custom_swipe_dieren,container,false);
ImageView imageView=(ImageView) itemView.findViewById(R.id.swip_image_view);
imageView.setImageResource(imageResources[position]);
imageView.setOnLongClickListener(longClickListener);
container.addView(itemView);
return itemView;
}
/* Declare and Initialise longClickListener in this file also */
/* Also do same for dragListener in this file */
}
This will work for you
Thanks

java.lang.IllegalStateException: ViewPager

My application gets all images URL from server and saves that to an ArrayList and displays these images in ViewPager. But it generates a IllegalStateException. Adapter given below:
public class FullScreenImageAdapter extends PagerAdapter {
private Context _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Context activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
#Override
public int getCount() {
return this._imagePaths.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.item, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.cardImage);
Picasso.with(_activity).load(_imagePaths.get(position)).into(imgDisplay);
((ViewPager) container).addView(viewLayout, 0);
return viewLayout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Adapter created as
FullScreenImageAdapter adapter=new FullScreenImageAdapter(FullScreenActivity.this,all_url);
viewPager.setAdapter(adapter);
And the log looks like below:
java.lang.IllegalStateException: The application's PagerAdapter
> changed the adapter's contents without calling
> PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1,
> found: 3 Pager id: com.wat.clickzy:id/view_pager Pager class: class
> android.support.v4.view.ViewPager Problematic adapter: class
> com.wat.clickzy.FullScreenImageAdapter
Please help me
You need to call notifysetdatachanged on the adapter that you're using, every time you're adding/removing something to that adapter.
Look here for even more clarity.

ViewPagerAdapter "id cannot be resolved or is not a field "

I have the following code in eclipse and i can't put the correct layout and imageview because cannot be resolved or are not a field
public ViewPagerAdapter(Context context, String[] gallery) {
this.context = context;
this.gallery = gallery;
}
#Override
public int getCount() {
return gallery.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
// Locate the TextViews in viewpager_item.xml
ImageView imgflag = (ImageView) itemView.findViewById(R.id.image);
Picasso.with(context).load(gallery[position]).into(imgflag);
((ViewPager) container).addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((View) object);
}
I tried cleanning the project importing R, and making rigth click in my project/"Android tools"/"Fix project properties".
I have this problem only when i create an adapter class
Try to use group of key Ctr + Shift + o to let eclipse auto import neccessary things for your project.

Listview inside tabpager fragment not showing

Im trying to add a listview inside a fragment witch is also inside a tabhost. the app is not crashing or anything, but the list view is not showing anything.
Here is my code:
Class for the tabPager
public class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentHome();
case 1:
return new FragmentBadges();
case 2:
return new FragmentBenefits();
}
return null;
}
Following is the fragment where I want the listview:
public class FragmentBadges extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_badges_list, null);
BadgesListAdapter adapter = new BadgesListAdapter(this.getActivity(), getCurrentBadges());
ListView badgeslist = (ListView) rootView.findViewById(R.id.badges_list);
badgeslist.setAdapter(adapter);
badgeslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//TODO do some
}
});
return rootView;
}
And following is the adapter:
public class BadgesListAdapter extends ArrayAdapter<String> {
private final Activity context;
private List<Badge> badges;
public BadgesListAdapter(Activity context, List<Badge> badges) {
super(context, R.layout.fragment_badges_listrow);
this.context = context;
this.badges = badges;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.fragment_badges_listrow, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.text);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(badges.get(position).getName());
imageView.setImageResource(badges.get(position).getDrawableValue());
return rowView;
}
while debugging I found out that the getView method inside the badgesListAdapter is never called.
I have this same code for another app but without the tabpager and there it all works perfectly...
Check whether getCurrentBadges() method not returns empty list. If so there is no data to show in ListView.
I was able to make it work by inhering ListFragment in FragmentBadges, following this tutorial https://www.youtube.com/watch?v=heR4zhj_wV0

Categories