Skip to content

Commit

Permalink
New Demo App
Browse files Browse the repository at this point in the history
Bugfix left & center align
Remove uneccesary code
  • Loading branch information
simonebortolin committed Dec 31, 2017
1 parent 2c187d8 commit 5110340
Show file tree
Hide file tree
Showing 28 changed files with 243 additions and 484 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ FlowLayoutManager is a layoutManager that works with Android ___RecyclerView___

## Screenshots
<img src="https://github.com/simonebortolin/FlowLayoutManager/blob/master/image_1.png" alt="" width="200px"></a>
<img src="https://github.com/simonebortolin/FlowLayoutManager/blob/master/image_2.png" alt="" width="200px"></a>
<img src="https://github.com/simonebortolin/FlowLayoutManager/blob/master/image_3.png" alt="" width="200px"></a>

## Installation

Expand All @@ -23,7 +25,7 @@ Step 1. Add it in your **root** build.gradle at the end of repositories:
Step 2. Add the dependency

dependencies {
compile 'com.github.simonebortolin:FlowLayoutManager:1.3.1'
compile 'com.github.simonebortolin:FlowLayoutManager:1.4.0'
}

Item per line limitation
Expand Down
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android {
targetSdkVersion androidTargetSdkVersion
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
Expand All @@ -24,6 +25,9 @@ android {
}

dependencies {
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-vector-drawable:26.1.0'
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile(group: 'com.android.support', name: 'design', version: supportLibVersion)
Expand Down
13 changes: 4 additions & 9 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xiaofeng.androidlibs">
package="com.xiaofeng.androidlibs">

<application
android:allowBackup="true"
Expand All @@ -11,18 +11,13 @@
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity">
</activity>
</application>

</manifest>
2 changes: 0 additions & 2 deletions app/src/main/assets/instruction.md

This file was deleted.

23 changes: 3 additions & 20 deletions app/src/main/java/com/xiaofeng/androidlibs/DemoAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,21 @@
* Adapter for Demo item
*/
public class DemoAdapter extends RecyclerView.Adapter<DemoViewHolder> {
List<String> items;
private int maxLinesPerItem;
private List<String> items;
private boolean showMeta = false;

public DemoAdapter() {
this.items = new LinkedList<>();
maxLinesPerItem = 1;
}

public DemoAdapter(int maxLinesPerItem, List<String> items) {
public DemoAdapter(List<String> items) {
this();
this.items.addAll(items);
this.maxLinesPerItem = maxLinesPerItem;
}

@Override
public DemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new DemoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.tag, parent, false)).setShowMeta(showMeta);
return new DemoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item, parent, false)).setShowMeta(showMeta);
}

@Override
Expand Down Expand Up @@ -62,18 +59,4 @@ public boolean onLongClick(View v) {
public int getItemCount() {
return items.size();
}

public void newItems(int maxLinesPerItem, List<String> newItems) {
this.maxLinesPerItem = maxLinesPerItem;
items.clear();
items.addAll(newItems);
}

public int getMaxLinesPerItem() {
return maxLinesPerItem;
}

public void setShowMeta(boolean showMeta) {
this.showMeta = showMeta;
}
}

This file was deleted.

169 changes: 67 additions & 102 deletions app/src/main/java/com/xiaofeng/androidlibs/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,118 +1,83 @@
package com.xiaofeng.androidlibs;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.xiaofeng.flowlayoutmanager.Alignment;
import com.xiaofeng.flowlayoutmanager.FlowLayoutManager;

import us.feras.mdv.MarkdownView;

public class MainActivity extends AppCompatActivity {

private static final int REQ_CODE_SETTINGS = 101;
RecyclerView recyclerView;
FlowLayoutManager flowLayoutManager;
MarkdownView markdownView;
private boolean settingChanged = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_app_bar);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init();
}

