【问题标题】:Listview getting refreshed after scrolling the list in android在android中滚动列表后Listview被刷新
【发布时间】:2013-12-31 10:27:52
【问题描述】:

先谢谢了,如果我在解释问题时弄错了地方,请原谅我

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

<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代码如下。

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 的代码。

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...”,它工作得很好,但是当我滚动更新的结果时会刷新。

我对此进行了很多搜索,但找不到答案。

请在这件事上提供一点帮助。

滚动前的结果是这样的

滚动后的结果是这样的

【问题讨论】:

  • android.amberfog.com/?p=296这是你的救赎
  • 你需要最后点击的位置并检查适配器的getView方法
  • 感谢@AstralProjection 该链接很有帮助,我只是想出了如何解决问题。感谢您的帮助....
  • 欢迎您,祝您编码愉快:)

标签: android listview android-listview android-adapter


【解决方案1】:

适配器正在刷新,这是正常的方式。

在您的 getView 中,仅当它为 null 时才对视图进行膨胀,并创建一个 Class Holder,因此您可以随时重用您的 Widget 视图(TextViews 等):

@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 设计成当你滚动列表项时它会更新。实际上当我们将 Adapter 与 ListView 绑定时,会自动调用 newview 并绑定所有数据。您可能已经注意到@ 987654325@滚动getView()正在被调用。观看this官方视频清晰了解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-16
      • 2021-08-07
      • 2011-04-06
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多