Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Saturday 10 December 2016

Sectional recyclerview android

Hello friends
Today i am introduced how to make sectional recyclerview without use any external library
If you search in google sectional recyclerview then you find more sample with external lib but i am find better solution. So lets start and improve our programming performance....

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

ActvityMain.java

package com.samset.headerrecyclerviewsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;

import com.samset.headerrecyclerviewsample.adapter.SectionalAdapter;
import com.samset.headerrecyclerviewsample.model.model.CountryState;

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

public class MainActivity extends AppCompatActivity {

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

    @Override
    protected void onStart() {
        super.onStart();

        SectionalAdapter adapter = new SectionalAdapter(getData());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false);
        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(adapter);
    }

    public List<CountryState> getData() {
        List<CountryState> list = new ArrayList<>();

        list.add(new CountryState("India", null, CountryState.COUNTRY_TYPE));
        list.add(new CountryState("Allahabad", "1", CountryState.CITY_TYPE));
        list.add(new CountryState("Bharatpur", "2", CountryState.CITY_TYPE));
        list.add(new CountryState("Patna", "3", CountryState.CITY_TYPE));
        list.add(new CountryState("Mumbai", "4", CountryState.CITY_TYPE));
        list.add(new CountryState("Jaipur", "5", CountryState.CITY_TYPE));

        list.add(new CountryState("Pakistan", null, CountryState.COUNTRY_TYPE));
        list.add(new CountryState("Karachi", "6", CountryState.CITY_TYPE));
        list.add(new CountryState("Ishlamabad", "7", CountryState.CITY_TYPE));
        list.add(new CountryState("Bluechistan", "8", CountryState.CITY_TYPE));
        list.add(new CountryState("Sindhu", "9", CountryState.CITY_TYPE));
       
        list.add(new CountryState("Russiya", null, CountryState.COUNTRY_TYPE));
        list.add(new CountryState("Mascow", "10", CountryState.CITY_TYPE));
        list.add(new CountryState("Abaza", "11", CountryState.CITY_TYPE));
        list.add(new CountryState("Baley", "12", CountryState.CITY_TYPE));
        list.add(new CountryState("Barysh", "13", CountryState.CITY_TYPE));
        list.add(new CountryState("Bely", "14", CountryState.CITY_TYPE));
        list.add(new CountryState("Bobrov", "15", CountryState.CITY_TYPE));
        list.add(new CountryState("Dolinsk", "16", CountryState.CITY_TYPE));

        return list;
    }
}

SectionalAdapter.java

package com.samset.headerrecyclerviewsample.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.samset.headerrecyclerviewsample.R;
import com.samset.headerrecyclerviewsample.model.model.CountryState;

import java.util.List;

public class SectionalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public final int CITY_TYPE = 0;
    public final int COUNTRY_TYPE = 1;
    private List<CountryState> mList;

    public SectionalAdapter(List<CountryState> list) {
        this.mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;

        switch (viewType) {
            case COUNTRY_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.section, parent, false);
                return new CountryViewHolder(view);
            case CITY_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.section_child, parent, false);
                return new CityViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        CountryState object = mList.get(position);
        if (object != null) {
            switch (object.getType()) {
                case COUNTRY_TYPE:
                    ((CountryViewHolder) holder).tvCountryName.setText(object.getName());
                    break;
                case CITY_TYPE:
                    ((CityViewHolder) holder).tvCityName.setText(object.getName());
                    //((CityViewHolder) holder).id.setText(object.getDescription());
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null)
            return 0;
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mList != null) {
            CountryState object = mList.get(position);
            if (object != null) {
                return object.getType();
            }
        }
        return 0;
    }

    public static class CountryViewHolder extends RecyclerView.ViewHolder {
        private TextView tvCountryName;

        public CountryViewHolder(View itemView) {
            super(itemView);
            tvCountryName = (TextView) itemView.findViewById(R.id.titleTextView);
        }
    }

    public static class CityViewHolder extends RecyclerView.ViewHolder {
        private TextView tvCityName;
        private TextView id;

        public CityViewHolder(View itemView) {
            super(itemView);
            tvCityName = (TextView) itemView.findViewById(R.id.titleTextView);

        }
    }
}

section.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#d7666666" />

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Section"
        android:textSize="22sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#d7666666" />

</LinearLayout>

Full SourceCodeDownload

Live Sample

 







No comments:

Post a Comment