I'm working on material design navigation drawer with sliding tabs and I have implemented the same.
Now I want to add cardview
with recycler view
inside one of the tab fragment but don't know how to do it.
Please can someone help me with this?
Here's my Tab fragment code:-
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2 ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout_tens and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout_tens,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new CurrentFragment();
case 1 : return new FavouriteFragment();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Current";
case 1 :
return "Favourite";
}
return null;
}
}
}
And this is my fragment class in which I have added recycler view but it is giving NULL Point exception
while setting the adapter.
public class FavouriteFragment extends Fragment {
private List<Person> persons;
private RecyclerView rv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.favourite_layout,container,false);
RecyclerView rv = (RecyclerView)rootView.findViewById(R.id.rv);
final LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
initializeData();
initializeAdapter();
return rootView;
}
private void initializeData(){
persons = new ArrayList<>();
persons.add(new Person("XYZ", "jhxdjksd", R.drawable.profilepic));
persons.add(new Person("ABC", "bsxhbs", R.drawable.profilepic));
persons.add(new Person("PQR", "bsahxb", R.drawable.profilepic));
}
private void initializeAdapter(){
RVAdapter adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
}
}
MainActivity
public class Tens extends AppCompatActivity {
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tens);
/**
*Setup the DrawerLayout and NavigationView
*/
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragmentTens as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragmentTens()).commit();
/**
* Setup click events on the Navigation View Items.
*/
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass = null;
switch(menuItem.getItemId()) {
case R.id.nav_item_tracker:
Intent intent = new Intent(
Tens.this, Tracker.class);
startActivity(intent);
break;
case R.id.nav_item_clinic:
fragmentClass = ClinicFragment.class;
break;
case R.id.nav_item_about:
Intent intent0 = new Intent(
Tens.this, AboutFragment.class);
startActivity(intent0);
break;
case R.id.nav_item_settings:
Intent intent1 = new Intent(
Tens.this, SettingsFragment.class);
startActivity(intent1);
break;
case R.id.nav_item_logout:
Intent intent2 = new Intent(
Tens.this, MainActivity.class);
startActivity(intent2);
break;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new TabFragmentTens()).commit();
mDrawerLayout.closeDrawers();
return false;
}
});
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
Here's my recyclerview class:-
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
TextView personAge;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
personName = (TextView)itemView.findViewById(R.id.person_name);
personAge = (TextView)itemView.findViewById(R.id.person_age);
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
}
}
List<Person> persons;
RVAdapter(List<Person> persons){
this.persons = persons;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view_tens_fvrt, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personAge.setText(persons.get(i).age);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
@Override
public int getItemCount() {
return persons.size();
}
I want to add cardview
with recycler view
inside Favourite
fragment. I know how to implement cardview
but unable to implement it inside fragments.