Skip to content

Commit

Permalink
update StaggeredGridLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomolong committed Jul 24, 2015
1 parent e615785 commit 0be0f4d
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 52 deletions.
6 changes: 6 additions & 0 deletions .idea/encodings.xml

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

18 changes: 1 addition & 17 deletions .idea/misc.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@


public class MainActivity extends AppCompatActivity {
PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
int mCount = 1;
RecyclerViewAdapter mRecyclerViewAdapter;
private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
private int mCount = 1;
private RecyclerViewAdapter mRecyclerViewAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -25,14 +25,15 @@ protected void onCreate(Bundle savedInstanceState) {
mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) findViewById(R.id.pullLoadMoreRecyclerView);
mPullLoadMoreRecyclerView.setRefreshing(true);
new DataAsyncTask().execute();
mPullLoadMoreRecyclerView.setLinearLayout();
mPullLoadMoreRecyclerView.setPullLoadMoreListener(new PullLoadMoreListener());

}

private List<String> setList() {
List<String> dataList = new ArrayList<>();
int start = 30 * (mCount - 1);
for (int i = start; i < 30 * mCount; i++) {
int start = 20 * (mCount - 1);
for (int i = start; i < 20 * mCount; i++) {
dataList.add("测试数据" + i);
}
return dataList;
Expand All @@ -54,7 +55,7 @@ protected List<String> doInBackground(Void... params) {
protected void onPostExecute(List<String> strings) {
super.onPostExecute(strings);
if (mRecyclerViewAdapter == null) {
mRecyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, strings);
mRecyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, mPullLoadMoreRecyclerView, strings);
mPullLoadMoreRecyclerView.setAdapter(mRecyclerViewAdapter);
} else {
mRecyclerViewAdapter.getDataList().addAll(strings);
Expand All @@ -68,6 +69,7 @@ class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListe
@Override
public void onRefresh() {
setRefresh();
new DataAsyncTask().execute();
}

@Override
Expand All @@ -78,9 +80,11 @@ public void onLoadMore() {
}

private void setRefresh() {
mRecyclerViewAdapter = null;

mRecyclerViewAdapter.getDataList().clear();

mCount = 1;
new DataAsyncTask().execute();

}

@Override
Expand All @@ -101,19 +105,22 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (id == R.id.action_linearlayout) {
mPullLoadMoreRecyclerView.setLinearLayout();
setRefresh();
return true;
mRecyclerViewAdapter = null;
}
if (id == R.id.action_gridlayout) {
mPullLoadMoreRecyclerView.setGridLayout();
setRefresh();
return true;
mRecyclerViewAdapter = null;

}
if (id == R.id.action_staggeredgridlayout) {
mPullLoadMoreRecyclerView.setStaggeredGridLayout();
setRefresh();
}
// if (id == R.id.action_staggeredgridlayout) {
// mPullLoadMoreRecyclerView.setStaggeredGridLayout();
// setRefresh();
// return true;
// }

return super.onOptionsItemSelected(item);
mPullLoadMoreRecyclerView.setRefreshing(true);

new DataAsyncTask().execute();
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.wuxiaolong.pullloadmorerecyclerviewsample;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.List;
import java.util.Random;

/**
* Created by WuXiaolong on 2015/7/2.
Expand All @@ -18,14 +20,16 @@ public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapte

private Context mContext;
private List<String> dataList;
private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;

public List<String> getDataList() {
return dataList;
}

public RecyclerViewAdapter(Context context, List<String> dataList) {
public RecyclerViewAdapter(Context context, PullLoadMoreRecyclerView pullLoadMoreRecyclerView, List<String> dataList) {
this.dataList = dataList;
mContext = context;
mPullLoadMoreRecyclerView = pullLoadMoreRecyclerView;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
Expand All @@ -39,19 +43,33 @@ public ViewHolder(View itemView) {

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item, parent, false);
View v;
if (mPullLoadMoreRecyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.staggered_recycler_view_item, parent, false);
} else {

v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item, parent, false);
}
return new ViewHolder(v);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.title.setText(dataList.get(position));
// holder.title.setHeight(200+new Random().nextInt(500));
if (mPullLoadMoreRecyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
holder.title.setHeight(200 + new Random().nextInt(500));
}
}

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

public int getViewHeight(View view) {
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(w, h);
return view.getMeasuredHeight();
}
}
12 changes: 5 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView
android:id="@+id/pullLoadMoreRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"

/>

</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/layout/recycler_view_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="18sp" />
</android.support.v7.widget.CardView>
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/res/layout/staggered_recycler_view_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="18sp" />
</android.support.v7.widget.CardView>

</LinearLayout>
10 changes: 5 additions & 5 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
android:orderInCategory="200"
android:title="@string/gridlayout"
app:showAsAction="never" />
<!--<item-->
<!--android:id="@+id/action_staggeredgridlayout"-->
<!--android:orderInCategory="300"-->
<!--android:title="@string/staggeredgridlayout"-->
<!--app:showAsAction="never" />-->
<item
android:id="@+id/action_staggeredgridlayout"
android:orderInCategory="300"
android:title="@string/staggeredgridlayout"
app:showAsAction="never" />
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void initView(Context context) {
mRecyclerView.setVerticalScrollBarEnabled(true);

mRecyclerView.setHasFixedSize(true);
setLinearLayout();
// setLinearLayout();
// 设置Item增加、移除动画
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//添加分割线
Expand Down Expand Up @@ -95,6 +95,7 @@ public void setGridLayout() {
mRecyclerView.setLayoutManager(gridLayoutManager);
}


/**
* 交错网格布局管理器
*/
Expand Down Expand Up @@ -167,6 +168,7 @@ public void scrollToTop() {
mRecyclerView.scrollToPosition(0);
}


public void setAdapter(RecyclerView.Adapter adapter) {
if (adapter != null) {
mRecyclerView.setAdapter(adapter);
Expand Down

0 comments on commit 0be0f4d

Please sign in to comment.