mirror of
https://github.com/FatttSnake/OxygenToolbox.git
synced 2026-04-06 08:21:27 +08:00
Add FoldLayout
This commit is contained in:
78
app/src/main/java/com/fatapp/oxygentoolbox/MainActivity.java
Normal file
78
app/src/main/java/com/fatapp/oxygentoolbox/MainActivity.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package com.fatapp.oxygentoolbox;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.drawerlayout.widget.DrawerLayout;
|
||||
import androidx.navigation.NavController;
|
||||
import androidx.navigation.Navigation;
|
||||
import androidx.navigation.ui.AppBarConfiguration;
|
||||
import androidx.navigation.ui.NavigationUI;
|
||||
|
||||
import com.fatapp.oxygentoolbox.layout.FoldLayout;
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.google.android.material.navigation.NavigationView;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private AppBarConfiguration mAppBarConfiguration;
|
||||
private FoldLayout foldLayout;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
FloatingActionButton fab = findViewById(R.id.fab);
|
||||
fab.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
|
||||
.setAction("Action", null).show();
|
||||
}
|
||||
});
|
||||
DrawerLayout drawer = findViewById(R.id.drawer_layout);
|
||||
NavigationView navigationView = findViewById(R.id.nav_view);
|
||||
// Passing each menu ID as a set of Ids because each
|
||||
// menu should be considered as top level destinations.
|
||||
mAppBarConfiguration = new AppBarConfiguration.Builder(
|
||||
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
|
||||
.setDrawerLayout(drawer)
|
||||
.build();
|
||||
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
|
||||
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
|
||||
NavigationUI.setupWithNavController(navigationView, navController);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
getMenuInflater().inflate(R.menu.main, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSupportNavigateUp() {
|
||||
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
|
||||
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|
||||
|| super.onSupportNavigateUp();
|
||||
}
|
||||
|
||||
public void initView() {
|
||||
foldLayout = (FoldLayout) findViewById(R.id.foldLayout);
|
||||
|
||||
List<View> viewList = new ArrayList<>();
|
||||
|
||||
viewList.add(getLayoutInflater().inflate(R.layout.layout_item, null));
|
||||
|
||||
foldLayout.addItemView(viewList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package com.fatapp.oxygentoolbox.layout;
|
||||
|
||||
import android.animation.Animator;
|
||||
import android.animation.AnimatorListenerAdapter;
|
||||
import android.animation.ValueAnimator;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.drawable.AnimatedVectorDrawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.fatapp.oxygentoolbox.R;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FoldLayout extends LinearLayout implements View.OnClickListener{
|
||||
|
||||
private boolean init;
|
||||
private final int layoutId;
|
||||
private boolean isShow;
|
||||
private LinearLayout content;
|
||||
private ValueAnimator showAnimator;
|
||||
private ValueAnimator hideAnimator;
|
||||
private View defaultView;
|
||||
|
||||
private OnItemClickListener mOnItemClickListener;
|
||||
|
||||
public FoldLayout(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public FoldLayout(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public FoldLayout(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
@SuppressLint("Recycle") TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.FoldLayout,defStyleAttr,0);
|
||||
layoutId = ta.getResourceId(R.styleable.FoldLayout_layoutId,-1);
|
||||
init(context);
|
||||
}
|
||||
|
||||
private void init(Context context) {
|
||||
setOrientation(VERTICAL);
|
||||
addDefaultLayout(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Init
|
||||
*/
|
||||
private void addDefaultLayout(Context context) {
|
||||
|
||||
defaultView = LayoutInflater.from(context).inflate(layoutId, this,true);
|
||||
defaultView.setOnClickListener(this);
|
||||
content = new LinearLayout(context);
|
||||
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
/*content.setShowDividers(SHOW_DIVIDER_BEGINNING|SHOW_DIVIDER_MIDDLE);
|
||||
content.setDividerDrawable(ContextCompat.getDrawable(context,R.drawable.item_divider));*/
|
||||
content.setOrientation(VERTICAL);
|
||||
addView(content,layoutParams);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if(isShow) {
|
||||
hideItem();
|
||||
} else {
|
||||
showItem();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
initAnimation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Animation
|
||||
*/
|
||||
private void initAnimation() {
|
||||
|
||||
int contentHeight = content.getMeasuredHeight();
|
||||
if(!init) {
|
||||
showAnimator = ValueAnimator.ofInt(0, contentHeight);
|
||||
showAnimator.addUpdateListener(animation -> {
|
||||
LayoutParams layoutParams = (LayoutParams) content.getLayoutParams();
|
||||
layoutParams.height = (int) animation.getAnimatedValue();
|
||||
content.setLayoutParams(layoutParams);
|
||||
});
|
||||
showAnimator.addListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationStart(Animator animation) {
|
||||
super.onAnimationStart(animation);
|
||||
LinearLayout linearLayout = defaultView.findViewById(R.id.foldLayoutLinearLayout);
|
||||
linearLayout.setBackground(getResources().getDrawable(R.drawable.top_radius_background));
|
||||
}
|
||||
});
|
||||
|
||||
hideAnimator = ValueAnimator.ofInt(contentHeight,0);
|
||||
hideAnimator.addUpdateListener(animation -> {
|
||||
LayoutParams layoutParams = (LayoutParams) content.getLayoutParams();
|
||||
layoutParams.height = (int) animation.getAnimatedValue();
|
||||
content.setLayoutParams(layoutParams);
|
||||
});
|
||||
hideAnimator.addListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
super.onAnimationEnd(animation);
|
||||
LinearLayout linearLayout = defaultView.findViewById(R.id.foldLayoutLinearLayout);
|
||||
linearLayout.setBackground(getResources().getDrawable(R.drawable.top_bottom_radius_background));
|
||||
}
|
||||
});
|
||||
init = true;
|
||||
hide();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Item
|
||||
*/
|
||||
public void addItemView(List<View> views) {
|
||||
for (int i = 0; i < views.size(); i++) {
|
||||
final int position = i;
|
||||
content.addView(views.get(i));
|
||||
views.get(i).setOnClickListener(v -> {
|
||||
if(null != mOnItemClickListener) {
|
||||
mOnItemClickListener.onItemClick(v,position);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto hide
|
||||
*/
|
||||
public void hide() {
|
||||
LayoutParams layoutParams = (LayoutParams) content.getLayoutParams();
|
||||
layoutParams.height = 0;
|
||||
content.setLayoutParams(layoutParams);
|
||||
}
|
||||
|
||||
public void showItem() {
|
||||
isShow = true;
|
||||
showAnimator.start();
|
||||
ImageView imageView = defaultView.findViewById(R.id.arrowIcon);
|
||||
imageView.setImageDrawable(getResources().getDrawable(R.drawable.right_to_down_arrow));
|
||||
AnimatedVectorDrawable animatedVectorDrawable = (AnimatedVectorDrawable) imageView.getDrawable();
|
||||
animatedVectorDrawable.start();
|
||||
}
|
||||
|
||||
public void hideItem() {
|
||||
isShow = false;
|
||||
hideAnimator.start();
|
||||
ImageView imageView = defaultView.findViewById(R.id.arrowIcon);
|
||||
imageView.setImageDrawable(getResources().getDrawable(R.drawable.down_to_right_arrow));
|
||||
AnimatedVectorDrawable animatedVectorDrawable = (AnimatedVectorDrawable) imageView.getDrawable();
|
||||
animatedVectorDrawable.start();
|
||||
}
|
||||
|
||||
interface OnItemClickListener {
|
||||
void onItemClick(View view,int position);
|
||||
}
|
||||
|
||||
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
|
||||
this.mOnItemClickListener = onItemClickListener;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.fatapp.oxygentoolbox.ui.gallery;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.fatapp.oxygentoolbox.R;
|
||||
|
||||
public class GalleryFragment extends Fragment {
|
||||
|
||||
private GalleryViewModel galleryViewModel;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
galleryViewModel =
|
||||
new ViewModelProvider(this).get(GalleryViewModel.class);
|
||||
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
|
||||
final TextView textView = root.findViewById(R.id.text_gallery);
|
||||
galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
|
||||
@Override
|
||||
public void onChanged(@Nullable String s) {
|
||||
textView.setText(s);
|
||||
}
|
||||
});
|
||||
return root;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.fatapp.oxygentoolbox.ui.gallery;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
public class GalleryViewModel extends ViewModel {
|
||||
|
||||
private MutableLiveData<String> mText;
|
||||
|
||||
public GalleryViewModel() {
|
||||
mText = new MutableLiveData<>();
|
||||
mText.setValue("This is gallery fragment");
|
||||
}
|
||||
|
||||
public LiveData<String> getText() {
|
||||
return mText;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.fatapp.oxygentoolbox.ui.home;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.fatapp.oxygentoolbox.R;
|
||||
import com.fatapp.oxygentoolbox.layout.FoldLayout;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class HomeFragment extends Fragment {
|
||||
|
||||
View root;
|
||||
|
||||
private HomeViewModel homeViewModel;
|
||||
|
||||
private FoldLayout foldLayout;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
homeViewModel =
|
||||
new ViewModelProvider(this).get(HomeViewModel.class);
|
||||
root = inflater.inflate(R.layout.fragment_home, container, false);
|
||||
initView();
|
||||
return root;
|
||||
}
|
||||
|
||||
private void initView() {
|
||||
foldLayout = (FoldLayout) root.findViewById(R.id.foldLayout);
|
||||
|
||||
List<View> viewList = new ArrayList<>();
|
||||
|
||||
viewList.add(getLayoutInflater().inflate(R.layout.layout_item, null));
|
||||
|
||||
foldLayout.addItemView(viewList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.fatapp.oxygentoolbox.ui.home;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
public class HomeViewModel extends ViewModel {
|
||||
|
||||
private MutableLiveData<String> mText;
|
||||
|
||||
public HomeViewModel() {
|
||||
mText = new MutableLiveData<>();
|
||||
mText.setValue("This is home fragment");
|
||||
}
|
||||
|
||||
public LiveData<String> getText() {
|
||||
return mText;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.fatapp.oxygentoolbox.ui.slideshow;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.fatapp.oxygentoolbox.R;
|
||||
|
||||
public class SlideshowFragment extends Fragment {
|
||||
|
||||
private SlideshowViewModel slideshowViewModel;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
slideshowViewModel =
|
||||
new ViewModelProvider(this).get(SlideshowViewModel.class);
|
||||
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
|
||||
final TextView textView = root.findViewById(R.id.text_slideshow);
|
||||
slideshowViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
|
||||
@Override
|
||||
public void onChanged(@Nullable String s) {
|
||||
textView.setText(s);
|
||||
}
|
||||
});
|
||||
return root;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.fatapp.oxygentoolbox.ui.slideshow;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
public class SlideshowViewModel extends ViewModel {
|
||||
|
||||
private MutableLiveData<String> mText;
|
||||
|
||||
public SlideshowViewModel() {
|
||||
mText = new MutableLiveData<>();
|
||||
mText.setValue("This is slideshow fragment");
|
||||
}
|
||||
|
||||
public LiveData<String> getText() {
|
||||
return mText;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user