bottom navigation with load fragment in all tab with us page viewer and Recycler View & Picasso
Represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation. Bottom navigation bars make it easy for users
Represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation. Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap.
preview of activity :-
Recycler View
picaso
online image load in android dependencies
HashMap adapter & View Holder
RecyclerView is flexible and efficient version of ListView. It is an container for rendering larger data set of views that can be recycled and scrolled very efficiently.RecyclerView is like traditional ListView widget, but with more flexibility to customizes and optimized to work with larger datasets,
Picasso is an image library for Android. It's created and maintained by Square, and caters to image loading and processing. It simplifies the process of displaying images from external locations. like amazon , or other online shopping site it has by default image and than after load a main image by internet connectivity .
for create simple RecyclerView also add Picasso
add manifest file to access internet permission
<uses-permission android:name="android.permission.INTERNET" />
first add a dependencies in app module
// RecyclerView
implementation 'com.android.support:recyclerview-v7:26.1.0'
// picaso
implementation 'com.squareup.picasso:picasso:2.3.2'
preview of add dependencies ;-
after add both dependencies click on sync now
now we started with create a new activity
- Main Activity.xml
- Main Activity.java
- navi_adapter.java
- recyclerviewadapter.java
- fst_fregment_layout.xml
- fst_fregment.java
- snd_fragment_layout.xml
- snd_fragment.java
- thd_fragment_layout.xml
- thd_fragment.java
1) Main Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.store.bottom_navi.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
2)Main Activity.java
package com.example.store.bottom_navi;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ViewPager viewpager;
FragmentManager fragmentManager ;
FragmentTransaction fragmentTransaction ;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener;
{
mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
FragmentTransaction transaction=null;
switch (item.getItemId()) {
case R.id.navigation_home:
viewpager.setCurrentItem(0);
/*selectedFragment=new Snd_fragment();
transaction= getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content,selectedFragment);
transaction.commit();*/
return true;
case R.id.navigation_dashboard:
viewpager.setCurrentItem(1);
/*selectedFragment = new First_fragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content,selectedFragment);
transaction.commit();*/
return true;
case R.id.navigation_notifications:
viewpager.setCurrentItem(2);
/* selectedFragment =new thd_fragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content,selectedFragment);
transaction.commit();
*/
return true;
}
return true;
}
};
//Used to select an item programmatically
//bottomNavigationView.getMenu().getItem(2).setChecked(true);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager =(ViewPager) findViewById(R.id.viewpager);
navi_adapter tb = new navi_adapter (MainActivity.this,getSupportFragmentManager());
tb.addFragment(new First_fragment(),"tab one");
tb.addFragment(new Snd_fragment(),"tab two");
tb.addFragment(new thd_fragment(),"tab three");
viewpager.setAdapter(tb);
final BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigation.setSelectedItemId(-1);
navigation.setSelected(false);
viewpager.setCurrentItem(0);
navigation.getMenu().getItem(0).setChecked(true);
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
navigation.getMenu().getItem(position).setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
/*FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content,new First_fragment());
transaction.commit();*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_top,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_setting)
{
Toast.makeText(this, "Setting Clicked", Toast.LENGTH_SHORT).show();
return true;
/* Fragment selectedFragment = new First_fragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content,selectedFragment);
transaction.commit();*/
}
else if(item.getItemId() == R.id.action_help)
{
Toast.makeText(this, "Help Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
3)navi_adapter.java
package com.example.store.bottom_navi;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by store on 07-03-2018.
*/
public class navi_adapter extends FragmentPagerAdapter {
private Context mContext;
List<Fragment> fragment_list = new ArrayList<>();
List<String> title_list = new ArrayList<>();
public navi_adapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
public void addFragment (Fragment fragment , String title)
{
fragment_list.add(fragment);
title_list.add(title);
}
@Override
public Fragment getItem(int position) {
return fragment_list.get(position);
// This determines the fragment for each tab
/* if (position == 0) {
return new fst_fregment_layout();
} else if (position == 1){
return new snd_fragment_layout();
} else {
return new fst_fregment_layout();
}*/
}
// This determines the number of tabs
@Override
public int getCount() {
return fragment_list.size();
}
// This determines the title for each tab
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return title_list.get(position);
/* switch (position) {
case 0:
return "tab1";
case 1:
return "tab2";
default:
return null;
}*/
}
}
4)recyclerviewadapter.java
package com.example.store.bottom_navi;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.HashMap;
import java.util.List;
/**
* Created by store on 23-02-2018.
*/
public class recycleviewadapter extends RecyclerView.Adapter<recycleviewadapter.MyViewHolder> {
private List<HashMap<String,Object>> employeeList;
Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tv1 ,tv2;
ImageView img1;
Button btn1;
public MyViewHolder(View view) {
super(view);
tv1 = (TextView) view.findViewById(R.id.tv1);
tv2 = (TextView) view.findViewById(R.id.tv2);
img1 = (ImageView) view.findViewById(R.id.img1);
btn1 = (Button)view.findViewById(R.id.btn1);
}
}
public recycleviewadapter(Context context , List<HashMap<String,Object>> employeeList) {
this.employeeList = employeeList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
HashMap<String,Object> movie = employeeList.get(position);
holder.tv1.setText(movie.get("name").toString());
holder.tv2.setText(movie.get("designation").toString());
//holder.img1.setImageResource(Integer.parseInt(movie.get("image").toString()));
Picasso.with(context)
.load(movie.get("image").toString())
.placeholder(R.drawable.icon)
.error(R.drawable.icon)
.into(holder.img1);
}
@Override
public int getItemCount() {
return employeeList.size();
}
}
5)fst_fregment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recylerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
6)fst_fregment.java
package com.example.store.bottom_navi;
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;
import java.util.HashMap;
import java.util.List;
/**
* Created by store on 28-02-2018.
*/
public class First_fragment extends Fragment {
String data ="";
RecyclerView recylerview;
List<HashMap<String, Object>> dataList = new ArrayList<>();
String[] ampName = new String[]{"Niranjan", "Shivam", "vaibhav"};
String[] ampdesi = new String[]{"CEO", "MD", "GE"};
String[] images = new String[]{ "http://www.freepngimg.com/download/recycle/2-2-recycle-png.png"};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.first_fragment_layout,container,false);
recylerview = (RecyclerView)rootView.findViewById(R.id.recylerview);
recylerview.setLayoutManager(new LinearLayoutManager( getActivity(),LinearLayoutManager.HORIZONTAL,false));
for (int i = 0 ; i<ampName.length ;i++){
HashMap<String, Object> map = new HashMap<>();
map.put("name", ampName[i]);
map.put("designation", ampdesi[i]);
map.put("image",images[0]);
dataList.add(map);
}
recylerview .setAdapter(new recycleviewadapter(getActivity(),dataList));
return rootView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recylerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
8)snd_fragment.java
package com.example.store.bottom_navi;
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;
import java.util.HashMap;
import java.util.List;
/**
* Created by store on 28-02-2018.
*/
public class Snd_fragment extends Fragment {
String data ="";
RecyclerView recylerview;
List<HashMap<String, Object>> dataList = new ArrayList<>();
String[] ampName = new String[]{"Niranjan", "Shivam", "vaibhav"};
String[] ampdesi = new String[]{"CEO", "MD", "GE"};
String[] images = new String[]{ "http://www.freepngimg.com/download/recycle/2-2-recycle-png.png"};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.first_fragment_layout,container,false);
recylerview = (RecyclerView)rootView.findViewById(R.id.recylerview);
recylerview.setLayoutManager(new LinearLayoutManager( getActivity(),LinearLayoutManager.VERTICAL,false));
for (int i = 0 ; i<ampName.length ;i++){
HashMap<String, Object> map = new HashMap<>();
map.put("name", ampName[i]);
map.put("designation", ampdesi[i]);
map.put("image",images[0]);
dataList.add(map);
}
recylerview .setAdapter(new recycleviewadapter(getActivity(),dataList));
return rootView;
}
}
9)thd_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
package com.example.store.bottom_navi;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by store on 28-02-2018.
*/
public class thd_fragment extends Fragment {
String data ="";
TextView tv3;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.thd_fragment_layout,container,false);
tv3 = (TextView)rootView.findViewById(R.id.tv3);
//data=getArguments().getString("name");
tv3.setText("Third Fragment");
return rootView;
}
}
No comments:
Post a Comment