点击加载片段中的视图?

时间:2018-10-05 18:45:39

标签: android android-layout android-recyclerview android-view

我有一个带有Imageview的RelativeLayout。单击ImageView时,我需要调用Async任务来获取一些数据并将其显示在RecyclerView的屏幕底部。像这样:

之前,点击任何内容:

enter image description here

单击ImageView-> (调用异步任务并显示进度栏):

enter image description here

结果

enter image description here

我可以使用初始布局(第一个图像),但是我不确定如何重新组织布局以使整个事情在单击时发生。我还需要能够增加RecyclerView,因为我将允许用户在该底部加载更多“评论”。

我现在拥有的简化布局基本上是这样:

<!--Does all of this belong in a ScrollView to allow for the comments section at the bottom? -->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        <!-- Lots of TextViews and buttons-->
        <ImageView
            android:id="@+id/main_picture_with_unknown_size"
            android:layout_width="match_parent"
            android:layout_height="wrap_content/>
         <ImageView
            android:id="@+id/button_comments"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"                           
            android:background="@drawable/ic_comments" />

    <!-- Do I need to put the comments section here in a FrameLayout?-->
    </RelativeLayout>

我在imageview上有一个单击侦听器,我想将其用作注释部分的触发器。如何在点击时展开/加载视图?

1 个答案:

答案 0 :(得分:0)

您可以这样做:

  1. 将回收站视图添加到布局中,并按android:visibility="gone"隐藏。
  2. 安装RecyclerView适配器并将其设置为RecyclerView
  3. 当用户单击图像时,启动您的AsyncTask。任务完成后,获取列表并将其提供给适配器。只需从您的活动中调用adapter.notifyDataSetChanged();,即可通知适配器使用新数据刷新自身。现在您具有完整的回收者视图。您可以设置recyclerView.setVisibility(true);

要实现无限滚动,您可以执行以下操作:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    if (layoutManager.findLastVisibleItemPosition() == adapter.getItemCount() - 1) {
                          // LOAD MORE COMMENTS AND ADD GIVE THEM TO YOUR ADAPTER 
                    }
                }
            });

希望有道理。