1

我有GridView一个自定义适配器,可以显示来自 RSS 频道的最新 BBC 新闻。我想在GridView我的 RSS 阅读完成之前加载一个 GIF 图像或 web 视图(这需要 5 秒)。这是我的代码:

private void readRssData() {
        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
            public void run() {
                try{
                    @SuppressWarnings("resource")
                    RSSReader reader = new RSSReader();
                    String uri = "http://feeds.bbci.co.uk/news/world/rss.xml";
                    RSSFeed feed = reader.load(uri);
                    List<RSSItem> list = feed.getItems();
                    int c = 0;
                    for (RSSItem i: list) {
                        if (c > 15){
                            break;
                        }else{
                            addNewsItemInGrid(i.getTitle(), i.getDescription());
                            c++;
                        }
                    }
                }catch (Exception ex){
                    Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_SHORT).show();
                }finally{
                    GridView gv = (GridView)findViewById(R.id.gridview);
                    gv.setAdapter(new RSSAdapter(getApplicationContext(), results));
                }
            }
        }, 3000);
    }


private ArrayList<SetAdd> results = new ArrayList<SetAdd>();
    private void addNewsItemInGrid(String title, String description) {
        SetAdd setadd = new SetAdd();
        setadd.setTitle(title);
        setadd.setDescription(description);
        results.add(setadd);
    }


<LinearLayout android:id="@+id/gridview_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/txt_DOTSTEXT"
        >
        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnWidth="90dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="10dp"
            android:horizontalSpacing="10dp"
            android:stretchMode="columnWidth"
            android:gravity="center" >
        </GridView>
    </LinearLayout>

你怎么看?如何使用图像或 webview 在我的顶部加载图像GridView

4

2 回答 2

1

将您的父布局更改为 Framelayout

<FrameLayout android:id="@+id/gridview_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/txt_DOTSTEXT"
        >
        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnWidth="90dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="10dp"
            android:horizontalSpacing="10dp"
            android:stretchMode="columnWidth"
            android:gravity="center" >
        </GridView>
       // add a loading image view here with some id .. and adjust its visibility inside the activity run time

    </FrameLayout>
于 2013-10-11T13:25:11.067 回答
1

将您的 xml 文件更改为 -

 <LinearLayout android:id="@+id/gridview_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/txt_DOTSTEXT"
        >
        <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:background="@drawable/myimage"
        android:id="@+id/imageview1"/>
        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnWidth="90dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="10dp"
            android:horizontalSpacing="10dp"
            android:stretchMode="columnWidth"
            android:gravity="center" >
        </GridView>
    </LinearLayout>

并在源代码中添加以下代码,例如在阅读 rss 后要删除图像时

ImageView imageView = (ImageView) findViewById(R.id.imageview1);
imageView.setVisibility(View.GONE);
于 2013-10-11T13:32:40.773 回答