private void init() {
recyclerView = findViewById(R.id.list);
flowLayoutManager = new FlowLayoutManager().singleItemPerLine();
flowLayoutManager.setAutoMeasureEnabled(true);
recyclerView.setLayoutManager(flowLayoutManager);
recyclerView.setAdapter(new DemoAdapter(1, DemoUtil.listWords()));
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(5, 5, 5, 5);
}
});

// markdownView = (MarkdownView)findViewById(R.id.instruction_mdown);
// markdownView.loadMarkdownFile("file:///android_asset/instruction.md");
loadSettingsFromSharedPref();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
startActivityForResult(new Intent(this, SettingsActivity.class), REQ_CODE_SETTINGS);
return true;
}
return super.onOptionsItemSelected(item);
}

private void loadSettingsFromSharedPref() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String itemsPerLineString = sharedPreferences.getString(getResources().getString(R.string.pref_key_max_items_per_line), getString(R.string.pref_max_items_per_line_default));
int itemsPerLine = Integer.valueOf(itemsPerLineString);

String alignmentString = sharedPreferences.getString(getResources().getString(R.string.pref_key_alignment), getString(R.string.pref_alignment_default));

if (alignmentString.equals("0")) {
flowLayoutManager.setAlignment(Alignment.LEFT);
} else if (alignmentString.equals("2")) {
flowLayoutManager.setAlignment(Alignment.RIGHT);
} else {
flowLayoutManager.setAlignment(Alignment.CENTER);
RecyclerView recyclerView;
FlowLayoutManager flowLayoutManager;
RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(5, 5, 5, 5);
}
};

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_left:
flowLayoutManager = new FlowLayoutManager().setAlignment(Alignment.LEFT);

recyclerView.setLayoutManager(flowLayoutManager);
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.addItemDecoration(itemDecoration);

return true;
case R.id.navigation_center:
flowLayoutManager = new FlowLayoutManager().setAlignment(Alignment.CENTER);
flowLayoutManager.setAutoMeasureEnabled(true);

recyclerView.setLayoutManager(flowLayoutManager);
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.addItemDecoration(itemDecoration);

return true;
case R.id.navigation_rigth:
flowLayoutManager = new FlowLayoutManager().setAlignment(Alignment.RIGHT);
flowLayoutManager.setAutoMeasureEnabled(true);

recyclerView.setLayoutManager(flowLayoutManager);
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.addItemDecoration(itemDecoration);

return true;
}
return false;
}
// boolean showMeta = sharedPreferences.getBoolean(getString(R.string.pref_key_show_meta), false);

flowLayoutManager.maxItemsPerLine(itemsPerLine);
DemoAdapter demoAdapter = (DemoAdapter)recyclerView.getAdapter();
// demoAdapter.setShowMeta(showMeta);
String maxLinesPerItemString = sharedPreferences.getString(getString(R.string.pref_key_max_lines_per_item), getString(R.string.pref_max_lines_per_item_default));
int maxLinesPerItem = Integer.valueOf(maxLinesPerItemString);
demoAdapter.newItems(maxLinesPerItem, DemoUtil.listWords());
recyclerView.getAdapter().notifyItemRangeChanged(0, recyclerView.getAdapter().getItemCount());
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE_SETTINGS) {
settingChanged = true;
}
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
if (settingChanged) {
settingChanged = false;
recyclerView.post(new Runnable() {
@Override
public void run() {
loadSettingsFromSharedPref();
}
});
}
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);

BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

recyclerView = findViewById(R.id.list);
flowLayoutManager = new FlowLayoutManager().setAlignment(Alignment.LEFT);
flowLayoutManager.setAutoMeasureEnabled(true);
recyclerView.setLayoutManager(flowLayoutManager);
recyclerView.setAdapter(new DemoAdapter(DemoUtil.listWords()));
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.addItemDecoration(itemDecoration);


}

}
Loading

0 comments on commit 5110340

Please sign in to comment.