更新底部的 recyclerview 内容失败 sheet

update recyclerview contents fail in the bottom sheet

我想通过单击事件更改底部 sheet 视图中的列表内容,但我只有空 sheet。这是从 BottomSheetSample(github.com/niravkalola/BottomSheetSample).

修改的演示

mainActivity.java

package com.nkdroid.bottomsheetsample;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements    ItemAdapter.ItemListener {

private Button btnView, btnDialog;
BottomSheetBehavior behavior;
private BottomSheetDialog mBottomSheetDialog;
private ItemAdapter mAdapter;
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnView = (Button) findViewById(R.id.btnView);
    btnDialog = (Button) findViewById(R.id.btnDialog);
    btnView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAdapter = new ItemAdapter(createItems());
            recyclerView.setAdapter(mAdapter);
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });

    btnDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showBottomSheetDialog();
        }
    });

    View bottomSheet = findViewById(R.id.bottom_sheet);
    behavior = BottomSheetBehavior.from(bottomSheet);
    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            // React to state change
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            // React to dragging events
        }
    });

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    mAdapter = new ItemAdapter(createItems(), this);
    recyclerView.setAdapter(mAdapter);

}


private void showBottomSheetDialog() {
    if (behavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
        behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
    }

    mBottomSheetDialog = new BottomSheetDialog(this);
    View view = getLayoutInflater().inflate(R.layout.sheet, null);
    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(new ItemAdapter(createItems(), new ItemAdapter.ItemListener() {
        @Override
        public void onItemClick(Item item) {
            if (mBottomSheetDialog != null) {
                mBottomSheetDialog.dismiss();
            }
        }
    }));

    mBottomSheetDialog.setContentView(view);
    mBottomSheetDialog.show();
    mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            mBottomSheetDialog = null;
        }
    });
}


@Override
protected void onDestroy() {
    super.onDestroy();
    mAdapter.setListener(null);
}

public List<Item> createItems() {
    Random r = new Random();
    ArrayList<Item> items = new ArrayList<>();
    items.add(new Item(R.mipmap.ic_launcher, "Item 1"+ r.nextInt(100)));
    items.add(new Item(R.mipmap.ic_launcher, "Item 2"));
    items.add(new Item(R.mipmap.ic_launcher, "Item 3"));
    items.add(new Item(R.mipmap.ic_launcher, "Item 4"));
    return items;
}

@Override
public void onItemClick(Item item) {
    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}

}

ItemAdapter.java:

package com.nkdroid.bottomsheetsample;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;


public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {

private List<Item> mItems;
private ItemListener mListener;

public ItemAdapter(List<Item> items, ItemListener listener) {
    mItems = items;
    mListener = listener;
}

public ItemAdapter(List<Item> items) {
    mItems = items;
}

public void setListener(ItemListener listener) {
    mListener = listener;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext())
            .inflate(R.layout.adapter, parent, false));
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.setData(mItems.get(position));
}

@Override
public int getItemCount() {
    return mItems.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public ImageView imageView;
    public TextView textView;
    public Item item;

    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        imageView = (ImageView) itemView.findViewById(R.id.imageView);
        textView = (TextView) itemView.findViewById(R.id.textView);
    }

    public void setData(Item item) {
        this.item = item;
        imageView.setImageResource(item.getDrawableResource());
        textView.setText(item.getTitle());
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.onItemClick(item);
        }
    }
}

public interface ItemListener {
    void onItemClick(Item item);
}
}

这是第一次和第二次单击 显示视图 按钮后的截图: first click second click

这已在 24.0.0 中修复,即 recyclerView 已更新。但是,如果您在 bottomsheet 和 recyclerview 上使用 wrap_content,则必须使用它,以便正确计算 bottomsheet 高度。

  bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {

                if(newState==BottomSheetBehavior.STATE_EXPANDED){
                    bottomSheet.requestLayout();

                    /*or
                     recyclerView.scrollToPosition(0);
                     recyclerView.requestLayout();*/

                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });

如果您想使用早期版本,请使用

 bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {

                if(newState==BottomSheetBehavior.STATE_EXPANDED){
                    bottomSheet.requestLayout();
                    bottomSheet.invalidate();
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });