Skip to content

Commit

Permalink
支持自定义图片展示列数
Browse files Browse the repository at this point in the history
  • Loading branch information
CeuiLiSA committed Dec 30, 2020
1 parent eda4d42 commit 65b14e2
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 34 deletions.
12 changes: 7 additions & 5 deletions app/src/main/java/ceui/lisa/adapters/IAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public IAdapter(List<IllustsBean> targetList, Context context) {
}

private void initImageSize() {
imageSize = (mContext.getResources().getDisplayMetrics().widthPixels) / 2;
imageSize = (mContext.getResources().getDisplayMetrics().widthPixels) / Shaft.sSettings.getLineCount();
}

@Override
Expand All @@ -65,10 +65,12 @@ public void bindData(IllustsBean target, ViewHolder<RecyIllustStaggerBinding> bi
params.width = imageSize;
params.height = target.getHeight() * imageSize / target.getWidth();

if (params.height < MIN_HEIGHT) {
params.height = MIN_HEIGHT;
} else if (params.height > MAX_HEIGHT) {
params.height = MAX_HEIGHT;
if (Shaft.sSettings.getLineCount() == 2) {
if (params.height < MIN_HEIGHT) {
params.height = MIN_HEIGHT;
} else if (params.height > MAX_HEIGHT) {
params.height = MAX_HEIGHT;
}
}
bindView.baseBind.illustImage.setLayoutParams(params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public BaseAdapter<IllustsBean, RecyIllustStaggerBinding> adapter() {
@Override
public void initRecyclerView() {
StaggeredGridLayoutManager layoutManager =
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
new StaggeredGridLayoutManager(Shaft.sSettings.getLineCount(), StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
baseBind.recyclerView.setLayoutManager(layoutManager);
baseBind.recyclerView.addItemDecoration(new SpacesItemWithHeadDecoration(DensityUtil.dp2px(8.0f)));
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/ceui/lisa/fragments/FragmentSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,47 @@ public void onClick(DialogInterface dialog, int which) {
.show();
}
});


baseBind.lineCount.setText(Shaft.sSettings.getLineCount() + "列");
baseBind.lineCountRela.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int index = 0;
if (Shaft.sSettings.getLineCount() == 3) {
index = 1;
} else if (Shaft.sSettings.getLineCount() == 4) {
index = 2;
}
String[] LINE_COUNT = new String[]{
"2列",
"3列",
"4列"
};
final int selectIndex = index;
new QMUIDialog.CheckableDialogBuilder(mActivity)
.setCheckedIndex(index)
.setSkinManager(QMUISkinManager.defaultInstance(mContext))
.addItems(LINE_COUNT, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == selectIndex) {
Common.showLog("什么也不做");
} else {
int lineCount = which + 2;
Shaft.sSettings.setLineCount(lineCount);
baseBind.lineCount.setText(String.format("%d列", lineCount));
Local.setSettings(Shaft.sSettings);
Common.showToast("重启APP生效", 2);
}
dialog.dismiss();
}
})
.show();
}
});


baseBind.colorSelectRela.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/ceui/lisa/fragments/ListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;

