Listview getting refreshed after scrolling the list in android

孤街浪徒 提交于 2019-12-25 02:39:06

问题


thanks in advance , please forgive me if I am mistaking somewhere when explaining the question

I have a Listview and a custom ArrayAdapter and the layout for the custom ArrayAdapter is as follows.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="48dp"
    android:layout_marginTop="14dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:layout_marginRight="43dp"
    android:text="Button" />

</RelativeLayout>

and the custom array adapter java code is as follows.

public class CustomArrayAdapter extends ArrayAdapter<String>{

Context context;
ArrayList<String> list=new ArrayList<String>();
public CustomArrayAdapter(Context con,ArrayList<String> data) {
    // TODO Auto-generated constructor stub
    super(con,R.layout.adapter_layout,data);
    this.context=con;
    this.list=data;

}

@Override
public View getView(int postion, View arg1, ViewGroup parent) {
    // TODO Auto-generated method stub
    //LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //View view=inflater.inflate(R.layout.adapter_layout, parent,false);

    Myclass item=new Myclass(context);

    /*TextView tv=(TextView)view.findViewById(R.id.textView1);
    Button button=(Button)view.findViewById(R.id.button1);
    item.SetContextForTextView(tv,list.get(postion));

    item.SetContextForTheButton(button);
    tv.setText(list.get(postion));*/
    item.setView();
    //item.setText(list.get(postion));



    return item.getView();
}

}

Since i have created a separate object of Myclass for every item of the list view the code for the Myclass is given below.

public class Myclass {
TextView tv;
Button button;
Context context;
View view;
LayoutInflater inflater;
public Myclass(Context con)
{
    context=con;
}
public void setText(String text)
{
    this.tv.setText(text);
}
public String getText()
{
    return this.tv.getText().toString();
}

public void SetContextForTextView(TextView text,String data)
{
    tv=text;
    setText(data);
}
public void SetContextForTheButton(Button btn)
{
    button=btn;
    button.setOnClickListener(button_click);
}

public OnClickListener button_click=new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        setText("hi...");

    }
};

public void setView()
{
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view=inflater.inflate(R.layout.adapter_layout,null);
    tv=(TextView)view.findViewById(R.id.textView1);
    button=(Button)view.findViewById(R.id.button1);
    button.setOnClickListener(button_click);

}

public View getView()
{
    return view;
}



}

since on the button click on the every list item I am updating the text of the TextView to "hi..." it works perfectly but when i am scrolling the updated result get refreshed.

I had searched a lot on this but cant found the answer .

Please little help in this matter would be appreciable.

before scrolling the result look like this

and after scrolling result look like this


回答1:


The Adapter is getting refresh, it the normal way to do it.

in your getView inflate the view only when its null, and create an Class Holder, so you can reuse your Widget views (TextViews and such) whenever you want :

@Override
public View getView(int postion, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = convertView;
ViewHolder h;
if(v == null){
    v=inflater.inflate(R.layout.adapter_layout, parent,false);
    h = new ViewHolder();
    h.tv = (TextView)v.findViewById(ID);
    v.setTag(h);
}else{
   h = (ViewHolder) v.getTag();
}
String s = getItem(position);
h.tv.setText(s);
return v;
}

private static class ViewHolder{
TextView tv;
}



回答2:


Android ListView has designed such that it will be update,when you scroll list items.Actually when we bind Adapter with ListView,Automatically newview is called and bind all data.You might have noticed that on listview scroll getView() is being called.Watch this official video for clear understanding.



来源:https://stackoverflow.com/questions/20855834/listview-getting-refreshed-after-scrolling-the-list-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!