Going back to the top of the screen when writing in the field

Asked

Viewed 201 times

5

I have the following error: I have a list and a field, every time I edit any text Edit from the list, it loses focus and goes to the top of the screen, notice the gif that at each letter of the word "test" the edittext loses and gains focus, only that the first gives no problem because it is already at the top of the list, now the others who are below the first give...
Erro quando escrevo

Activity Code

public class CategoriaListActivity extends ActionBarActivity {
private Categoria categoria = new Categoria();
private ListView listView;
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence titles[] = {"Receita", "Despesa"};
int numTabs = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_categoria_list);

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Window window = getWindow();
    adapter = new ViewPagerAdapter(getSupportFragmentManager(), titles, numTabs);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.setStatusBarColor(getResources().getColor(R.color.color_black));
    }

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true);
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    tabs.setViewPager(pager);


    //refreshLista();


}
public void addCategoriaDespesa(View view) {

    final EditText etCatDes = (EditText)
            findViewById(R.id.etCateg);

    ImageButton btCatDes = (ImageButton)
            findViewById(R.id.btAdd);


    if(etCatDes.getText().toString().length() == 0){
        etCatDes.setError("Campo obrigatório");
        return;
    }

    categoria.setNome(etCatDes.getText().toString());
    categoria.setDespesa(1);

    CategoriaDAO categoriaDAO = new CategoriaDAO(this);
    categoriaDAO.inserir(categoria);

    Toast.makeText(this, "Categoria adicionada!", Toast.LENGTH_SHORT).show();

    etCatDes.setText("");

    refreshLista(1);
}

public void addCategoriaReceita(View view) {

    final EditText etCatRec = (EditText)
            findViewById(R.id.etCategR);

    ImageButton btCatRec = (ImageButton)
            findViewById(R.id.btAdd);

    if (etCatRec.getText().toString().length() == 0) {
        etCatRec.setError("Campo obrigatório");
        return;
    }

    categoria.setNome(etCatRec.getText().toString());
    categoria.setDespesa(0);


    CategoriaDAO categoriaDAO = new CategoriaDAO(this);
    categoriaDAO.inserir(categoria);



    Toast.makeText(this, "Categoria adicionada!", Toast.LENGTH_SHORT).show();

    refreshLista(0);
    etCatRec.setText("");
}


public void refreshLista(int despesa) {
    CategoriaDAO categoria = new CategoriaDAO(this);
    List<Categoria> list = categoria.getLista(despesa);
    ListView listView;
    if (despesa == 1) {
        listView = (ListView) this.findViewById(R.id.listaDes);
    }else {
        listView = (ListView) this.findViewById(android.R.id.list);
    }
    CategoriaAdapter catAd = new CategoriaAdapter(this, list, listView);
    listView.setAdapter(catAd);
}

/*   @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_categoria_list, 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.
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
            return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Edited.

Xml da Activity

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
xmlns:android="http://schemas.android.com/apk/res/android">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>

<com.prjctt.allan.newfinance.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1"
    />

</LinearLayout>

Fragment and xml of Fragment dealing with that part of gif

public class CategoriaReceita extends Fragment {
    Categoria categoria;
    EditText etCat;
    ImageButton addCat;

public CategoriaReceita() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_categoria_receita, container, false);

    addCat = (ImageButton) v.findViewById(R.id.btAddR);
    etCat = (EditText) v.findViewById(R.id.etCategR);

    refreshLista(v);
    return v;


}
public void refreshLista(View layout) {
    CategoriaDAO categoria = new CategoriaDAO(this.getActivity());
    List<Categoria> list = categoria.getLista(0);
    ListView listView = (ListView) layout.findViewById(android.R.id.list);
    CategoriaAdapter catAd = new CategoriaAdapter(this.getActivity(), list, listView);
    listView.setAdapter(catAd);
}



}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.prjctt.allan.newfinance.CategoriaReceita">


<android.support.v7.widget.CardView
    android:id="@+id/some_card_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_marginTop="2dp"
    android:background="#cccccc">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <EditText
            android:inputType="textCapSentences"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/etCategR"
            android:hint="Adicione uma categoria"
            android:layout_weight="4"
            android:layout_margin="8dp" />

        <ImageButton
            android:src="@drawable/ic_action_newc"
            android:background="@android:color/transparent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Adicionar"
            android:id="@+id/btAddR"
            android:onClick="addCategoriaReceita"
            android:layout_weight="1"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp" />
    </LinearLayout>
</android.support.v7.widget.CardView>


<android.support.v7.widget.CardView
    android:id="@+id/some_card_view1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_marginTop="2dp"
    android:background="#cccccc">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@android:id/list" />
</android.support.v7.widget.CardView>

Adapter and xml related to category list

public class CategoriaAdapter extends BaseAdapter {
private Context context;
List<Categoria> lista;
private ListView listView;
private SubCategoria subcategoria = new SubCategoria();
private TextView lvPai;

public CategoriaAdapter(Context context, List<Categoria> lista, ListView listView) {
    this.context = context;
    this.lista = lista;
    this.listView = listView;
}


@Override
public int getCount() {
    return lista.size();
}

@Override
public Object getItem(int position) {
    return lista.get(position);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
    final int auxPosition = position;

    final Categoria categoria = new Categoria();


    LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    final CardView layout = (CardView)
            inflater.inflate(R.layout.categoria_row, null);

    final TextView categ = (TextView)
            layout.findViewById(R.id.tvCat);
    categ.setText(lista.get(position).getNome());

    final EditText etsubcat = (EditText)
            layout.findViewById(R.id.etSubCat);

    final ImageButton btaddsub = (ImageButton)
            layout.findViewById(R.id.btAddSubCat);



    etsubcat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            etsubcat.requestFocus();
        }
    });

    btaddsub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Categoria cat = (Categoria) getItem(position);

            subcategoria.setNome(etsubcat.getText().toString());
            subcategoria.setIdCategoria(cat.getId());


            SubCategoriaDAO subCategoriaDAO = new SubCategoriaDAO(context);

            if(etsubcat.getText().toString().length() == 0) {
                etsubcat.setError("Campo obrigatório");
            }else {
                subCategoriaDAO.inserir(subcategoria);
                Toast.makeText(context, "Subcategoria adicionada!", Toast.LENGTH_SHORT ).show();
                etsubcat.setText("");

                lvPai = (TextView) layout.findViewById(R.id.tvCat);
                List<SubCategoria> list = subCategoriaDAO.getLista(lvPai.getText().toString());
                ListView listView = (ListView) layout.findViewById(android.R.id.list);
                /*ViewGroup.LayoutParams params =listView.getLayoutParams();
                params.height = list.size() * 42;
                listView.setLayoutParams(params);
                listView.requestLayout();*/
                listView.setAdapter(new SubCategoriaAdapter(context, list, listView));
            }
        }
    });


    listView.setDivider(null);


    lvPai = (TextView) layout.findViewById(R.id.tvCat);
    SubCategoriaDAO subcategoria = new SubCategoriaDAO(context);
    List<SubCategoria> list = subcategoria.getLista(lvPai.getText().toString());
    ListView listView = (ListView) layout.findViewById(android.R.id.list);
    listView.setAdapter(new SubCategoriaAdapter(context, list, listView));


    return layout;
}


<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/some_card_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#cccccc">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_margin="8dp"
    android:text="Large Text"
    android:id="@+id/tvCat" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/etSubCat"
    android:inputType="textCapSentences"
    android:layout_alignBottom="@+id/btAddSubCat"
    android:layout_alignLeft="@+id/tvCat"
    android:layout_alignStart="@+id/tvCat"
    android:layout_toLeftOf="@+id/btAddSubCat"
    android:layout_toStartOf="@+id/btAddSubCat"
    android:hint="Adicionar subcategoria"
    android:layout_marginLeft="28dp"/>

<ImageButton
    android:contentDescription="Adicionar subcategoria"
    android:src="@drawable/ic_action_newc"
    android:background="@android:color/transparent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btAddSubCat"
    android:layout_below="@+id/tvCat"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:focusable="false"/>


<com.prjctt.allan.newfinance.ExpandedListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:scrollbars="none"
    android:padding="0dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/btAddSubCat"/>

  • Your first Edittext is autofocus?

  • Post the code of your Activity.

  • Edited by @Israelsousa

  • Pole the XML of Activity also.

  • @Israelsousa Editado

  • I can’t understand yours XML, where is the code that contains the two Edittext, subcategory, etc..?

  • is on the list Adapter, editing @Israelsousa

  • Adapter included @Israelsousa

  • Try to remove the android:focusable="false" or switch to true and see what happens.

  • Do Imagebutton? Is it? @Israelsousa

  • You have to try. Your code is a little confused, the problem is at the moment you give a focus to Editext Add subcategory.

  • I commented on that part, still the error... @Israelsousa

  • In fact @Israelsousa the error is as follows, every time I edit any text Edit from the list, it loses focus and goes to the top of the screen, notice the gif that at each letter of the word "test" the edittext loses and gains focus, only the first gives no problem pq is already at the top of the list..

Show 8 more comments

1 answer

2


I managed, after a search I discovered the attribute descendantFocusability, and I was testing the values until I found the value afterDescendants.

Listview looked like this in xml, it solved my problem:

<ListView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@android:id/list"
        android:descendantFocusability="afterDescendants"/>

Thank you for the comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.