如何从Android中的适配器访问活动内部的视图?

时间:2018-09-05 12:22:16

标签: android android-activity adapter

有人知道如何在活动中访问适配器imageView来隐藏视图。请指定任何示例。

3 个答案:

答案 0 :(得分:2)

我希望这对您有用。

通过使用$ cat Dockerfile FROM nginx RUN touch /usr/share/nginx/html/test1234 && ls /usr/share/nginx/html/ $ docker build -t nginx-image-test .; docker run -p 8889:80 --name some-nginx -v /full_path/test:/usr/share/nginx/html:rw -d nginx-image-test; ls ./test; ... $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 33ccbea6c1c1 nginx-image-test "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 0.0.0.0:8889->80/tcp some-nginx $ cat test/index.html hello world $ curl -i http://localhost:8889 HTTP/1.1 200 OK Server: nginx/1.15.3 Date: Thu, 06 Sep 2018 15:35:43 GMT Content-Type: text/html Content-Length: 12 Last-Modified: Thu, 06 Sep 2018 15:31:11 GMT Connection: keep-alive ETag: "5b91483f-c" Accept-Ranges: bytes hello world ,我们可以轻松地从活动或片段中隐藏视图。

@if (Model.SideItems != null) { <div class="col-md-12">&nbsp;</div> var sideWidgetItems = ((IEnumerable<dynamic>)Model.SideItems.Items).Where(x => x.ContentItem.ContentType == "HtmlWidget"); if (sideWidgetItems.Any()) { foreach (var item in sideWidgetItems) { <div class="col-md-12"> <div class="panel panel-success"> <div class="panel-heading">@Html.Raw(item.ContentItem.Content.Parts[3].Title) </div> <div class="panel-body">@Html.Raw(item.ContentItem.Content.Parts[1].Text) </div> </div> </div> } } } 中保存标志,即在活动中为true。

如果您使用的是SharedPreferences,则以SharedPreferences方法检查条件

Recyclerview

答案 1 :(得分:0)

转到适配器的onBindViewHolder并获取imageview的ID和类似的代码 holder.mImgVw.setVisibility(View.GONE);

答案 2 :(得分:0)

您不应直接与ImageView交互,而可以使用notifyItemChanged()更新适配器中的ImageView状态。但是,您需要通过在模型数据中添加标志或使用SparseBooleanArray作为保存ImageView状态的机制来稍微修改Adapter代码。

这里是示例:

public class Adapter ... {
  private SparseBooleanArray mSelectedItems;
  private List<YourModel> mItems;

  public Adapter(List<YourModel> items) {

    mItems = items;
    mSelectedItems = new SparseBooleanArray();
  }

  ...
  public void onBindViewHolder(....) {
    int itemPosition = viewHolder.getAdapterPosition();
    YourModel item = items.get(itemPosition);

    boolean visible = mSelectedItems.get(itemPosition);   
    viewHolder.imageView.setVisibility(visible? View.VISIBLE: View.GONE);

    ...
  }

  public void setItemVisibilityByPosition(int position, boolean visible) {
    mSelectedItems.put(position, visible);
    notifyItemChanged(position);
  }

}

您可以使用以下方法更改图像可见性:

 // Assume the mAdapter is your Adapter
 mAdapter.setItemVisibilityByPosition(5, true);