Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to place ads below every 5 items in fragments #116

Open
tariqb69000 opened this issue Mar 16, 2018 · 0 comments
Open

how to place ads below every 5 items in fragments #116

tariqb69000 opened this issue Mar 16, 2018 · 0 comments

Comments

@tariqb69000
Copy link

I want to place admob ads below every 5 items in fragments.
I tried but i did not could. someone is here who help me
`DemoViewHolder
package baleli.urdupoetrybytariq;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**

  • Created by SONU on 31/08/15.
    */
    public abstract class DemoViewHolder extends RecyclerView.ViewHolder {

    public TextView title;
    public Button shareBtn;

    public DemoViewHolder(View view) {
    super(view);

     this.title = (TextView) view.findViewById(R.id.cardTitle);
     this.shareBtn= (Button) view.findViewById(R.id.shareBtn);
    

    }
    }`

`RecyclerView_Adapter
package baleli.urdupoetrybytariq;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**

  • Created by SONU on 10/09/15.
    */
    public class RecyclerView_Adapter extends
    RecyclerView.Adapter {
    private ArrayList arrayList;
    private Context context;

    public RecyclerView_Adapter(Context context,
    ArrayList arrayList) {
    this.context = context;
    this.arrayList = arrayList;

    }

    @OverRide
    public int getItemCount() {
    return (null != arrayList ? arrayList.size() : 0);

    }

    @OverRide
    public void onBindViewHolder(DemoViewHolder holder,
    int position) {

     final DemoViewHolder mainHolder = (DemoViewHolder) holder;
     //Setting text over textview
     mainHolder.title.setText(arrayList.get(position));
    
     //Share Button Here
     mainHolder.shareBtn.setOnClickListener(new View.OnClickListener() {
    
         @Override
         public void onClick(View view) {
    
             Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
             sharingIntent.setType("text/plain");
             String shareBody = mainHolder.title.getText(). toString ();
             sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
             sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
             context.startActivity(Intent.createChooser(sharingIntent, "Share Using"));
    
         }
     });
    

    }

    @OverRide
    public DemoViewHolder onCreateViewHolder(
    ViewGroup viewGroup, int viewType) {
    LayoutInflater mInflater = LayoutInflater.from(viewGroup.getContext());

     ViewGroup mainGroup = (ViewGroup) mInflater.inflate(
             R.layout.item_row, viewGroup, false);
    
    
    
     DemoViewHolder mainHolder = new DemoViewHolder(mainGroup) {
         @Override
         public String toString() {
             return super.toString();
         }
     };
    
    
     return mainHolder;
    

    }

}`

Fragments
package baleli.urdupoetrybytariq;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**

  • Created by SONU on 16/09/15.
    */
    public class Romantic extends Fragment {
    private View view;

    private String title;//String for tab title

    private static RecyclerView recyclerView;

    public Romantic() {
    }

    public Romantic(String title) {
    this.title = title;//Setting tab title
    }

    @nullable
    @OverRide
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.dummy_fragment, container, false);

     setRecyclerView();
     return view;
    

    }
    //Setting recycler view
    private void setRecyclerView() {

     recyclerView = (RecyclerView) view
             .findViewById(R.id.recyclerView);
     recyclerView.setHasFixedSize(true);
     recyclerView
             .setLayoutManager(new LinearLayoutManager(getActivity()));//Linear Items
    
    
     ArrayList<String> arrayList = new ArrayList<>();
     arrayList.add(" Koi Raat Mere Aashna, Mujhe Yun Bhi To Naseeb Ho\n" +
             " Na Rahe Khayal Libaas Ka, Wo Itna Mere Qareeb Ho\n" +
             " Badan Ki Garm Aanch Se Meri Aarzu Ko Aag De\n" +
             " Mera Josh Behak Uthey, Mera Haal Bhi Ajeeb Ho\n" +
             " Tere Chashni Wajood Ka Sara Ras Mein Chura Loon\n" +
             " Phir Tu Hi Mera Marz Ho, Aur Tu Hi Mera Tabeeb Ho");
     //As like your can insert data to your ArrayList.
     RecyclerView_Adapter adapter = new RecyclerView_Adapter(getActivity(), arrayList);
     recyclerView.setAdapter(adapter);// set adapter on recyclerview
    

    }
    }

`MainActivity
package baleli.urdupoetrybytariq;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private static Toolbar toolbar;
private static ViewPager viewPager;
private static TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    viewPager = (ViewPager) findViewById(R.id.viewPager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager);//setting tab over viewpager

    //SHARE BUTTON

    //Implementing tab selected listener over tablayout
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
            switch (tab.getPosition()) {
                case 0:
                    Log.e("TAG", "TAB1");
                    break;
                case 1:
                    Log.e("TAG", "TAB2");
                    break;
                case 2:
                    Log.e("TAG", "TAB3");
                    break;
                case 3:
                    Log.e("TAG", "TAB4");
                    break;
                case 4:
                    Log.e("TAG", "TAB5");
                    break;
                case 5:
                    Log.e("TAG", "TAB6");
                    break;

            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
}


//Setting View Pager
private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new Mohabbat(""), "Mohabbat Shayari");
    adapter.addFrag(new Bewafa(""), "Bewafa Shayari");
    adapter.addFrag(new Dard(""), "Dard-E Dil Shayari");
    adapter.addFrag(new Romantic(""), "Romantic Shayari");
    adapter.addFrag(new Dosti(""), "Dosti Shayari");
    adapter.addFrag(new twolines(""), "2 Lines Shayari");



    viewPager.setAdapter(adapter);
}


//View Pager fragments setting adapter class
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();//fragment arraylist
    private final List<String> mFragmentTitleList = new ArrayList<>();//title arraylist

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }


    //adding fragments and title method
    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }


}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant