gpt4 book ai didi

android - 从基本适配器滚动到 ListView 的底部

转载 作者:行者123 更新时间:2023-11-30 00:12:54 25 4
gpt4 key购买 nike

我有一个 ListView,在那个 ListView 里面我有一个 ImageButton。当用户单击该 ImageButton 时,一个新项目会膨胀,但每次我添加一个新项目时,我都必须向下滚动才能看到添加的项目。我需要帮助才能使滚动条在添加后自动移动到最后一项。

请注意,我的 ImageButton 处于不同的布局(但它是 ListView 的一部分)并且相同的布局有一个 LinearLayout 作为我膨胀的父 View subview 。

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayoutOther"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/_5sdp">

<TextView
android:id="@+id/objectiveOtherCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Other Objectives"
android:layout_marginStart="@dimen/_10sdp"
android:layout_marginTop="@dimen/_10sdp"
android:textColor="@color/common_google_signin_btn_text_light_focused"
android:textSize="@dimen/_12ssp" />

<ImageButton
android:id="@+id/addOtherObjectiveTextBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/objectiveOtherCheckBox"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginTop="@dimen/_10sdp"
android:background="@drawable/round_button2"
android:src="@android:drawable/ic_input_add"/>
//parent layout
<LinearLayout
android:id="@+id/otherObjLinearLayout"
android:layout_width="wrap_content"
android:layout_marginTop="@dimen/_15sdp"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_below="@+id/objectiveOtherCheckBox"
android:layout_height="wrap_content"
android:orientation="vertical"/>

</RelativeLayout>

这是我的基本适配器中的代码,它会膨胀 subview 。在此函数中,我想在添加项目后将 ListView 滚动到底部。

ImageButton imageButton = (ImageButton) convertView.findViewById(R.id.addOtherObjectiveTextBox);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(v.getContext(), "Clicked123", Toast.LENGTH_SHORT).show();
final Dialog dialog = new Dialog(v.getContext());
dialog.setContentView(R.layout.otherobjectivedialog);

final EditText enterObjET = (EditText) dialog.findViewById(R.id.enterObjEditText);
Button closeEnterObjBtn = (Button) dialog.findViewById(R.id.closeEnterObjDialog);
Button objConfirmBtn = (Button) dialog.findViewById(R.id.enterObjConfirmBtn);

closeEnterObjBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

objConfirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final String enteredObj = enterObjET.getText().toString();
if(enteredObj.equals("")){
Toast.makeText(v.getContext(), "Please write a objective", Toast.LENGTH_LONG).show();
}else {

final View editChildView = layoutInflater.inflate(R.layout.other_objective_edittext, null);
editParentLL.addView(editChildView);

final TextView tvOO = editChildView.findViewById(R.id.otherobjTextView);
ImageView ivOOEdit = editChildView.findViewById(R.id.otherobjEdit);
ImageView ivOODelete = editChildView.findViewById(R.id.otherobjDelete);
tvOO.setText(enteredObj);
ivOODelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int posValue2 = editParentLL.indexOfChild(editChildView);
editParentLL.removeView(editChildView);
otherObjList.remove(posValue2);
}
});
ivOOEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int editPosValue = editParentLL.indexOfChild(editChildView);
final Dialog dialog2 = new Dialog(con);
dialog2.setContentView(R.layout.otherobjectivedialog);

final EditText enterObjET2 = (EditText) dialog2.findViewById(R.id.enterObjEditText);
Button closeEnterObjBtn2 = (Button) dialog2.findViewById(R.id.closeEnterObjDialog);
Button objConfirmBtn2 = (Button) dialog2.findViewById(R.id.enterObjConfirmBtn);
String otherObjectiveTextToEdit = otherObjList.get(editPosValue);
enterObjET2.setText(otherObjectiveTextToEdit);

closeEnterObjBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog2.dismiss();
}
});
objConfirmBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int editPosValue2 = editParentLL.indexOfChild(editChildView);
final String enteredObj2 = enterObjET2.getText().toString();
if(enteredObj2.equals("")){
Toast.makeText(con, "Please write a objective", Toast.LENGTH_LONG).show();
}else {
dialog2.dismiss();
tvOO.setText(enteredObj2);
otherObjList.put(editPosValue2, enteredObj2);

}
}
});
dialog2.show();
}
});
//objectiveOtherET.setText(objectiveOtherET.getText().toString()+ " \n" + enteredObj);
//Log.i("POS", "onClick: "+editParentLL.indexOfChild(editChildView));
otherObjList.put(oo, enteredObj);
oo++;
dialog.dismiss();
}
}
});

dialog.show();

/* LinearLayout linearLayoutNew = new LinearLayout(con);
linearLayoutNew.setOrientation(LinearLayout.VERTICAL);
et = new EditText(con);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setHint("Set some objective");
linearLayoutNew.addView(et);
ll.addView(linearLayoutNew);*/
}

最佳答案

通知列表更改后,您需要在 ListView 上调用滚动。

list_View.smoothScrollToPosition(list_View.getChildCount());

或者如果从非 Ui 线程调用。

list_View.post(new Runnable() {
@Override
public void run() {
list_View.smoothScrollToPosition(list_View.getChildCount());
}
});

关于android - 从基本适配器滚动到 ListView 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862924/

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