我正在使用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();
}
}
SetupPrinterAdapter
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>
最佳答案
问题是,
a)当前材质对话框具有自己的布局.customView(R.layout.test, false)
而且您还拥有自己的布局,可以从中初始化RecyclerView
RecyclerView rv = view.findViewById(R.id.rvSetupPrinter2);
因此,根据点
a
,view
与MaterialDialog
没有连接,并且不会显示recyclerview。解决方案:将
view
与材料对话框链接为MaterialDialog dialog =
new MaterialDialog.Builder(this)
.title(R.string.title_printer_setup)
.customView(view, false)
// ^^^ use view
.negativeText(android.R.string.cancel)
.build();
或更好的解决方案是将
view
删除为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();
}
关于android - 未连接适配器;跳过 Material 对话框中“回收者” View 的布局,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51014671/