diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 40807c7..f413f19 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -32,5 +32,7 @@ + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index f1e4abb..96f0688 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,12 +2,12 @@ apply plugin: 'com.android.application' apply plugin: 'me.tatarka.retrolambda' android { - compileSdkVersion 24 - buildToolsVersion '25.0.0' + compileSdkVersion 25 + buildToolsVersion '25.0.1' defaultConfig { applicationId "com.thedeveloperworldisyours.fullrecycleview" minSdkVersion 16 - targetSdkVersion 24 + targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -54,10 +54,10 @@ dependencies { }) - compile 'com.android.support:appcompat-v7:24.2.1' - compile 'com.android.support:recyclerview-v7:24.2.1' - compile 'com.android.support:design:24.2.1' - compile 'com.android.support:support-v4:24.2.1' + compile 'com.android.support:appcompat-v7:25.0.1' + compile 'com.android.support:recyclerview-v7:25.0.1' + compile 'com.android.support:design:25.0.1' + compile 'com.android.support:support-v4:25.0.1' testCompile 'junit:junit:4.12' //swipe diff --git a/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/MainActivity.java b/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/MainActivity.java index 75179ae..17cdb90 100644 --- a/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/MainActivity.java +++ b/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/MainActivity.java @@ -16,6 +16,7 @@ import com.thedeveloperworldisyours.fullrecycleview.indexed.IndexedFragment; import com.thedeveloperworldisyours.fullrecycleview.multiple.MultipleFragment; import com.thedeveloperworldisyours.fullrecycleview.sections.SectionFragment; +import com.thedeveloperworldisyours.fullrecycleview.sectionwithline.SectionWithLineFragment; import com.thedeveloperworldisyours.fullrecycleview.single.SingleFragment; import com.thedeveloperworldisyours.fullrecycleview.snap.SnapFragment; import com.thedeveloperworldisyours.fullrecycleview.swipe.SwipeListFragment; @@ -113,6 +114,10 @@ public boolean onOptionsItemSelected(MenuItem item) { mFragment = AddFavoritesFragment.newInstance(); break; + case R.id.main_menu_section_with_line: + mFragment = SectionWithLineFragment.newInstance(); + break; + default: return super.onOptionsItemSelected(item); } diff --git a/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/sectionwithline/ElementList.java b/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/sectionwithline/ElementList.java new file mode 100644 index 0000000..523e538 --- /dev/null +++ b/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/sectionwithline/ElementList.java @@ -0,0 +1,45 @@ +package com.thedeveloperworldisyours.fullrecycleview.sectionwithline; + +/** + * Created by javierg on 08/08/2017. + */ + +public class ElementList { + + private String mName; + boolean mSection; + boolean mNextSection; + + + public ElementList(String name, boolean section, boolean nextSection) { + this.mName = name; + this.mSection = section; + this.mNextSection = nextSection; + } + + public boolean isSection() { + return mSection; + } + + public void setSection(boolean mSection) { + this.mSection = mSection; + } + + public String getName() { + return mName; + } + + public void setName(String name) { + this.mName = name; + } + + public boolean ismNextSection() { + return mNextSection; + } + + public void setmNextSection(boolean mNextSection) { + this.mNextSection = mNextSection; + } + +} + diff --git a/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/sectionwithline/SectionWithLineAdapter.java b/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/sectionwithline/SectionWithLineAdapter.java new file mode 100644 index 0000000..ff4040f --- /dev/null +++ b/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/sectionwithline/SectionWithLineAdapter.java @@ -0,0 +1,162 @@ +package com.thedeveloperworldisyours.fullrecycleview.sectionwithline; + +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.SectionIndexer; +import android.widget.TextView; + +import com.thedeveloperworldisyours.fullrecycleview.R; + +import java.util.HashMap; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; + +/** + * Created by javierg on 08/08/2017. + */ + +public class SectionWithLineAdapter extends RecyclerView + .Adapter implements SectionIndexer { + + HashMap mMapIndex; + String[] mSections; + public static final int ITEM_FINAL = 0; + public static final int ITEM = 1; + public static final int SECTION = 2; + private Context mContext; + private List mList; + private static SectionWithLineAdapter.MyClickListener sClickListener; + // Allows to remember the last item shown on screen + private int mLastPositionSection = -1; + + @Override + public Object[] getSections() { + return mSections; + } + + @Override + public int getPositionForSection(int i) { + return mMapIndex.get(mSections[i]); + } + + @Override + public int getSectionForPosition(int i) { + return 0; + } + + static class SectionHolder extends RecyclerView.ViewHolder { + @BindView(R.id.section_with_line_list_item_header_text) + TextView mTextViewSection; + + SectionHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + } + } + + static class DataObjectHolder extends RecyclerView.ViewHolder + implements View + .OnClickListener { + @BindView(R.id.section_with_line_list_item_name) + TextView mName; + + @BindView(R.id.section_with_line_list_item_view) + View mView; + + DataObjectHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + mName.setOnClickListener(this); + } + + @Override + public void onClick(View v) { + + } + } + + public void setOnItemClickListener(SectionWithLineAdapter.MyClickListener myClickListener) { + this.sClickListener = myClickListener; + } + + public SectionWithLineAdapter(Context context, List list, String[] sections, HashMap mapIndex) { + mContext = context; + mList = list; + mSections = sections; + mMapIndex = mapIndex; + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.section_with_line_list_item, parent, false); + + View viewSection = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.section_with_lien_list_item_header, parent, false); + + switch (viewType) { + case ITEM_FINAL: + case ITEM: + return new SectionWithLineAdapter.DataObjectHolder(view); + case SECTION: + return new SectionWithLineAdapter.SectionHolder(viewSection); + default: + return new SectionWithLineAdapter.SectionHolder(viewSection); + } + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + switch (holder.getItemViewType()) { + + case ITEM: + SectionWithLineAdapter.DataObjectHolder dataObjectHolder = (SectionWithLineAdapter.DataObjectHolder) holder; + dataObjectHolder.mName.setText(mList.get(position).getName()); + break; + + case ITEM_FINAL: + SectionWithLineAdapter.DataObjectHolder dataObjectHolderFinal = (SectionWithLineAdapter.DataObjectHolder) holder; + dataObjectHolderFinal.mName.setText(mList.get(position).getName()); + dataObjectHolderFinal.mView.setVisibility(View.GONE); + break; + case SECTION: + SectionWithLineAdapter.SectionHolder sectionHolder = (SectionWithLineAdapter.SectionHolder) holder; + sectionHolder.mTextViewSection.setText(mList.get(position).getName()); + mLastPositionSection = position; + break; + } + } + + @Override + public int getItemCount() { + return mList.size(); + } + + public void refreshData(List dataset) { + mList.clear(); + mList.addAll(dataset); + notifyDataSetChanged(); + } + + interface MyClickListener { + void onItemClick(int position, boolean addItem); + } + + @Override + public int getItemViewType(int position) { + if (mList.get(position).isSection()) { + return SECTION; + } else { + if (mList.get(position).ismNextSection()) { + return ITEM_FINAL; + } else { + return ITEM; + } + } + } +} diff --git a/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/sectionwithline/SectionWithLineFragment.java b/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/sectionwithline/SectionWithLineFragment.java new file mode 100644 index 0000000..dd5e9fb --- /dev/null +++ b/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/sectionwithline/SectionWithLineFragment.java @@ -0,0 +1,139 @@ +package com.thedeveloperworldisyours.fullrecycleview.sectionwithline; + +import android.os.Bundle; +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 com.thedeveloperworldisyours.fullrecycleview.R; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Set; + +import butterknife.BindView; +import butterknife.ButterKnife; + +public class SectionWithLineFragment extends Fragment { + + HashMap mMapIndex; + String[] mSections; + List fruits; + + @BindView(R.id.section_with_line_fragment_recycler_view) + RecyclerView mRecyclerView; + + SectionWithLineAdapter mAdapter; + + public SectionWithLineFragment() { + // Required empty public constructor + } + + public static SectionWithLineFragment newInstance() { + return new SectionWithLineFragment(); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View view = inflater.inflate(R.layout.section_with_line_fragment, container, false); + ButterKnife.bind(this, view); + + mRecyclerView.setScrollbarFadingEnabled(true); + mRecyclerView.setHasFixedSize(true); + + mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); + + mAdapter = new SectionWithLineAdapter(getActivity(), getDataSet(), mSections, mMapIndex); + mRecyclerView.setAdapter(mAdapter); + + return view; + } + + private ArrayList getDataSet() { + + String[] fruits = getResources().getStringArray(R.array.fruits_array); + + + List fruitList = Arrays.asList(fruits); + getListIndexed(fruitList); + + ArrayList results = new ArrayList<>(); + ElementList obj; + int section = 0; + int normal = 0; + String fruit, fruitPlus; + String ch, chPlus; + int total = fruitList.size() + mSections.length; + + for (int index = 0; index < total; index++) { + + fruit = fruitList.get(normal); + ch = fruit.substring(0, 1); + + if (index == 0 || ch.equals(mSections[section])) { + if (index != 0) { + obj = new ElementList(fruitList.get(normal-1), false, true); + results.add(index-1, obj); + } + obj = new ElementList(ch, true, false); + mMapIndex.put(ch, index); + if (section < mSections.length - 1) { + section++; + } else { + section = 0; + } + } else { + + obj = new ElementList(fruitList.get(normal), false, false); + + normal++; + } + + results.add(index, obj); + } + return results; + } + + + public void getListIndexed(List fruitList) { + + this.fruits = fruitList; + mMapIndex = new LinkedHashMap<>(); + + for (int x = 0; x < fruits.size(); x++) { + String fruit = fruits.get(x); + String ch = fruit.substring(0, 1); + ch = ch.toUpperCase(Locale.US); + + // HashMap will prevent duplicates + mMapIndex.put(ch, x); + } + + Set sectionLetters = mMapIndex.keySet(); + + // create a list from the set to sort + ArrayList sectionList = new ArrayList<>(sectionLetters); + + Collections.sort(sectionList); + + mSections = new String[sectionList.size()]; + + sectionList.toArray(mSections); + } + +} diff --git a/app/src/main/res/layout/section_with_lien_list_item_header.xml b/app/src/main/res/layout/section_with_lien_list_item_header.xml new file mode 100644 index 0000000..a9209f2 --- /dev/null +++ b/app/src/main/res/layout/section_with_lien_list_item_header.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/section_with_line_fragment.xml b/app/src/main/res/layout/section_with_line_fragment.xml new file mode 100644 index 0000000..de4150f --- /dev/null +++ b/app/src/main/res/layout/section_with_line_fragment.xml @@ -0,0 +1,12 @@ + + + + + diff --git a/app/src/main/res/layout/section_with_line_list_item.xml b/app/src/main/res/layout/section_with_line_list_item.xml new file mode 100644 index 0000000..28022c5 --- /dev/null +++ b/app/src/main/res/layout/section_with_line_list_item.xml @@ -0,0 +1,24 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml index 401759f..c70f5ca 100644 --- a/app/src/main/res/menu/main_menu.xml +++ b/app/src/main/res/menu/main_menu.xml @@ -53,6 +53,10 @@ android:id="@+id/main_menu_add_favorites" android:title="@string/main_menu_add_favorites" /> + + - FullRecycleView + FullRecyclerView List - Basic Drag and Swipe @@ -32,6 +32,7 @@ Sections Indexed Add Favorites + Section with line Deleted Ed @@ -119,4 +120,7 @@ Woodapple + + Hello blank fragment + diff --git a/title.png b/title.png index 9c49fd5..6068f1f 100644 Binary files a/title.png and b/title.png differ