Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
louisgeek committed May 27, 2017
1 parent 5dd02dd commit 70dc246
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 35 deletions.
3 changes: 0 additions & 3 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions app/src/main/java/com/classichu/classicadapter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -15,15 +17,14 @@
import com.classichu.adapter.recyclerview.ClassicRVHeaderFooterAdapter;
import com.classichu.adapter.recyclerview.ClassicRecyclerViewAdapter;
import com.classichu.adapter.widget.ClassicEmptyView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private List<String> mDatas = new ArrayList<>();

RecyclerView id_recycler_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -43,10 +44,21 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
});
id_list_view.setVisibility(View.GONE);

Button id_tb = (Button) findViewById(R.id.id_tb);
id_tb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (id_recycler_view.getLayoutManager() instanceof LinearLayoutManager){
id_recycler_view.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
}else{
id_recycler_view.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}
}
});

RecyclerView id_recycler_view = (RecyclerView) findViewById(R.id.id_recycler_view);
id_recycler_view = (RecyclerView) findViewById(R.id.id_recycler_view);
id_recycler_view.setLayoutManager(new LinearLayoutManager(this));
id_recycler_view.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
// id_recycler_view.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(mDatas, R.layout.item_classic_list);
recyclerViewAdapter.setOnItemClickListener(new ClassicRecyclerViewAdapter.OnItemClickListener() {
@Override
Expand Down Expand Up @@ -87,7 +99,7 @@ public void onClickEmptyView(View view) {
//id_recycler_view.setVisibility(View.GONE);
///// mDatas.clear();
RecyclerViewRVHFAdapter recyclerViewRVHFAdapter =
new RecyclerViewRVHFAdapter(mDatas,R.layout.item_classic_list);
new RecyclerViewRVHFAdapter(this,mDatas,R.layout.item_classic_list);
recyclerViewRVHFAdapter.setEmptyView(classicEmptyView);
recyclerViewRVHFAdapter.setOnItemClickListener(new ClassicRVHeaderFooterAdapter.OnItemClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.classichu.classicadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

Expand All @@ -14,8 +15,9 @@

public class RecyclerViewRVHFAdapter extends ClassicRVHeaderFooterAdapter<String> {

public RecyclerViewRVHFAdapter(List<String> mDataList, int mItemLayoutId) {
super(mDataList, mItemLayoutId);

public RecyclerViewRVHFAdapter(Context mContext, List<String> mDataList, int mItemLayoutId) {
super(mContext, mDataList, mItemLayoutId);
}

@Override
Expand All @@ -35,7 +37,7 @@ public int getItemViewType(int realPosition) {

@Override
public void findBindView(int realPosition, ClassicRVHeaderFooterViewHolder classicRVHeaderFooterViewHolder) {
TextView id_tv_title = classicRVHeaderFooterViewHolder.findBindView(R.id.id_tv_title);
TextView id_tv_title = classicRVHeaderFooterViewHolder.findBindItemView(R.id.id_tv_title);
id_tv_title.setText(mDataList.get(realPosition) + "");
}

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
android:visibility="gone"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/id_tb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="213123123"
/>

<android.support.v7.widget.RecyclerView
android:id="@+id/id_recycler_view"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.util.SparseArray;
import android.util.TypedValue;
Expand All @@ -17,6 +18,7 @@
import com.classichu.adapter.listener.OnNotFastClickListener;
import com.classichu.adapter.widget.ClassicEmptyView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -50,36 +52,45 @@ public ClassicRVHeaderFooterAdapter(Context mContext, List<D> mDataList, int mIt
this.mContext = mContext;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
//为GridLayoutManager 合并头布局的跨度
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
/**
* 抽象方法 返回当前index位置的item所占用的跨度的数量
* ##单元格合并 就是相当于占据了设定列spanCount的数量
* ##不合并 就是相当于占据了原来1个跨度
*
* @param position
* @return
*/
@Override
public int getSpanSize(int position) {
int spanSize=1;
private void configLayoutManagerSpanInfo(RecyclerView.ViewHolder viewHolder, int position) {
if (viewHolder != null && viewHolder.itemView != null && viewHolder.itemView.getLayoutParams() != null) {
ViewGroup.LayoutParams vglp = viewHolder.itemView.getLayoutParams();
if (vglp instanceof GridLayoutManager.LayoutParams) {
GridLayoutManager.LayoutParams sglm_lp = (GridLayoutManager.LayoutParams) vglp;
int spanCout=sglm_lp.getSpanSize();
int spanSize = 1;
if (mHeaderViews.size() > 0 && position < mHeaderViews.size()) {
spanSize = spanCout;
} else if (mFooterViews.size() > 0 && position >= getFooterFirstPosition()) {
spanSize = spanCout;
}
//反射
setFieldValue(sglm_lp,"mSpanSize",spanSize);
}
}else if (viewHolder != null && viewHolder.itemView != null && viewHolder.itemView.getLayoutParams() != null) {
//处理 StaggeredGridLayoutManager
ViewGroup.LayoutParams vglp = viewHolder.itemView.getLayoutParams();
if (vglp instanceof StaggeredGridLayoutManager.LayoutParams) {
StaggeredGridLayoutManager.LayoutParams sglm_lp = (StaggeredGridLayoutManager.LayoutParams) vglp;
if (mHeaderViews.size() > 0 && position < mHeaderViews.size()) {
spanSize=gridLayoutManager.getSpanCount();
sglm_lp.setFullSpan(true);
} else if (mFooterViews.size() > 0 && position >= getFooterFirstPosition()) {
spanSize=gridLayoutManager.getSpanCount();
sglm_lp.setFullSpan(true);
}
return spanSize;
}
});
}
}
private <E> void setFieldValue(E eObj, String fieldName, Object value) {
try {
Field field = eObj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(eObj, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Log.d(TAG, "onCreateViewHolder: viewType:" + viewType);
Expand All @@ -104,6 +115,7 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
configLayoutManagerSpanInfo(holder, position);
//Log.d(TAG, "onBindViewHolder: position:" + position);
int itemType = this.getItemViewType(position);
// header 类型
Expand Down

0 comments on commit 70dc246

Please sign in to comment.