Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 4 April 2016

MultiSelectorListView

Hello friends if you want select multiple selection in listview without lose state then follow me.

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:visibility="gone">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.samset.multiselectorlistview.utils.custom_ui.BaseLayout
                android:id="@+id/baseLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start|top"
                android:minHeight="50dp"/>
        </LinearLayout>

    </ScrollView>

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:listSelector="@android:color/transparent" />

</LinearLayout>


ActivityMain.java


import android.content.Context;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.samset.multiselectorlistview.utils.custom_ui.BaseLayout;
import com.samset.multiselectorlistview.utils.custom_ui.FilterObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ListView mListView;
    private ArrayList<FilterObject> mArrFilter;
    private ScrollView mScrollViewFilter;
    private FilterAdapter mFilter_Adapter ;
    private BaseLayout mFlowLayoutFilter ;



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

        //programatically set statusbar color
       
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
        }

        mArrFilter = new ArrayList<>();

        String[] strArr = getResources().getStringArray(R.array.country);

        int lengthOfstrArr = strArr.length;

        for (int i = 0; i < lengthOfstrArr; i++) {
            FilterObject filter_object = new FilterObject();
            filter_object.mName = strArr[i];
            filter_object.mIsSelected = false;
            mArrFilter.add(filter_object);
        }

        getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setTitle(getString(R.string.app_name));

        mListView = (ListView) findViewById(R.id.listview);
        mScrollViewFilter = (ScrollView)findViewById(R.id.scrollView);
        mFlowLayoutFilter = (BaseLayout)findViewById(R.id.baseLayout);

        mFilter_Adapter = new FilterAdapter(mArrFilter);
        mListView.setAdapter(mFilter_Adapter);


    }


    public class FilterAdapter extends BaseAdapter {
        ArrayList<FilterObject> arrMenu;

        public FilterAdapter(ArrayList<FilterObject> arrOptions) {
            this.arrMenu = arrOptions;
        }

        public void updateListView(ArrayList<FilterObject> mArray) {
            this.arrMenu = mArray;
            notifyDataSetChanged();
        }

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

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
                viewHolder = new ViewHolder();
                viewHolder.mTtvName = (TextView) convertView.findViewById(R.id.tvName);
                viewHolder.mTvSelected = (TextView) convertView.findViewById(R.id.tvSelected);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            final FilterObject mService_Object = arrMenu.get(position);
            viewHolder.mTtvName.setText(mService_Object.mName);

            if (mService_Object.mIsSelected) {
                viewHolder.mTvSelected.setVisibility(View.VISIBLE);
            } else {
                viewHolder.mTvSelected.setVisibility(View.INVISIBLE);
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mService_Object.mIsSelected = !mService_Object.mIsSelected;
                    mScrollViewFilter.setVisibility(View.VISIBLE);

                    addFilterTag();
                    notifyDataSetChanged();
                }
            });
            return convertView;
        }

        public class ViewHolder {
            TextView mTtvName, mTvSelected;

        }
    }

    public void addFilterTag() {
        final ArrayList<FilterObject> arrFilterSelected = new ArrayList<>();

        mFlowLayoutFilter.removeAllViews();

        int length = mArrFilter.size();
        boolean isSelected = false;
        for (int i = 0; i < length; i++) {
            FilterObject fil = mArrFilter.get(i);
            if (fil.mIsSelected) {
                isSelected = true;
                arrFilterSelected.add(fil);
            }
        }
        if (isSelected) {
            mScrollViewFilter.setVisibility(View.VISIBLE);
        } else {
            mScrollViewFilter.setVisibility(View.GONE);
        }
        int size = arrFilterSelected.size();
        LayoutInflater layoutInflater = (LayoutInflater)
                this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        for (int i = 0; i < size; i++) {
            View view = layoutInflater.inflate(R.layout.tag_edit, null);

            TextView tv = (TextView) view.findViewById(R.id.tvTag);
            LinearLayout linClose = (LinearLayout) view.findViewById(R.id.linClose);
            final FilterObject filter_object = arrFilterSelected.get(i);
            linClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //showToast(filter_object.name);


                   
int innerSize = mArrFilter.size();
                    for (int j = 0; j < innerSize; j++) {
                        FilterObject mFilter_Object = mArrFilter.get(j);
                        if (mFilter_Object.mName.equalsIgnoreCase(filter_object.mName)) {
                            mFilter_Object.mIsSelected = false;

                        }
                    }
                    addFilterTag();
                    mFilter_Adapter.updateListView(mArrFilter);
                }
            });


            tv.setText(filter_object.mName);
            int color = getResources().getColor(R.color.colorAccent);

            View newView = view;
            newView.setBackgroundColor(color);

            BaseLayout.LayoutParams params = new BaseLayout.LayoutParams(BaseLayout.LayoutParams.WRAP_CONTENT, BaseLayout.LayoutParams.WRAP_CONTENT);
            params.rightMargin = 10;
            params.topMargin = 5;
            params.leftMargin = 10;
            params.bottomMargin = 5;

            newView.setLayoutParams(params);

            mFlowLayoutFilter.addView(newView);
        }
    }
}

FilterObject.java


public class FilterObject {
    public String mName ;
    public boolean mIsSelected ;
}


style.xml


<resources>

    <!-- Base application theme. -->
   
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->

       
<item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

    </style>

    <declare-styleable name="BaseLayout">
        <attr name="android:gravity" />
    </declare-styleable>
    <declare-styleable name="BaseLayout_Layout">
        <attr name="android:layout_gravity" />
    </declare-styleable>


</resources>

string.xml


<string-array name="country">
    <item >India</item>
    <item>United States</item>
    <item>Canada</item>
    <item>Australia</item>
    <item>United Kingdom</item>
    <item>Philippines</item>
    <item >Japan</item>
    <item>Italy</item>
    <item>Germany</item>
    <item>Russia</item>
    <item>Malaysia</item>
    <item>France</item>
    <item>Sweden</item>
    <item>New Zealand</item>
    <item>Singapore</item>
    <item>BanglaDesh</item>
    <item>Pakistan</item>
    <item>Bhutan</item>
    <item>Nepal</item>
    <item>Tibbat</item>
    <item>Shri Lanka</item>
    <item>Ukrain</item>
    <item>Iran</item>
    <item>Iraq</item>
    <item>Tajakistan</item>
    <item>Afganistan</item>
    <item>Antartika</item>
    <item>Myanmar</item>
    <item>Viyatnam</item>
    <item>Taivaan</item>
    <item>South Koria</item>
    <item>North Koria</item>
    <item>Dubai</item>
    <item>China</item>
    <item>Morissus</item>
    <item>Kajakhstan</item>
    <item>Tirkmanistan</item>
    <item>Kyrgystan</item>
    <item>Seychelles</item>
    <item>Austriya</item>
    <item>Sudan</item>
    <item>Naijeria</item>
    <item>AAA</item>
    <item>ABC </item>
    <item>DEF</item>
    <item>DDD</item>
    <item>VVV</item>
    <item>FFF </item>
    <item>GGG</item>
    <item>RRR</item>
    <item>GHI</item>
    <item>JKL</item>
</string-array>



Thank you 
I hope this blog is helps you.

Full Source code
Download hereLive Sample