将Button结束动态添加到GridView

时间:2013-03-02 19:26:17

标签: android android-button android-gridview

努力实现

  • 在GridView之后添加按钮layout_height="wrap_parent"& layout_width="fill_parent"提供更好的UIx。

约束:

  • 将从Facebook-Graph-Api获取的ImageView添加到由ImageView组成的GridView。

  • 获取图像后,添加GridView的按钮。

尝试

  • 1 即可。在AysncTask的onPostExecute()中将Button的可见性设置为可见,这有助于我获取Image的

  • 2 即可。在ScrollListener中将Button的可见性设置为Visible,如果Scrolling到达GridView的末尾,即GridView中的 Last Image

  • ,它将运行
  • 但是在GridView的末尾没有显示Button。不知道为什么??

我的XML布局::

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" 
android:id="@+id/rootview"
>
<Button
    android:id="@+id/fbconnect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/connectfb" 
    android:layout_centerHorizontal="true"
    android:visibility="visible"
    />
<GridView 
    android:id="@+id/gridphoto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:columnWidth="80dp"
    android:numColumns="auto_fit" 
    android:verticalSpacing="2dp"
    android:horizontalSpacing="2dp"
    android:stretchMode="columnWidth"
    />
<Button 
        android:id="@+id/loadmore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_centerHorizontal="true"
        android:layout_below="@id/gridphoto"
        android:visibility="gone"
        android:text="@string/load_more"
    />
</RelativeLayout>

代码::通过AysncTask获取图像并在onPostExecute上设置可见性,以在GridView末尾添加LoadMore按钮。

 protected void onPostExecute(Void result) {
  loadMore.setVisibility(View.VISIBLE); // Load More Button
 }

Code ::在GridView上实现ScrollListener - &gt;检查是否达到底部,然后更改“加载可见性更多”按钮。

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
 if (gridView.getLastVisiblePosition() + 1 == increasingItem) // Checking if it reaches bottom of GridView or not. It prefectly runs. Checked via Logging into StackTrace.
  {
     loadMore.setVisibility(View.VISIBLE); // Load More Button
  }
}

1 个答案:

答案 0 :(得分:4)

我用这段代码完成了它:

MainActivity:

 public class MainActivity extends Activity implements OnScrollListener {




        ArrayAdapter<String> adapter;

        GridView gridphoto;
        Button loadMore;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            gridphoto = (GridView) findViewById(R.id.gridphoto);
            loadMore = (Button) findViewById(R.id.loadmore);
            gridphoto.setOnScrollListener(this);
            adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
            for (int i = 0; i < 80; i++) {
                adapter.add("dummy data " + i);

            }
            gridphoto.setAdapter(adapter);

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            if (gridphoto.getLastVisiblePosition() + 1 == 80) {
                loadMore.setVisibility(View.VISIBLE); // Load More Button
            }
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }
    }

和activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <GridView
            android:id="@+id/gridphoto"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:columnWidth="80dp"
            android:gravity="center"
            android:horizontalSpacing="2dp"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:verticalSpacing="2dp" />

        <Button
            android:id="@+id/loadmore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Load More"
            android:visibility="gone" />
    </LinearLayout>

</RelativeLayout>

它的工作方式与预期一致,希望它能帮助