我有一个应用程序,我在其中使用带有片段的幻灯片选项卡,在每个片段中我都有带有自定义适配器的 recyclerview,我现在有 5 个选项卡,当我滑动时应用程序可以正常工作,显示正确的数据,前 4 个选项卡共享相同的布局recyclerview 适配器第 5 个没有,现在如果我单击第 4 个选项卡,应用程序崩溃它说调试一段时间后它无法获取 textview 我发现布局传递给了适配器和 textviews 的 id '不匹配,所以在这种情况下,第 4 个选项卡的布局被传递给适配器,但是 textviews 的 id 来自布局不同的第 5 个选项卡,为什么会发生这种情况?这是片段中的适配器的外观:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_two, container, false);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
DisplayMetrics metrics = getResources().getDisplayMetrics();
List<Info> mDataset = new ArrayList<Info>();
mDataset.add(new Info("Display", Build.DISPLAY,""));
mDataset.add(new Info("Width", width+" px",""));
mDataset.add(new Info("Height", height+" px",""));
mDataset.add(new Info("Density", ""+metrics.density,""));
if ( v != null ) {
mRecyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view2);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mAdapter = new MyAdapter(mDataset, getContext(), "Display");
mRecyclerView.setAdapter(mAdapter);
}
return v;
}
这是适配器,我在每个片段中使用相同的适配器:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Info> mDataset;
private int mLayout;
private Context mContext;
private static int mTextInfoA;
private static int mTextInfoB;
private static int mTextInfoC;
private static String mFragment;
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(List<Info> mDataset, Context mContext, String mFragment) {
this.mDataset = mDataset;
this.mContext = mContext;
this.mFragment = mFragment;
if( mFragment.equals("Device") || mFragment.equals("Battery") || mFragment.equals("Display") || mFragment.equals("Ram") ) {
this.mLayout = R.layout.cardview_layout;
this.mTextInfoA = R.id.info_name;
this.mTextInfoB = R.id.info_value;
} else if( mFragment.equals("Storage") ){
this.mLayout = R.layout.cardview_storage_layout;
this.mTextInfoA = R.id.storage_total_value;
this.mTextInfoB = R.id.storage_free_value;
this.mTextInfoC = R.id.storage_textview;
}
}
// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(mLayout, parent, false);
ViewHolder vHolder = new ViewHolder(v);
return vHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
Info info = mDataset.get(i);
viewHolder.mTextInfoa.setText(info.a);
viewHolder.mTextInfob.setText(info.b);
if (!info.c.isEmpty()) {
if (info.c.equals("Internal Storage")) {
viewHolder.mTextInfoc.setBackgroundColor(Color.parseColor("#2196F3"));
} else if (info.c.equals("External Storage")) {
viewHolder.mTextInfoc.setBackgroundColor(Color.parseColor("#F44336"));
}
viewHolder.mTextInfoc.setText(info.c);
}
}
@Override
public int getItemCount() {
return mDataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextInfoa;
public TextView mTextInfob;
public TextView mTextInfoc;
public ViewHolder(View itemView) {
super(itemView);
mTextInfoa = (TextView) itemView.findViewById(mTextInfoA);
mTextInfob = (TextView) itemView.findViewById(mTextInfoB);
mTextInfoc = (TextView) itemView.findViewById(mTextInfoC);
}
}
单击前三个选项卡可以,但是如果我在第一个选项卡上并且直接单击第四个或第五个选项卡,它会因该错误而崩溃。
我的活动 OnCreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
我的主要布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />