2013-10-11 168 views
1

我有一個GridView與自定義適配器,顯示RSS頻道的最新BBC新聞。我想在GridView的頂部加載GIF圖像或webview,直到我的RSS閱讀完成(需要5秒鐘)。這是我的代碼:覆蓋gridview頂部的圖像視圖

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上加載圖像?

回答

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> 
+0

這樣做的伎倆,謝謝。 –

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