我是编程新手,而且我正在尝试使用Android Studio制作应用程序。所以,我想要的是这样的东西
我想要的输出:
这是我的代码:
list_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:id="@+id/iconimage"/>
<TextView
android:layout_width="match_parent"
android:layout_height="55dp"
android:padding="16dp"
android:textColor="#000000"
android:textSize="12sp"
android:id="@+id/submenu"
android:gravity="center_vertical" />
</LinearLayout>
list_submenu.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textColor="#000000"
android:layout_marginLeft="44dp"
android:textSize="12sp"
android:id="@+id/submenu"/>
</LinearLayout>
MainActivity.java
package com.example.mokui.hopeful;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
View view_Group;
private DrawerLayout mDrawerLayout;
ExpandableListAdapter mMenuAdapter;
ExpandableListView expandableList;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
// icons
static int[] icon = {R.drawable.home, R.drawable.write};
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
expandableList.setIndicatorBounds(expandableList.getRight()- 80, expandableList.getWidth());
} else {
expandableList.setIndicatorBoundsRelative(expandableList.getRight()- 80, expandableList.getWidth());
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
expandableList = (ExpandableListView) findViewById(R.id.navigationmenu);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
/* ------------------------------EXPAND EXTENSION --------------------------------- */
if (navigationView != null) {
setupDrawerContent(navigationView);
}
prepareListData();
mMenuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expandableList.setAdapter(mMenuAdapter);
expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView,
View view,
int groupPosition,
int childPosition, long id) {
//Log.d("DEBUG", "submenu item clicked");
Toast.makeText(MainActivity.this,
"Header: "+String.valueOf(groupPosition) +
"\nItem: "+ String.valueOf(childPosition), Toast.LENGTH_SHORT)
.show();
view.setSelected(true);
if (view_Group != null) {
view_Group.setBackgroundColor(Color.parseColor("#ffffff"));
}
view_Group = view;
view_Group.setBackgroundColor(Color.parseColor("#DDDDDD"));
mDrawerLayout.closeDrawers();
return false;
}
});
expandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
//Log.d("DEBUG", "heading clicked");
return false;
}
});
/* ------------------------------EXPAND EXTENSION --------------------------------- */
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding data header
listDataHeader.add("Main Map");
listDataHeader.add("Create Post");
listDataHeader.add("Statistics");
listDataHeader.add("");
listDataHeader.add("Edit Account");
listDataHeader.add("Logout");
// listDataHeader.add("menu3");
// Adding child data
List<String> heading1 = new ArrayList<String>();
List<String> heading2 = new ArrayList<String>();
List<String> heading3 = new ArrayList<String>();
heading3.add("HeatMap and Graphs");
List<String> heading4 = new ArrayList<String>();
List<String> heading5 = new ArrayList<String>();
List<String> heading6 = new ArrayList<String>();
listDataChild.put(listDataHeader.get(0), heading1);// Header, Child data
listDataChild.put(listDataHeader.get(1), heading2);
listDataChild.put(listDataHeader.get(2), heading3);
listDataChild.put(listDataHeader.get(3), heading4);
listDataChild.put(listDataHeader.get(4), heading5);
listDataChild.put(listDataHeader.get(5), heading6);
} /* ---------------------------------------- */
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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 onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
/*
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
} */
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
ExpandableListAdapter.xml
package com.example.mokui.hopeful;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mListDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> mListDataChild;
ExpandableListView expandList;
public ExpandableListAdapter(Context context,
List<String> listDataHeader,
HashMap<String,
List<String>> listChildData
// ,ExpandableListView mView
)
{
this.mContext = context;
this.mListDataHeader = listDataHeader;
this.mListDataChild = listChildData;
//this.expandList = mView;
}
@Override
public int getGroupCount() {
int i = mListDataHeader.size();
//Log.d("GROUPCOUNT", String.valueOf(i));
return i;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.mListDataChild.get(
this.mListDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
//Log.d("CHILD", mListDataChild.get(this.mListDataHeader.get(groupPosition))
// .get(childPosition).toString());
return this.mListDataChild.get(
this.mListDataHeader.get(groupPosition))
.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_header, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.submenu);
ImageView headerIcon = (ImageView) convertView.findViewById(R.id.iconimage);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
//lblListHeader.setText(headerTitle.getIconName());
// headerIcon.setImageResource(MainActivity.icon[groupPosition]);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_submenu, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.submenu);
txtListChild.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
但到目前为止,这就是我得到的:我当前的输出结果:
我只想让统计选项成为唯一崩溃的选项。但事实证明,一切都是可以折叠的。
答案 0 :(得分:0)
希望这会对你有帮助......
您可以使用Linearlayout并为该线性布局提供动画。
要扩展该部分,您只需在Statistics选项单击侦听器中调用此方法:
llHidden.measure(MeasureSpec.EXACTLY, MeasureSpec.EXACTLY); // llHidden is the layout which will be GONE as visibility initially, later it will be visible inside Statistics Option.
expand1(llHidden, llHidden.getMeasuredHeight());
要折叠该部分,您只需在统计选项单击侦听器中调用此方法:
collapse1(llHidden);
/**
* To expand any view with smooth animation
*/
@SuppressLint("NewApi")
public static void expand1(final View v, final int height) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) {
v.measure(LayoutParams.MATCH_PARENT, height);
final int targtetHeight = height;
v.getLayoutParams().height = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1 ? height
: (int) (targtetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return false;
}
};
a.setDuration((int) (targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
} else {
v.setVisibility(View.VISIBLE);
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(widthSpec, heightSpec);
ValueAnimator mAnimator = slideAnimator(0, v.getMeasuredHeight(), v);
mAnimator.start();
}
}
/**
* To Collapse any view with smooth animation
*/
@SuppressLint("NewApi")
public static void collapse1(final View v) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) {
v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return false;
}
};
a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
} else {
int finalHeight = v.getHeight();
ValueAnimator mAnimator = slideAnimator(finalHeight, 0, v);
mAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
v.setVisibility(View.GONE);
}
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
mAnimator.start();
}
}