1)FragmentList.java
===================
===================
package viewpager.com.viewpagerdemo; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v7.widget.GridLayoutManager; 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.List; public class FragmentList extends Fragment { private RecyclerView recyclerView; private List<Model> models; public void setList(List<Model> models) { this.models = models; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.row_view_pager, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); recyclerView=(RecyclerView)view.findViewById(R.id.recycler); GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 3); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setAdapter(new ListAdapter(getActivity(),models)); } public List<Model> getModels() { return models; } public void setModels(List<Model> models) { this.models = models; } }
2)ListAdapter.java
==================
package viewpager.com.viewpagerdemo; import android.content.Context; 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 android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * Created by surbhi on 9/8/16. */public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private Context context; private List<Model> models; public ListAdapter(final Context context, final List<Model> models) { this.context = context; this.models=models; } // Create new views (invoked by the layout manager) @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.row_list, parent, false); return new ViewHolder(view); } // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) { final ViewHolder holder = (ViewHolder) viewHolder; holder.textView.setText(models.get(position).getText()); holder.root_layout.setTag(position); if(models.get(position).isSelected()) { holder.imageView.setVisibility(View.VISIBLE); }else { holder.imageView.setVisibility(View.GONE); } holder.root_layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int pos=(Integer)v.getTag(); if(models.get(pos).isSelected()) { models.get(pos).setSelected(false); } else models.get(pos).setSelected(true); notifyDataSetChanged(); } }); } @Override public long getItemId(int position) { return position; } @Override public int getItemCount() { return models.size(); } private class ViewHolder extends RecyclerView.ViewHolder { private TextView textView; private ImageView imageView; private LinearLayout root_layout; public ViewHolder(View convertView) { super(convertView); textView = (TextView) convertView.findViewById(R.id.textView); root_layout= (LinearLayout) convertView.findViewById(R.id.root_layout); imageView = (ImageView) convertView.findViewById(R.id.imageView); } } }3)MainActivity.java
===================
package viewpager.com.viewpagerdemo; import android.content.Context; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.viewpagerindicator.CirclePageIndicator; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private CirclePageIndicator circlePageIndicator; Context context; ViewPager viewPager; List<Model> models; List<ParentModel> parentModels; private Button btnDone; private ScreenSlidePagerAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context=this; circlePageIndicator=(CirclePageIndicator)findViewById(R.id.indicator); btnDone=(Button)findViewById(R.id.btnDone); viewPager=(ViewPager) findViewById(R.id.viewpager); circlePageIndicator.setRadius(getResources().getDimension(R.dimen.indicator_radius)); circlePageIndicator.setStrokeWidth(1); circlePageIndicator.setStrokeColor(ContextCompat.getColor(context, R.color.indicator_border_color)); circlePageIndicator.setPageColor(ContextCompat.getColor(context, R.color.white)); circlePageIndicator.setFillColor(ContextCompat.getColor(context, R.color.indicator_fill_color)); setListValue(); parentModels=new ArrayList<>(); int length=models.size(); int value=length / 9; final int size; if(length%9== 0) size=value; else { size=value +1; } btnDone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { List<Model> selectedValue=new ArrayList<Model>(); for (int i=0;i<size;i++) { FragmentList fragmentList=(FragmentList) mAdapter.getItem(i); Log.d("Size", ""+fragmentList.getModels().size()); List<Model> getModels=fragmentList.getModels(); int len=getModels.size(); for (int j=0;j<len;j++) { if(getModels.get(j).isSelected()) { selectedValue.add(getModels.get(j)); } } } Log.d("selected value", ""+selectedValue.size()); } }); for (int i=0;i<size;i++) { ParentModel parentModel=new ParentModel(); parentModel.setPosition(i); List<Model> modelsValue=new ArrayList<>(); if(i==size-1) { modelsValue=models.subList(i * 9,models.size()); }else { /* if(i==0) { modelsValue = models.subList(i * 8, 9); } else {*/ modelsValue = models.subList(i*9, 9*(1+i)); // } } parentModel.setModels(modelsValue); parentModels.add(parentModel); } mAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), parentModels); viewPager.setAdapter(mAdapter); circlePageIndicator.setViewPager(viewPager); } private void setListValue() { models=new ArrayList<>(); Model model=new Model(); model.setText("1"); models.add(model); model=new Model(); model.setText("2"); models.add(model); model=new Model(); model.setText("3"); models.add(model); model=new Model(); model.setText("4"); models.add(model); model=new Model(); model.setText("5"); models.add(model); model=new Model(); model.setText("6"); models.add(model); model=new Model(); model.setText("7"); models.add(model); model=new Model(); model.setText("8"); models.add(model); model=new Model(); model.setText("9"); models.add(model); model=new Model(); model.setText("10"); models.add(model); model=new Model(); model.setText("11"); models.add(model); model=new Model(); model.setText("12"); models.add(model); model=new Model(); model.setText("13"); models.add(model); model=new Model(); model.setText("14"); models.add(model); model=new Model(); model.setText("15"); models.add(model); model=new Model(); model.setText("16"); models.add(model); model=new Model(); model.setText("17"); models.add(model); model=new Model(); model.setText("18"); models.add(model); /* model=new Model(); model.setText("19"); models.add(model); model=new Model(); model.setText("20"); models.add(model);*/ } /** * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in * sequence. */ private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { List<ParentModel> list; public ScreenSlidePagerAdapter(FragmentManager fm, List<ParentModel> list) { super(fm); this.list = list; } @Override public Fragment getItem(int position) { FragmentList fragment = new FragmentList(); fragment.setList(list.get(position).getModels()); return fragment; } @Override public int getCount() { return list.size(); } } }4)ParentModel.java
==================
package viewpager.com.viewpagerdemo; import android.graphics.PorterDuff; import java.util.List; /** * Created by on 26/6/17. */ public class ParentModel { int position; List<Model> models; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public List<Model> getModels() { return models; } public void setModels(List<Model> models) { this.models = models; } }5)Model.java
============
package viewpager.com.viewpagerdemo; /** * Created by on 26/6/17. */ public class Model { String image; String text; boolean isSelected; public boolean isSelected() { return isSelected; } public void setSelected(boolean selected) { isSelected = selected; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getText() { return text; } public void setText(String text) { this.text = text; } }1)row_view_pager<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>2)row_list.xml==============<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/root_layout" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/textView" android:textSize="15sp" android:layout_margin="20dp" android:text="dddfsdf" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:src="@drawable/check"/> </LinearLayout>3)activity_main.xml=========================<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_alignParentTop="true" android:layout_above="@+id/indicator" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.viewpagerindicator.CirclePageIndicator android:id="@+id/indicator" android:layout_height="wrap_content" android:layout_width="match_parent" android:paddingTop="5dp" android:paddingBottom="5dp" android:layout_above="@+id/btnDone" /> <Button android:id="@+id/btnDone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Done"/> </RelativeLayout>color.xml==============<?xml version="1.0" encoding="utf-8"?><resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="indicator_border_color">#333333</color> <color name="indicator_fill_color">#898989</color> <color name="white">#ffffff</color> </resources>
No comments:
Post a Comment