代码之家  ›  专栏  ›  技术社区  ›  derrickrozay

没有适配器附加;跳转布局为回收者查看内部材料对话框

  •  1
  • derrickrozay  · 技术社区  · 7 年前

    我正在使用 material-dialogs 图书馆 显示列表但项目未显示。

    我有一个 BaseActivity 它被几个类扩展。我有一个用 MaterialDrawer .当另一个选择 Setup Printer 菜单项我想打开 material-dialog 显示打印机列表的。

    基本活动

    public abstract class BaseActivity extends AppCompatActivity {
    
        private AccountHeader headerResult = null;
        private Drawer result = null;
        Printer mPrinter = null;
        private List<PrinterModel> mPrinters;
    
    protected void setupToolbar() {
        setSupportActionBar(toolbar);
        ActionBar ab = getSupportActionBar();
        if (ab != null) { ab.setDisplayShowTitleEnabled(false); }
    
        new DrawerBuilder().withActivity(this).build();
    
        headerResult = new AccountHeaderBuilder()
                .withActivity(this)
                .withCompactStyle(true)
                .withHeaderBackground(R.drawable.logo)
                .withSavedInstance(savedInstanceState)
                .build();
    
    
        result = new DrawerBuilder()
                .withActivity(this)
                .withAccountHeader(headerResult)
                .addDrawerItems(
                        new SectionDrawerItem().withName(R.string.side_menu_title_settings),
                        new PrimaryDrawerItem().withName(R.string.side_menu_0_printer).withIcon(R.drawable.fire_smoke).withIdentifier(R.integer.side_menu_0_printer),
                        new SectionDrawerItem().withName("Settings"),
                        new SecondaryDrawerItem().withName("tWO").withIcon(R.drawable.fire)
                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        int item = (int) drawerItem.getIdentifier();
                        switch (item) {
                            case R.integer.side_menu_0_printer:
                                onSelectPrinter();
                                break;
                            default:
                                break;
                        }
                        System.out.println("DRAWER " + drawerItem.getIdentifier());
                        if (drawerItem != null && drawerItem.getIdentifier() == 1) {
                            //startSupportActionMode(new ActionBarCallBack());
                        }
    
                        if (drawerItem instanceof Nameable) {
    
                        }
    
                        return false;
                    }
                })
                .withSavedInstance(savedInstanceState)
                .build();
    }
    
    
    protected void onSelectPrinter() {
    
        PrinterModel printer = new PrinterModel("TESTDASDASD", "ASDJASLDKJASDASDASD");
        mPrinters = new ArrayList<>();
        mPrinters.add(0, printer);
        mPrinters.add(1, printer);
        mPrinters.add(2, printer);
        mPrinters.add(3, printer);
    
        SetupPrinterAdapter adapter = new SetupPrinterAdapter(mPrinters);
        LayoutInflater inflater=LayoutInflater.from(getBaseContext());
        View view=inflater.inflate(R.layout.test,null);
        RecyclerView.LayoutManager llm = new LinearLayoutManager(this);
        //LinearLayoutManager llm = new LinearLayoutManager(this);
    
        MaterialDialog dialog =
                        new MaterialDialog.Builder(this)
                        .title(R.string.title_printer_setup)
                        .customView(R.layout.test, false)
                        .negativeText(android.R.string.cancel)
                        .build();
    
        RecyclerView rv = view.findViewById(R.id.rvSetupPrinter2);
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);
        rv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        System.out.println("TEST" + adapter.getItemCount()); 
        dialog.show();
    }
    

    mainActivity(从baseActivity调用setuptoolbar())

    public class MainActivity extends BaseActivity {
    
        Context mContext;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            ButterKnife.bind(this);
            mContext = this;
            setupToolbar();
        }
    }
    

    设置打印机适配器

    package com.app.app;
    
    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import java.util.List;
    
    public class SetupPrinterAdapter extends RecyclerView.Adapter<SetupPrinterAdapter.ViewHolder> {
    
        public static class ViewHolder extends RecyclerView.ViewHolder {
    
            public TextView tvPrinterName;
    
            public ViewHolder(final View itemView) {
                super(itemView);
                tvPrinterName = itemView.findViewById(R.id.tvPrinterName);
            }
        }
    
        private List<PrinterModel> mPrinterModels;
    
        public SetupPrinterAdapter(List<PrinterModel> printers) {
            System.out.println("TEST " + printers.get(0).getName());
    
            mPrinterModels = printers;
        }
    
        @Override
        public SetupPrinterAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Context context = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);
            View rv = inflater.inflate(R.layout.rv_select_printer, parent, false);
            ViewHolder vh = new ViewHolder(rv);
            return vh;
        }
    
        @Override
        public void onBindViewHolder(SetupPrinterAdapter.ViewHolder viewHolder, int position) {
            PrinterModel printer = mPrinterModels.get(position);
            TextView mPrinterName = viewHolder.tvPrinterName;
            mPrinterName.setText(printer.getName());
        }
    
        @Override
        public int getItemCount() {
            return mPrinterModels.size();
        }
    }
    

    测试布局

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvSetupPrinter2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            />
    </android.support.constraint.ConstraintLayout>
    </LinearLayout>
    

    rv_select_printer.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="#80000000"
    
            android:id="@+id/tvPrinterName"
            />
    
    </RelativeLayout>
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Pavneet_Singh    7 年前

    问题是,

    a)当前材质对话框有自己的布局

    .customView(R.layout.test, false)

    而且,您也有自己的布局,从中开始初始化。 RecyclerView

    RecyclerView rv = view.findViewById(R.id.rvSetupPrinter2);
    

    所以按点 a 我是说, view MaterialDialog 并且没有显示RecyclerView。

    解决方案:链接 看法 材质对话框为

    MaterialDialog dialog =
                    new MaterialDialog.Builder(this)
                    .title(R.string.title_printer_setup)
                    .customView(view, false)
                    //          ^^^ use view
                    .negativeText(android.R.string.cancel)
                    .build();
    

    或者更好的办法是 看法 作为

    protected void onSelectPrinter() {
    
        PrinterModel printer = new PrinterModel("TESTDASDASD", "ASDJASLDKJASDASDASD");
        mPrinters = new ArrayList<>();
        mPrinters.add(0, printer);
        mPrinters.add(1, printer);
        mPrinters.add(2, printer);
        mPrinters.add(3, printer);
    
        SetupPrinterAdapter adapter = new SetupPrinterAdapter(mPrinters);
        RecyclerView.LayoutManager llm = new LinearLayoutManager(this);
        //LinearLayoutManager llm = new LinearLayoutManager(this);
    
        MaterialDialog dialog =
                        new MaterialDialog.Builder(this)
                        .title(R.string.title_printer_setup)
                        .customView(R.layout.test, false)
                        .negativeText(android.R.string.cancel)
                        .build();
    
        RecyclerView rv = dialog.getCustomView().findViewById(R.id.rvSetupPrinter2);
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);
        rv.setAdapter(adapter);
        System.out.println("TEST" + adapter.getItemCount()); 
        dialog.show();
    }