Skip to content

Commit

Permalink
feat(0.9.2): 新增表格中充满宽度(含头尾)demo
Browse files Browse the repository at this point in the history
  • Loading branch information
free46000 committed Mar 27, 2017
1 parent 392cd8b commit d7e3214
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<activity android:name=".ItemClickActivity_"/>
<activity android:name=".ChatActivity_"/>
<activity android:name=".HeadFootActivity_"/>
<activity android:name=".HeadFootGridActivity_"/>
<activity android:name=".FullSpanGridActivity_"/>
<activity android:name=".LoadMoreActivity_"/>
</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Object> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 2 additions & 2 deletions demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<string name="multi_item_title">多类型演示</string>
<string name="item_click_title">点击事件演示</string>
<string name="chat_title">聊天界面演示</string>
<string name="head_foot_title">头尾界面演示</string>
<string name="head_foot_grid_title">头尾界面演示 在表格中并充满宽度</string>
<string name="head_foot_title">头尾视图演示</string>
<string name="head_foot_grid_title">表格中充满宽度演示(含头尾视图)</string>
<string name="load_more_title">下拉刷新加载更多界面演示</string>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
* <p>
* {@link ViewHolderManager#isFullSpan()}
* {@link ViewHolderManager#getSpanSize(int)}
*
* @param footView foot view
* @see HeadFootHolderManager
Expand All @@ -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)}
* <p>
* {@link ViewHolderManager#isFullSpan()}
* {@link ViewHolderManager#getSpanSize(int)}
*
* @param headView head view
* @see HeadFootHolderManager
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,6 +104,8 @@ public void setFullSpan(boolean fullSpan) {

/**
* @return 是否填满父布局
* @see StaggeredGridLayoutManager.LayoutParams#setFullSpan
* @see GridLayoutManager#setSpanSizeLookup
*/
public boolean isFullSpan() {
return fullSpan;
Expand All @@ -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);

}

Expand Down

0 comments on commit d7e3214

Please sign in to comment.