使用BottomSheetDialog显示RecyclerView的问题

时间:2019-03-09 15:46:49

标签: java android android-recyclerview

我无法使用BottomSheetDialog将recyclerview与适配器绑定 这是我的 MainActivity

public class MainActivity extends AppCompatActivity {

BottomAppBar bottomAppBar;
FloatingActionButton floatingActionButton;
ImageView emptyView;

private RecyclerView recyclerView;
private List<AppModel> appModelResult = new ArrayList<>();
private AppAdapter appAdapter;
private RecyclerView.LayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    emptyView = findViewById(R.id.empty_view);
    bottomAppBar = findViewById(R.id.bottom_app_bar);
    setSupportActionBar(bottomAppBar);
    floatingActionButton =findViewById(R.id.fab);
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddAppDialog addAppDialog = new AddAppDialog();
            addAppDialog.show(getSupportFragmentManager(),"AddBottomSheet");
        }
    });

    bottomAppBar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BottomSheetDialog bottomSheet = new BottomSheetDialog();
            bottomSheet.show(getSupportFragmentManager(), "BottomSheet");
        }
    });

//RecyclerView Binding
    recyclerView = findViewById(R.id.app_list);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    appAdapter = new AppAdapter(appModelResult);
    recyclerView.setAdapter(appAdapter);
    appAdapter.setItems(appModelResult);


    if(appAdapter.getItemCount() == 0){
        recyclerView.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
    }else{
        recyclerView.setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.GONE);
    }

}}   

这是我的 BottomSheetDialog ,我在其中输入了2个EditText和一个Spinner。在“保存”按钮上键入应该会在主屏幕上显示书面数据,但是在显示器上什么也没有。

public class AddAppDialog extends BottomSheetDialog implements AdapterView.OnItemSelectedListener {

static TextInputEditText appUrl;
TextInputEditText appDesc;
MaterialButton saveButton;
Spinner spinnerCategory;

AppModel appModel = new AppModel();
List<AppModel> appModelResult = new ArrayList<>();


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_bottomsheet_add,container,false);

    appUrl = v.findViewById(R.id.app_url);
    appDesc = v.findViewById(R.id.app_desc);
    saveButton = v.findViewById(R.id.save_button);
    spinnerCategory = v.findViewById(R.id.spinner_category);
    List<String> categories = new ArrayList<String>();
    categories.add(0,"Choose category");
    categories.add("Education");
    categories.add("Entertainment");
    categories.add("Games");
    categories.add("Health");
    categories.add("Personalization");
    categories.add("Social");
    categories.add("Tools");
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter(getActivity().getApplicationContext(),android.R.layout.simple_spinner_item,categories);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerCategory.setAdapter(adapter);
    spinnerCategory.setOnItemSelectedListener(this);

    appUrl.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) { }

        @Override
        public void afterTextChanged(Editable s) {
            new Parse().execute();
        }
    });

    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(appUrl.getText().toString().trim().length() != 0) {
                appModel.setAppDesc(appDesc.getText().toString());
                appModelResult.add(0,new AppModel(Parse.eImage,Parse.eName,appModel.getAppCategory(),appModel.getAppDesc()));
                System.out.println(appModelResult.get(0).getImage());
                System.out.println(appModelResult.get(0).getAppText());
                System.out.println(appModelResult.get(0).getAppCategory());
                System.out.println(appModelResult.get(0).getAppDesc());

                dismiss();
            }else{
                appUrl.setError("URL is required :)");
            }
        }
    });

    return v;
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if(parent.getItemAtPosition(position).equals("Choose category")){

    }else{
        String selectedCategory = parent.getItemAtPosition(position).toString();
        appModel.setAppCategory(selectedCategory);
    }
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}}

最后,我的 RecyclerView 适配器如下所示:

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.AppViewHolder> {

private List<AppModel> mAppModelList;

public AppAdapter(List<AppModel> appModelList) {
    mAppModelList = appModelList;
}

public static class AppViewHolder extends RecyclerView.ViewHolder{

    public ImageView appImage;
    public TextView appText,appCategory,appDesc;

    public AppViewHolder(@NonNull View itemView) {
        super(itemView);

        appImage = itemView.findViewById(R.id.app_image_list);
        appText = itemView.findViewById(R.id.app_text_list);
        appCategory = itemView.findViewById(R.id.app_category_list);
        appDesc = itemView.findViewById(R.id.app_desc_list);

    }
}

public void setItems(List<AppModel> items){
    mAppModelList.addAll(items);
    notifyDataSetChanged();
}

public void clearItems(){
    mAppModelList.clear();
    notifyDataSetChanged();
}

@NonNull
@Override
public AppViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_list,parent,false);
    AppViewHolder appViewHolder = new AppViewHolder(v);
    return  appViewHolder;
}

@Override
public void onBindViewHolder(@NonNull AppViewHolder holder, int position) {
    AppModel appModel = mAppModelList.get(position);
    holder.appText.setText(appModel.getAppText());
    holder.appCategory.setText(appModel.getAppCategory());
    holder.appDesc.setText(appModel.getAppDesc());
    Picasso.get().load(appModel.getImage()).into(holder.appImage);
}

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

我认为XML文件还可以,因此我没有在这里粘贴它,唯一的问题是关于绑定过程。

1 个答案:

答案 0 :(得分:0)

解决了在Fragment中添加RecyclerView并通过分离和附加刷新列表的问题

getActivity().getSupportFragmentManager().beginTransaction().detach(getFragmentManager().findFragmentById(R.id.recycler_fragment)).commitNowAllowingStateLoss();
                getActivity().getSupportFragmentManager().beginTransaction().attach(new RecyclerFragment()).commitAllowingStateLoss();

注意!

要重新加载片段,请仅使用动态片段。