diff --git a/demo/src/main/AndroidManifest.xml b/demo/src/main/AndroidManifest.xml index 11253f3..13d4c6f 100644 --- a/demo/src/main/AndroidManifest.xml +++ b/demo/src/main/AndroidManifest.xml @@ -19,7 +19,7 @@ - + diff --git a/demo/src/main/java/com/freelib/multiitem/demo/HeadFootGridActivity.java b/demo/src/main/java/com/freelib/multiitem/demo/FullSpanGridActivity.java similarity index 54% rename from demo/src/main/java/com/freelib/multiitem/demo/HeadFootGridActivity.java rename to demo/src/main/java/com/freelib/multiitem/demo/FullSpanGridActivity.java index 0b5b557..607e0f1 100644 --- a/demo/src/main/java/com/freelib/multiitem/demo/HeadFootGridActivity.java +++ b/demo/src/main/java/com/freelib/multiitem/demo/FullSpanGridActivity.java @@ -1,22 +1,18 @@ package com.freelib.multiitem.demo; import android.content.Context; -import android.os.Handler; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.TextView; import com.freelib.multiitem.adapter.BaseItemAdapter; -import com.freelib.multiitem.adapter.holder.BaseViewHolder; import com.freelib.multiitem.demo.bean.ImageBean; import com.freelib.multiitem.demo.bean.ImageTextBean; import com.freelib.multiitem.demo.bean.TextBean; +import com.freelib.multiitem.demo.viewholder.FullSpanTextViewManager; import com.freelib.multiitem.demo.viewholder.ImageAndTextManager; import com.freelib.multiitem.demo.viewholder.ImageViewManager; -import com.freelib.multiitem.demo.viewholder.LoadMoreHolderManager; -import com.freelib.multiitem.demo.viewholder.TextViewManager; -import com.freelib.multiitem.listener.OnItemClickListener; import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.EActivity; @@ -25,51 +21,47 @@ import java.util.ArrayList; import java.util.List; +/** + * 在表格中充满宽度可以时任意ViewHolderManager + * 详见{@link com.freelib.multiitem.adapter.holder.ViewHolderManager#isFullSpan} + * {@link com.freelib.multiitem.adapter.holder.ViewHolderManager#getSpanSize(int)} + */ @EActivity(R.layout.layout_recycler) -public class HeadFootGridActivity extends AppCompatActivity { +public class FullSpanGridActivity extends AppCompatActivity { @ViewById(R.id.recyclerView) protected RecyclerView recyclerView; - public static void startHeadFootGridActivity(Context context) { - HeadFootGridActivity_.intent(context).start(); + public static void startFullSpanGridActivity(Context context) { + FullSpanGridActivity_.intent(context).start(); } @AfterViews protected void initViews() { setTitle(R.string.head_foot_grid_title); - recyclerView.setLayoutManager(new LinearLayoutManager(this)); + recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); //初始化adapter BaseItemAdapter adapter = new BaseItemAdapter(); //为XXBean数据源注册XXManager管理类 - adapter.register(TextBean.class, new TextViewManager()); + adapter.register(TextBean.class, new FullSpanTextViewManager()); adapter.register(ImageTextBean.class, new ImageAndTextManager()); adapter.register(ImageBean.class, new ImageViewManager()); + //充满宽度详见ViewHolderManager#isFullSpan返回true即可,其实不一定是head或者foot TextView headView1 = new TextView(this); - headView1.setText("通过addHeadView增加的head1"); + headView1.setText("通过addHeadView增加的充满宽度的head"); adapter.addHeadView(headView1); - adapter.addHeadItem(new TextBean("通过addHeadItem增加的head2")); TextView footView1 = new TextView(this); - footView1.setText("通过addFootView增加的foot1"); + footView1.setText("通过addFootView增加充满宽度的foot1"); adapter.addFootView(footView1); - adapter.addFootItem(new TextBean("通过addFootItem增加的foot2")); recyclerView.setAdapter(adapter); List list = new ArrayList<>(); - for (int i = 0; i < 2; i++) { - list.add(new TextBean("AAA" + i)); + for (int i = 0; i < 3; i++) { + if (i==1){ + list.add(new TextBean("FullSpanTextViewManager充满宽度Item")); + } list.add(new ImageBean(R.drawable.img1)); list.add(new ImageTextBean(R.drawable.img2, "BBB" + i)); } - adapter.setOnItemClickListener(new OnItemClickListener() { - @Override - public void onItemClick(BaseViewHolder viewHolder) { - if (viewHolder.getItemPosition() == 0) { - adapter.addDataItem(new TextBean("我是后加的")); - } else { - adapter.removeDataItem(adapter.getDataList().size() - 1); - } - } - }); adapter.setDataItems(list); } } \ No newline at end of file diff --git a/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java b/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java index a9eff3b..48b83c3 100644 --- a/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java +++ b/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java @@ -54,7 +54,7 @@ public void onItemClick(BaseViewHolder viewHolder) { HeadFootActivity.startHeadFootActivity(MainActivity.this); break; case 4: - HeadFootGridActivity.startHeadFootGridActivity(MainActivity.this); + FullSpanGridActivity.startFullSpanGridActivity(MainActivity.this); break; case 5: LoadMoreActivity.startLoadMoreActivity(MainActivity.this); diff --git a/demo/src/main/java/com/freelib/multiitem/demo/viewholder/FullSpanTextViewManager.java b/demo/src/main/java/com/freelib/multiitem/demo/viewholder/FullSpanTextViewManager.java new file mode 100644 index 0000000..edf99d9 --- /dev/null +++ b/demo/src/main/java/com/freelib/multiitem/demo/viewholder/FullSpanTextViewManager.java @@ -0,0 +1,11 @@ +package com.freelib.multiitem.demo.viewholder; + +/** + * Created by free46000 on 2017/3/28. + */ +public class FullSpanTextViewManager extends TextViewManager { + @Override + public boolean isFullSpan() { + return true; + } +} diff --git a/demo/src/main/res/values/strings.xml b/demo/src/main/res/values/strings.xml index 4490013..04f755e 100644 --- a/demo/src/main/res/values/strings.xml +++ b/demo/src/main/res/values/strings.xml @@ -5,8 +5,8 @@ 多类型演示 点击事件演示 聊天界面演示 - 头尾界面演示 - 头尾界面演示 在表格中并充满宽度 + 头尾视图演示 + 表格中充满宽度演示(含头尾视图) 下拉刷新加载更多界面演示 diff --git a/library/src/main/java/com/freelib/multiitem/adapter/BaseItemAdapter.java b/library/src/main/java/com/freelib/multiitem/adapter/BaseItemAdapter.java index 861ced8..821171b 100644 --- a/library/src/main/java/com/freelib/multiitem/adapter/BaseItemAdapter.java +++ b/library/src/main/java/com/freelib/multiitem/adapter/BaseItemAdapter.java @@ -155,8 +155,9 @@ public void removeDataItem(int position, int itemCount) { /** * 添加foot View,默认为充满父布局 - * {@link GridLayoutManager#setSpanSizeLookup(GridLayoutManager.SpanSizeLookup)} - * {@link android.support.v7.widget.StaggeredGridLayoutManager.LayoutParams#setFullSpan(boolean)} + *

+ * {@link ViewHolderManager#isFullSpan()} + * {@link ViewHolderManager#getSpanSize(int)} * * @param footView foot view * @see HeadFootHolderManager @@ -180,8 +181,9 @@ public void addFootItem(Object footItem) { /** * 添加head View,默认为充满父布局 - * {@link GridLayoutManager#setSpanSizeLookup(GridLayoutManager.SpanSizeLookup)} - * {@link android.support.v7.widget.StaggeredGridLayoutManager.LayoutParams#setFullSpan(boolean)} + *

+ * {@link ViewHolderManager#isFullSpan()} + * {@link ViewHolderManager#getSpanSize(int)} * * @param headView head view * @see HeadFootHolderManager @@ -315,9 +317,7 @@ public int getItemCount() { @Override public void onViewAttachedToWindow(BaseViewHolder holder) { super.onViewAttachedToWindow(holder); - - System.out.println("onViewAttachedToWindow:::" + holder.getItemPosition() + "==" + holder.getItemData()); - +// System.out.println("onViewAttachedToWindow:::" + holder.getItemPosition() + "==" + holder.getItemData()); //当StaggeredGridLayoutManager的时候设置充满横屏 if (holder.getViewHolderManager().isFullSpan() && holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) { StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams(); @@ -328,7 +328,7 @@ public void onViewAttachedToWindow(BaseViewHolder holder) { @Override public void onAttachedToRecyclerView(final RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); - System.out.println("onAttachedToRecyclerView:::"); +// System.out.println("onAttachedToRecyclerView:::" + getItemCount()); RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); if (manager instanceof GridLayoutManager) { //GridLayoutManager时设置每行的span diff --git a/library/src/main/java/com/freelib/multiitem/adapter/holder/ViewHolderManager.java b/library/src/main/java/com/freelib/multiitem/adapter/holder/ViewHolderManager.java index f0a71bb..66498d9 100644 --- a/library/src/main/java/com/freelib/multiitem/adapter/holder/ViewHolderManager.java +++ b/library/src/main/java/com/freelib/multiitem/adapter/holder/ViewHolderManager.java @@ -2,6 +2,8 @@ import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.StaggeredGridLayoutManager; import android.support.v7.widget.RecyclerView.ViewHolder; import android.view.LayoutInflater; import android.view.View; @@ -102,6 +104,8 @@ public void setFullSpan(boolean fullSpan) { /** * @return 是否填满父布局 + * @see StaggeredGridLayoutManager.LayoutParams#setFullSpan + * @see GridLayoutManager#setSpanSizeLookup */ public boolean isFullSpan() { return fullSpan; @@ -113,9 +117,10 @@ public boolean isFullSpan() { * * @param spanCount span总数量 * @return 当前所占span大小 + * @see GridLayoutManager#setSpanSizeLookup */ public int getSpanSize(int spanCount) { - return spanSize >= 0 ? spanSize : isFullSpan() ? spanCount : 1; + return spanSize > 0 ? spanSize : (isFullSpan() ? spanCount : 1); }