import ceui.lisa.R;
import ceui.lisa.activities.Shaft;
import ceui.lisa.adapters.BaseAdapter;
import ceui.lisa.core.BaseRepo;
import ceui.lisa.interfaces.FeedBack;
Expand Down Expand Up @@ -215,7 +216,7 @@ public void verticalRecyclerView() {

protected void staggerRecyclerView() {
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(
2, StaggeredGridLayoutManager.VERTICAL);
Shaft.sSettings.getLineCount(), StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.addItemDecoration(new SpacesItemDecoration(
DensityUtil.dp2px(8.0f)));
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/ceui/lisa/http/ErrorCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import ceui.lisa.models.ErrorResponse;
import ceui.lisa.models.ErrorResponse2;
import ceui.lisa.utils.Common;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/ceui/lisa/utils/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public class Settings {

private int themeIndex;

private int lineCount = 2;

public int getLineCount() {
return lineCount;
}

public void setLineCount(int lineCount) {
this.lineCount = lineCount;
}

public int getThemeIndex() {
return themeIndex;
}
Expand Down
68 changes: 55 additions & 13 deletions app/src/main/java/ceui/lisa/view/SpacesItemDecoration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import ceui.lisa.activities.Shaft;

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

private int space;
Expand All @@ -18,21 +20,61 @@ public SpacesItemDecoration(int space) {
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

outRect.bottom = space;

int position = parent.getChildAdapterPosition(view);
if (position == 0 || position == 1) {
outRect.top = space;
}

StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
if (params.getSpanIndex() % 2 != 0) {
//右边
outRect.left = space / 2;
outRect.right = space;
} else {
//左边
outRect.left = space;
outRect.right = space / 2;


if (Shaft.sSettings.getLineCount() == 2) {
if (position == 0 || position == 1) {
outRect.top = space;
}

if (params.getSpanIndex() % 2 != 0) {
//右边
outRect.left = space / 2;
outRect.right = space;
} else {
//左边
outRect.left = space;
outRect.right = space / 2;
}
} else if (Shaft.sSettings.getLineCount() == 3) {
if (position == 0 || position == 1 || position == 2) {
outRect.top = space;
}

if (params.getSpanIndex() % 3 == 0) {
//左边
outRect.left = space;
outRect.right = space / 2;
} else if(params.getSpanIndex() % 3 == 1) {
//中间
outRect.left = space / 2;
outRect.right = space / 2;
}else if(params.getSpanIndex() % 3 == 2) {
//右边
outRect.left = space / 2;
outRect.right = space;
}
} else if (Shaft.sSettings.getLineCount() == 4) {
if (position == 0 || position == 1 || position == 2 || position == 3) {
outRect.top = space;
}


if (params.getSpanIndex() % 4 == 0) {
//左边
outRect.left = space;
outRect.right = space / 2;
} else if(params.getSpanIndex() % 4 == 1 || params.getSpanIndex() % 4 == 2) {
//中间
outRect.left = space / 2;
outRect.right = space / 2;
} else if(params.getSpanIndex() % 4 == 3) {
//右边
outRect.left = space / 2;
outRect.right = space;
}
}
}
}
72 changes: 60 additions & 12 deletions app/src/main/java/ceui/lisa/view/SpacesItemWithHeadDecoration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import ceui.lisa.activities.Shaft;

public class SpacesItemWithHeadDecoration extends RecyclerView.ItemDecoration {

private int space;
Expand All @@ -21,22 +23,68 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle

int position = parent.getChildAdapterPosition(view);


StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
if (params.getSpanIndex() % 2 != 0) {
//右边
outRect.left = space / 2;
outRect.right = space;
} else {
//左边
outRect.left = space;
outRect.right = space / 2;
}

if (position == 0) {
outRect.top = 0;
outRect.right = 0;
outRect.left = 0;
}

position--;

StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();


if (Shaft.sSettings.getLineCount() == 2) {
if (position == 0 || position == 1) {
outRect.top = space;
}

if (params.getSpanIndex() % 2 != 0) {
//右边
outRect.left = space / 2;
outRect.right = space;
} else {
//左边
outRect.left = space;
outRect.right = space / 2;
}
} else if (Shaft.sSettings.getLineCount() == 3) {
if (position == 0 || position == 1 || position == 2) {
outRect.top = space;
}

if (params.getSpanIndex() % 3 == 0) {
//左边
outRect.left = space;
outRect.right = space / 2;
} else if(params.getSpanIndex() % 3 == 1) {
//中间
outRect.left = space / 2;
outRect.right = space / 2;
}else if(params.getSpanIndex() % 3 == 2) {
//右边
outRect.left = space / 2;
outRect.right = space;
}
} else if (Shaft.sSettings.getLineCount() == 4) {
if (position == 0 || position == 1 || position == 2 || position == 3) {
outRect.top = space;
}


if (params.getSpanIndex() % 4 == 0) {
//左边
outRect.left = space;
outRect.right = space / 2;
} else if(params.getSpanIndex() % 4 == 1 || params.getSpanIndex() % 4 == 2) {
//中间
outRect.left = space / 2;
outRect.right = space / 2;
} else if(params.getSpanIndex() % 4 == 3) {
//右边
outRect.left = space / 2;
outRect.right = space;
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_new_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
android:layout_width="match_parent"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_height="wrap_content"
app:contentInsetStartWithNavigation="0dp"
android:background="?attr/colorPrimary"
android:elevation="0dp"
android:fitsSystemWindows="true"
Expand Down
28 changes: 27 additions & 1 deletion app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
android:id="@+id/login_out"
style="@style/ripple_rela">


<TextView
style="@style/setting_text_left"
android:text="@string/login_out" />
Expand Down Expand Up @@ -459,6 +458,33 @@
</TextView>


</RelativeLayout>

<View style="@style/half_divider" />

<RelativeLayout
android:id="@+id/line_count_rela"
style="@style/ripple_rela">

<TextView
style="@style/setting_text_left"
android:text="@string/string_336" />

<TextView
android:id="@+id/line_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="@dimen/sixteen_dp"
android:layout_centerVertical="true"
android:text="@string/string_95"
android:textColor="?attr/colorPrimary"
android:textSize="14sp">


</TextView>


</RelativeLayout>

<View style="@style/half_divider" />
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -517,5 +517,6 @@
<string name="string_334">原图画质好,如果觉得原图加载太慢了,可以关闭</string>
<string name="about_network">网络</string>
<string name="string_335">添加收藏默认私人收藏</string>
<string name="string_336">图片纵向展示列数</string>

</resources>

0 comments on commit 65b14e2

Please sign in to comment.