我正在尝试默认扩展自定义expandablelistview的第一项,但没有获得结果......
这是我的来源......
public class MainActivity extends Activity {
private final String[] array = {"Hello", "World", "Android", "is", "Awesome", "World", "Android", "is", "Awesome", "World", "Android", "is", "Awesome", "World", "Android", "is", "Awesome"};
ExpandableLayoutListView expandableLayoutListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.view_row, R.id.header_text, array);
expandableLayoutListView = (ExpandableLayoutListView) findViewById(R.id.listview);
expandableLayoutListView.setAdapter(arrayAdapter);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 100ms
try
{
View vi = getViewByPosition(0,expandableLayoutListView);
//ExpandableLayout.expand(vi);*/
expandableLayoutListView.setItemSelected(0,vi);
//ExpandableLayout.expand(vi);
}
catch(Exception e)
{e.printStackTrace();}
}
}, 1000);
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static View getViewByPosition(int pos, ExpandableLayoutListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
这是我的ExpandableLayoutListView
public class ExpandableLayoutListView extends ListView
{
Context ctx;
private int mItemSelected = -1 ;
private Integer position = -1;
public ExpandableLayoutListView(Context context)
{
super(context);
ctx = this.getContext();
setOnScrollListener(new OnExpandableLayoutScrollListener());
}
public ExpandableLayoutListView(Context context, AttributeSet attrs)
{
super(context, attrs);
setOnScrollListener(new OnExpandableLayoutScrollListener());
}
public ExpandableLayoutListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
setOnScrollListener(new OnExpandableLayoutScrollListener());
}
@Override
public boolean performItemClick(View view, int position, long id)
{
this.position = position;
for (int index = 0; index < getChildCount(); ++index)
{
if (index != (position - getFirstVisiblePosition()))
{
ExpandableLayoutItem currentExpandableLayout = (ExpandableLayoutItem) getChildAt(index).findViewWithTag(ExpandableLayoutItem.class.getName());
currentExpandableLayout.hide();
}
}
ExpandableLayoutItem expandableLayout = (ExpandableLayoutItem) getChildAt(position - getFirstVisiblePosition()).findViewWithTag(ExpandableLayoutItem.class.getName());
if (expandableLayout.isOpened())
expandableLayout.hide();
else
expandableLayout.show();
return super.performItemClick(view, position, id);
}
@Override
public void setOnScrollListener(OnScrollListener l)
{
if (!(l instanceof OnExpandableLayoutScrollListener))
throw new IllegalArgumentException("OnScrollListner must be an OnExpandableLayoutScrollListener");
super.setOnScrollListener(l);
}
public class OnExpandableLayoutScrollListener implements OnScrollListener
{
private int scrollState = 0;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
this.scrollState = scrollState;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
if (scrollState != SCROLL_STATE_IDLE)
{
for (int index = 0; index < getChildCount(); ++index)
{
ExpandableLayoutItem currentExpandableLayout = (ExpandableLayoutItem) getChildAt(index).findViewWithTag(ExpandableLayoutItem.class.getName());
if (currentExpandableLayout.isOpened() && index != (position - getFirstVisiblePosition()))
{
currentExpandableLayout.hideNow();
}
else if (!currentExpandableLayout.getCloseByUser() && !currentExpandableLayout.isOpened() && index == (position - getFirstVisiblePosition()))
{
currentExpandableLayout.showNow();
}
}
}
}
}
public void setItemSelected(int position,View vi){
mItemSelected=position;
getView(position,vi);
}
public void getView(int position, View convertView ){
if(mItemSelected==position){
convertView.setActivated(true);
}else{
convertView.setActivated(false);
}
}
}
使用ExpandableLayoutItem
public class ExpandableLayoutItem extends RelativeLayout
{
private Boolean isAnimationRunning = false;
private static Boolean isOpened = false;
private static Integer duration;
private FrameLayout contentLayout;
private FrameLayout headerLayout;
private Boolean closeByUser = true;
public ExpandableLayoutItem(Context context)
{
super(context);
}
public ExpandableLayoutItem(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context, attrs);
}
public ExpandableLayoutItem(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context, attrs);
}
private void init(final Context context, AttributeSet attrs)
{
final View rootView = View.inflate(context, R.layout.view_expandable, this);
headerLayout = (FrameLayout) rootView.findViewById(R.id.view_expandable_headerlayout);
final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableLayout);
final int headerID = typedArray.getResourceId(R.styleable.ExpandableLayout_el_headerLayout, -1);
final int contentID = typedArray.getResourceId(R.styleable.ExpandableLayout_el_contentLayout, -1);
contentLayout = (FrameLayout) rootView.findViewById(R.id.view_expandable_contentLayout);
if (headerID == -1 || contentID == -1)
throw new IllegalArgumentException("HeaderLayout and ContentLayout cannot be null!");
if (isInEditMode())
return;
duration = typedArray.getInt(R.styleable.ExpandableLayout_el_duration, getContext().getResources().getInteger(android.R.integer.config_shortAnimTime));
final View headerView = View.inflate(context, headerID, null);
headerView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
headerLayout.addView(headerView);
setTag(ExpandableLayoutItem.class.getName());
final View contentView = View.inflate(context, contentID, null);
contentView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
contentLayout.addView(contentView);
contentLayout.setVisibility(GONE);
headerLayout.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (isOpened() && event.getAction() == MotionEvent.ACTION_UP)
{
hide();
closeByUser = true;
}
return isOpened() && event.getAction() == MotionEvent.ACTION_DOWN;
}
});
}
public static void expand(final View v)
{
isOpened = true;
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
v.getLayoutParams().height = 0;
v.setVisibility(VISIBLE);
Animation animation = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
v.getLayoutParams().height = (interpolatedTime == 1) ? LayoutParams.WRAP_CONTENT : (int) (targetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
animation.setDuration(duration);
v.startAnimation(animation);
}
private void collapse(final View v)
{
isOpened = false;
final int initialHeight = v.getMeasuredHeight();
Animation animation = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1)
{
v.setVisibility(View.GONE);
isOpened = false;
}
else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
animation.setDuration(duration);
v.startAnimation(animation);
}
public void hideNow()
{
contentLayout.getLayoutParams().height = 0;
contentLayout.invalidate();
contentLayout.setVisibility(View.GONE);
isOpened = false;
}
public void showNow()
{
if (!this.isOpened())
{
contentLayout.setVisibility(VISIBLE);
this.isOpened = true;
contentLayout.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
contentLayout.invalidate();
}
}
public Boolean isOpened()
{
return isOpened;
}
public void show()
{
if (!isAnimationRunning)
{
expand(contentLayout);
isAnimationRunning = true;
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
isAnimationRunning = false;
}
}, duration);
}
}
public FrameLayout getHeaderLayout()
{
return headerLayout;
}
public FrameLayout getContentLayout()
{
return contentLayout;
}
public void hide()
{
if (!isAnimationRunning)
{
collapse(contentLayout);
isAnimationRunning = true;
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
isAnimationRunning = false;
}
}, duration);
}
closeByUser = false;
}
public Boolean getCloseByUser()
{
return closeByUser;
}
}
任何解决方案请帮助......?
答案 0 :(得分:2)
因为您正在扩展ListView,所以您应该尝试这样做:
expandableLayoutListView.expandGroup(position, false);
您也可以扩展ExpandableListView而不是ListView,然后使用:
expandableLayoutListView.expandGroup(position);
因为ExpandableListView具有以下方法:
public boolean expandGroup(int groupPos) {
return expandGroup(groupPos, false);
}
答案 1 :(得分:2)
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 100ms
try
{
ExpandableLayoutListView expandableLayoutListView = (ExpandableLayoutListView) findViewById(R.id.listview);
ExpandableLayoutItem currentExpandableLayout = (ExpandableLayoutItem) expandableLayoutListView.getChildAt(0)
.findViewWithTag(ExpandableLayoutItem.class.getName());
currentExpandableLayout.show();
}
catch(Exception e)
{e.printStackTrace();}
}
}, 100);
答案 2 :(得分:1)
expandableLayoutListView.expandGroup(0);