在Android中滚动列表后刷新Listview [英] Listview getting refreshed after scrolling the list in android

查看:404
本文介绍了在Android中滚动列表后刷新Listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此先感谢您,如果我在解释该问题时有误处,请原谅我

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

我有一个Listview和一个自定义ArrayAdapter,自定义ArrayAdapter的布局如下.

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>

和自定义数组适配器的Java代码如下.

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

}

因为我已经为列表视图的每个项目创建了一个单独的Myclass对象,所以下面给出了Myclass的代码.

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



}

由于在按钮上单击了每个列表项,所以我将TextView的文本更新为"hi ...",它可以正常工作,但是当我滚动更新的结果时,会刷新.

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

并且滚动结果如下所示

推荐答案

适配器正在刷新,这是正常的操作方式.

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

getView中的

仅在视图为null时才将其膨胀,并创建一个类持有者,因此您可以在任何需要的地方重用Widget视图(TextViews等):

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

这篇关于在Android中滚动列表后刷新Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