gpt4 book ai didi

android - 默认展开 CustomExpandable View 的第一项

转载 作者:行者123 更新时间:2023-11-29 14:36:23 25 4
gpt4 key购买 nike

我正在尝试默认展开自定义可扩展 ListView 的第一项,但没有得到结果...

这是我的来源....

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;
}
}

接下来我可以尝试什么?

最佳答案

因为您正在扩展 ListView,所以您应该尝试这样做:

expandableLayoutListView.expandGroup(position, false);

您还可以扩展 ExpandableListView 而不是 ListView,然后使用:

expandableLayoutListView.expandGroup(position);

因为ExpandableListView有这个方法:

public boolean expandGroup(int groupPos) {
return expandGroup(groupPos, false);
}

关于android - 默认展开 CustomExpandable View 的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36031585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com