Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add recycler view demo #114

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ptr-demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ dependencies {
}
compile 'in.srain.cube:clog:1.0.2'
compile 'in.srain.cube:cube-sdk:1.0.44.39-SNAPSHOT@aar'
compile 'com.google.android:support-v4:r7'
// compile 'com.google.android:support-v4:r7'
compile 'com.android.support:recyclerview-v7:22.1.1'
}
27 changes: 27 additions & 0 deletions ptr-demo/res/layout/fragment_classic_header_with_recycler_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<in.srain.cube.views.ptr.PtrClassicFrameLayout
android:id="@+id/rotate_header_recycler_view_frame"
xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cube_mints_f1f1f1"
cube_ptr:ptr_duration_to_close="200"
cube_ptr:ptr_duration_to_close_header="1000"
cube_ptr:ptr_keep_header_when_refresh="true"
cube_ptr:ptr_pull_to_fresh="false"
cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2"
cube_ptr:ptr_resistance="1.7">

<android.support.v7.widget.RecyclerView
android:id="@+id/rotate_header_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="12dp"
android:paddingRight="12dp"/>

</in.srain.cube.views.ptr.PtrClassicFrameLayout>
</RelativeLayout>
1 change: 1 addition & 0 deletions ptr-demo/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<string name="ptr_demo_block_grid_view">Contains A GridView</string>
<string name="ptr_demo_block_list_view">Contains A ListView</string>
<string name="ptr_demo_block_recycler_view">Contains A RecyclerView</string>
<string name="ptr_demo_block_web_view">Contains A WebView</string>
<string name="ptr_demo_block_scroll_view">Contains ScrollView</string>
<string name="ptr_demo_block_frame_layout">Contains A FrameLayout</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import java.util.ArrayList;

import in.srain.cube.mints.base.BlockMenuFragment;
import in.srain.cube.util.LocalDisplay;
import in.srain.cube.views.ptr.PtrDefaultHandler;
Expand All @@ -19,8 +22,6 @@
import in.srain.cube.views.ptr.demo.ui.viewpager.ViewPagerActivity;
import in.srain.cube.views.ptr.header.StoreHouseHeader;

import java.util.ArrayList;

public class PtrDemoHomeFragment extends BlockMenuFragment {

@Override
Expand Down Expand Up @@ -55,6 +56,15 @@ public void onClick(View v) {
getContext().pushFragmentToBackStack(WithListView.class, null);
}
}));

itemInfos.add(newItemInfo(R.string.ptr_demo_block_recycler_view, R.color.cube_mints_4d90fe, new OnClickListener() {

@Override
public void onClick(View v) {
getContext().pushFragmentToBackStack(WithRecyclerView.class, null);
}
}));

itemInfos.add(newItemInfo(R.string.ptr_demo_block_web_view, R.color.cube_mints_4d90fe, new OnClickListener() {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
package in.srain.cube.views.ptr.demo.ui.classic;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;
import in.srain.cube.mints.base.TitleBaseFragment;
import in.srain.cube.request.JsonData;
import in.srain.cube.request.RequestFinishHandler;
import in.srain.cube.util.LocalDisplay;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.demo.R;
import in.srain.cube.views.ptr.demo.data.DemoRequestData;
import in.srain.cube.views.ptr.demo.ui.MaterialStyleFragment;
import in.srain.cube.views.ptr.header.MaterialHeader;

/**
* Created by yeungeek on 2015/6/20.
*
* @see <p>
* <a href="https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh/issues/105">issue#105</a>
* </p>
*/
public class WithRecyclerView extends TitleBaseFragment {
private ImageLoader mImageLoader;
private PtrClassicFrameLayout mPtrFrame;
private RecyclerAdapter mAdapter;

@Override
protected View createView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
setHeaderTitle(R.string.ptr_demo_block_recycler_view);
mImageLoader = ImageLoaderFactory.create(getContext());

final View contentView = layoutInflater.inflate(R.layout.fragment_classic_header_with_recycler_view, null);
final RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.rotate_header_recycler_view);
//itemClick
mAdapter = new RecyclerAdapter();
mAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
if (position >= 0) {
final String url = mAdapter.getItem(position).optString("pic");
if (!TextUtils.isEmpty(url)) {
getContext().pushFragmentToBackStack(MaterialStyleFragment.class, url);
}
}
}
});

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
// recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
recyclerView.setAdapter(mAdapter);

