Android 刷新ListView时自定义视图组中的问题

Android 刷新ListView时自定义视图组中的问题,android,listview,viewgroup,Android,Listview,Viewgroup,首先,我在gitHub上创建了一个回购协议,其中包含一个示例项目,其中有我的bug: 我在customViewGroups中的ListView遇到困难: 我模拟了一个assynk数据查询,然后尝试填充我的listview,但遇到了一些困难。请注意,这个应用程序有点过头了,因为我需要在我的大应用程序中使用它。 我不能传给碎片 我的问题是我的listView不刷新,有时会在打开菜单时刷新 package renaud.bug; import java.util.ArrayList;

首先,我在gitHub上创建了一个回购协议,其中包含一个示例项目,其中有我的bug:

我在customViewGroups中的ListView遇到困难: 我模拟了一个assynk数据查询,然后尝试填充我的listview,但遇到了一些困难。请注意,这个应用程序有点过头了,因为我需要在我的大应用程序中使用它。 我不能传给碎片

我的问题是我的listView不刷新,有时会在打开菜单时刷新

    package renaud.bug;

import java.util.ArrayList;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SampleBugActivity extends Activity {

    private boolean menuDisplayed;
    private CustomViewGroup cvg;
    private ListView myListView;
    private ArrayList<String> myStrings;
    private MyAdapter stringAdapter;
    private LayoutInflater inflater;
    private ArrayList<String> array;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myStrings = new ArrayList<String>();
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        cvg = (CustomViewGroup) findViewById(R.id.viewGroup);

        stringAdapter = new MyAdapter(this, R.layout.list_view_item, myStrings);
        myListView = (ListView) findViewById(R.id.listView1);

        stringAdapter.add("TEST ");
        stringAdapter.notifyDataSetChanged();

        myListView.setAdapter(stringAdapter);

        new Thread(assyncSim).start();
    }

    private Runnable onFinish = new Runnable() {

        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), "thread finishes push menu button", Toast.LENGTH_LONG);
            Log.i("renaud", "thread finishes push menu button");
            if (array != null && array.size() > 0) {
                stringAdapter.notifyDataSetChanged();
                for (int i = 0; i < array.size(); i++) {
                    stringAdapter.add(array.get(i));
                }
            }
            stringAdapter.notifyDataSetChanged();
        }
    };
    private Runnable assyncSim = new Runnable() {

        @Override
        public void run() {

            array = new ArrayList<String>();
            for (int i = 0; i < 10; i++) {
                array.add("Item " + i);
            }
            synchronized (this) {
                try {
                    wait(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            runOnUiThread(onFinish);
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (!menuDisplayed) {
            customScroll(200);
            menuDisplayed = true;
            return true;
        } else {
            customScroll(0);
            menuDisplayed = false;
            return true;
        }

    }

    public void customScroll(final int i) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                if (i == 0) {
                    for (int inc = 200; inc >= 0; inc -= 5) {
                        final int inc2 = inc;
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                cvg.scrollTo(inc2);
                            }
                        });
                        try {

                            synchronized (this) {
                                wait(5);
                            }

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    for (int inc = 0; inc <= 200; inc += 5) {
                        final int inc2 = inc;
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                cvg.scrollTo(inc2);
                            }
                        });
                        try {

                            synchronized (this) {
                                wait(5);
                            }

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }).start();
    }

    private class MyAdapter extends ArrayAdapter<String> {

        private ArrayList<String> myStrings;

        public MyAdapter(Context context, int textViewResourceId,
                ArrayList<String> strings) {
            super(context, textViewResourceId, strings);
            this.myStrings = strings;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_view_item, null);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.itemTextView);
            tv.setText(myStrings.get(position));

            return convertView;
        }
    }

}
package renaud.bug;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类SampleBugActivity扩展活动{
私有布尔菜单显示;
私人CustomViewGroup cvg;
私有列表视图myListView;
私有数组列表myStrings;
私有MyAdapter stringAdapter;
私人充气机;
私有数组列表数组;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myStrings=newarraylist();
充气器=(LayoutInflater)getSystemService(Context.LAYOUT\u充气器\u SERVICE);
cvg=(CustomViewGroup)findViewById(R.id.viewGroup);
stringAdapter=新的MyAdapter(这个,R.layout.list\u view\u项目,myString);
myListView=(ListView)findViewById(R.id.listView1);
添加(“测试”);
stringAdapter.notifyDataSetChanged();
myListView.setAdapter(stringAdapter);
新线程(assyncSim.start();
}
private Runnable onFinish=new Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),“线程完成按钮菜单按钮”,Toast.LENGTH\u LONG);
Log.i(“雷诺”,“线程完成”菜单按钮);
if(array!=null&&array.size()>0){
stringAdapter.notifyDataSetChanged();
对于(int i=0;i=0;inc-=5){
最终int inc2=inc;
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
cvg.scrollTo(增量2);
}
});
试一试{
已同步(此){
等待(5);
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}否则{

对于(int inc=0;inc)您在这里有什么“困难”?没有刷新我的列表视图!抱歉(打开菜单时有时刷新)
package renaud.bug;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;


public class CustomViewGroup extends ViewGroup{
    private LayoutInflater inflater;
    private int mCurScreen;
    private int mDefaultScreen = 0;
    private View menuView;
    private View practicalView;

    public CustomViewGroup(Context context) {
        super(context);
        initView(context);
    }

    public CustomViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CustomViewGroup(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        mCurScreen = mDefaultScreen;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        practicalView = inflater.inflate(R.layout.layout, null);
        this.addView(practicalView);
        menuView = inflater.inflate(R.layout.menu_layout, null);
        this.addView(menuView);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        if (changed) {
            int childLeft = 0;
            final int childCount = getChildCount();

            for (int i = 0; i < childCount; i++) {
                final View childView = getChildAt(i);
                if (childView.getVisibility() != View.GONE) {
                    final int childWidth = childView.getMeasuredWidth();
                    childView.layout(childLeft, 0, childLeft + childWidth,
                            childView.getMeasuredHeight());
                    childLeft += childWidth;
                }
            }
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException(
                    "ScrollLayout only canmCurScreen run at EXACTLY mode!");
        }

        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException(
                    "ScrollLayout only can run at EXACTLY mode!");
        }

        // The children are given the same width and height as the scrollLayout
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
        }
        // Log.e(TAG, "moving to screen "+mCurScreen);
        scrollTo(mCurScreen * width, 0);

    }







    public void scrollTo(int i){
        scrollTo(i,0);
    }



}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <renaud.bug.CustomViewGroup
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewGroup"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </renaud.bug.CustomViewGroup>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="#991111"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#119911" >


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

    <TextView
        android:id="@+id/itemTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0"
        android:textAppearance="?android:attr/textAppearanceMedium" />



</LinearLayout>