单击最后一个显示列表项的编辑文本时滚动列表视图

时间:2014-09-03 09:35:07

标签: android listview android-listview

我是用户编辑底部标签内的列表项目中的文本

我想在用户点击最后一个显示编辑文本项

时滚动列表视图

我用过

 android:windowSoftInputMode="adjustPan|adjustResize"
 android:configChanges="keyboardHidden|screenSize"
在我的listview活动中但没有效果

这是我的listview适配器类

class CreateAdapter extends ArrayAdapter<String>  {
String[] strItecode=null;
String[] strItem;
String[] strQuantity;
Context context;

int temp;
CreateAdapter(Context context, String[] strItemcode, String[] strItem,
        String[] strQauntity) {
    super(context, R.layout.create_list_item, R.id.txtItemcode, strItemcode);
    this.context = context;
    this.strItecode = strItemcode;
    this.strItem = strItem;
    this.strQuantity = strQauntity;
   // text= new String[strItem.length];
}
private int editingPosition = 0;
private TextWatcher watcher = new TextWatcher() {
          public void onTextChanged(CharSequence s, int start, int before, int count) {
              text[editingPosition] = s.toString();
          }
          public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
          public void afterTextChanged(Editable s) { }
      };

public View getView( final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    temp=position;
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {

        holder = new ViewHolder();
        convertView = mInflater
                .inflate(R.layout.create_list_item, parent, false);

        holder.txtItecode = (TextView) convertView
                .findViewById(R.id.txtItemcode);
        holder.txtItem = (TextView) convertView
                .findViewById(R.id.txtItem);
        holder.editQuantity = (EditText) convertView
                .findViewById(R.id.editcreateQuantity);
        holder.editQuantity.setTag(position);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.editQuantity.removeTextChangedListener(watcher);
   if(text[temp].contentEquals("0"))
       holder.editQuantity.setText("");
   else
    holder.editQuantity.setText(text[temp]);

    holder.editQuantity.setOnFocusChangeListener(new OnFocusChangeListener() {       
        public void onFocusChange(View v, boolean hasFocus) {
          if(hasFocus) editingPosition = position;
        }
    });

    holder.editQuantity.addTextChangedListener(watcher);

    // The rest is good, just make sure to remove the call to setting the EditText's text at the end
    holder.txtItecode.setText(strItecode[position]);
    holder.txtItem.setText(strItem[position]);
  //  holder.editQuantity.setText(text[temp]);

    return convertView;


}


class ViewHolder {
    TextView txtItecode;
    TextView txtItem;
    EditText editQuantity;
}

这里是我的列表视图

  <LinearLayout 
        android:id="@+id/listlayout"
        android:layout_width="fill_parent"

        android:layout_height="wrap_content" 
         android:layout_below="@+id/header"
          android:layout_above="@+id/lastbutton">

        <ListView

            android:id="@+id/createlist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"
            android:clickable="true"
            android:divider="#eeeeee"
            android:dividerHeight="1dip"
            android:scrollingCache="true"
            android:smoothScrollbar="true"

           android:transcriptMode="normal"
            android:descendantFocusability="beforeDescendants" >

        </ListView>

    </LinearLayout>

请建议我如何在点击列表视图的最后编辑文本时设置自动滚动

提前致谢

3 个答案:

答案 0 :(得分:1)

EditText很难用作ListView的项目。我建议你使用Button。点击此按钮,打开AlertDialog并在旁边使用EditText,输入数量值按确定并保存该值。例如,在您的适配器类中,您可以像下面的通用示例一样编写代码。

btnQuantity.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

                showAlertDialog();

}

});

private void showAlertDialog() 
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        EditText inputQuantity = new EditText(context);

        builder.setTitle("Enter Quantity");

        builder.setView(inputQuantity);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                // do your stuff or save entered quantity into any variable or whatever you want to do

            }
        });

        dialog.show();
    }

答案 1 :(得分:0)

好的,所以你想在点击上一个ScrollView时添加EditText。你可以在上一个onClickListener的{​​{1}}内尝试以下代码来生成{ {1}}。

EditText

现在,如果要动态设置整个布局,请删除程序中的以下行:

ScrollView

并添加以下行:

    LinearLayout linLayout=(LinearLayout)findViewById(R.id.Your_LinearLayout);

    ScrollView sv = new ScrollView(this);
    sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    LinearLayout ll = new LinearLayout(this);
    ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    ll.setOrientation(1);
    sv.addView(ll); // adding a new LinearLayout inside your ScrollView

    for(int i = 0; i < 20; i++) 
    {
        Button b = new Button(this); //instead of Button you can add anything you want to add inside the LinearLayout of the ScrollView
        b.setText("Button "+i);
        ll.addView(b); //adding the Button to the LinearLayout inside ScrollView
    }

    linLayout.addView(sv);//finally adding the ScrollView to the parent LinearLayout of the layout which you have posted in the question

希望这有帮助。

Follow this tutorial for more info of how to add a ScrollView programmatically

答案 2 :(得分:0)

可能会有点晚,但这个答案可能有助于其他人搜索如何做到这一点。 Android的Listview附带了一些内置功能。试试

 getListView().addFooterView(LayoutInflater.from(getActivity()).inflate(R.layout.footer, null));

我的页脚只不过是需要高度的空视图

footer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            style="@style/bottomCellLayout"
            android:layout_width="match_parent"
            android:paddingBottom="12dp">

<View
        android:id="@+id/separator"
        style="@style/cellSeparatorLight"
        android:layout_width="match_parent"
        android:background="@color/grayNormal"
        android:layout_height="1dp"/>

</RelativeLayout>