mPtrFrame = (PtrClassicFrameLayout) contentView.findViewById(R.id.rotate_header_recycler_view_frame);
mPtrFrame.setLastUpdateTimeRelateObject(this);

//materialHeader();//change header

mPtrFrame.setPtrHandler(new PtrHandler() {
@Override
public void onRefreshBegin(PtrFrameLayout frame) {
updateData();
}

@Override
public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
// return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header); //same work
return canDoRefresh(recyclerView);
}
});

// the following are default settings
mPtrFrame.setResistance(1.7f);
mPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f);
mPtrFrame.setDurationToClose(200);
mPtrFrame.setDurationToCloseHeader(1000);
// default is false
mPtrFrame.setPullToRefresh(false);
// default is true
mPtrFrame.setKeepHeaderWhenRefresh(true);
mPtrFrame.postDelayed(new Runnable() {
@Override
public void run() {
mPtrFrame.autoRefresh();
}
}, 100);

return contentView;
}

private void materialHeader() {
// header
final MaterialHeader header = new MaterialHeader(getContext());
int[] colors = getResources().getIntArray(R.array.google_colors);
header.setColorSchemeColors(colors);
header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
header.setPadding(0, LocalDisplay.dp2px(15), 0, LocalDisplay.dp2px(10));
header.setPtrFrameLayout(mPtrFrame);

mPtrFrame.setLoadingMinTime(1000);
mPtrFrame.setDurationToCloseHeader(1500);
mPtrFrame.setHeaderView(header);
mPtrFrame.addPtrUIHandler(header);
}

protected void updateData() {
DemoRequestData.getImageList(new RequestFinishHandler<JsonData>() {
@Override
public void onRequestFinish(final JsonData data) {
mPtrFrame.postDelayed(new Runnable() {
@Override
public void run() {
mAdapter.getDataList().clear();
mAdapter.getDataList().addAll(data.optJson("data").optJson("list").toArrayList());
mPtrFrame.refreshComplete();
mAdapter.notifyDataSetChanged();
}
}, 0);
}
});
}

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
protected ArrayList<JsonData> mItemDataList = new ArrayList();
protected OnItemClickListener onItemClickListener;

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view, onItemClickListener);
return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (position < getItemCount() && position < mItemDataList.size()) {
final ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.mImageView.loadImage(mImageLoader, mItemDataList.get(position).optString("pic"));
}
}

@Override
public int getItemCount() {
return mItemDataList.size();
}

public JsonData getItem(final int position) {
if (position < mItemDataList.size()) {
return mItemDataList.get(position);
} else {
return null;
}
}

public ArrayList<JsonData> getDataList() {
return mItemDataList;
}

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
}

private class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private OnItemClickListener mOnItemClickListener;
CubeImageView mImageView;

public ViewHolder(View itemView) {
super(itemView);
}

public ViewHolder(View itemView, OnItemClickListener onItemClickListener) {
super(itemView);
itemView.setOnClickListener(this);
this.mOnItemClickListener = onItemClickListener;
mImageView = (CubeImageView) itemView.findViewById(R.id.list_view_item_image_view);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}

@Override
public void onClick(View v) {
if (null != mOnItemClickListener) {
mOnItemClickListener.onItemClick(v, getLayoutPosition());
}
}
}

public interface OnItemClickListener {
void onItemClick(final View view, final int position);
}

//check
public boolean canDoRefresh(final RecyclerView recyclerView) {
if (recyclerView.getChildCount() == 0) {
return true;
}
int top = recyclerView.getChildAt(0).getTop();
if (top != 0) {
return false;
}
// final RecyclerView recyclerView = (RecyclerView) mRecyclerView;
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
int position = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
if (position == 0) {
return true;
} else if (position == -1) {
position = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
return position == 0;
}
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
boolean allViewAreOverScreen = true;
int[] positions = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(null);
for (int i = 0; i < positions.length; i++) {
if (positions[i] == 0) {
return true;
}
if (positions[i] != -1) {
allViewAreOverScreen = false;
}
}
if (allViewAreOverScreen) {
positions = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(null);
for (int i = 0; i < positions.length; i++) {
if (positions[i] == 0) {
return true;
}
}
}
}
return false;
}
}