我有一个应用程序,我使用带有片段的幻灯片选项卡,在每个片段中我都有自定义适配器的recyclerview,我有5个选项卡现在应用程序工作正常,当我滑动时,显示正确的数据,前4个选项卡共享相同对于第五个没有的recyclerview适配器的布局,现在如果我点击第四个选项卡,应用程序崩溃它说它在调试一段时间后无法获得textview我发现布局传递到适配器和文本视图的ID不匹配,因此在这种情况下,第4个选项卡的布局将传递给适配器,但是文本视图的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);
}
}
点击前三个标签即可,但是如果我在第1个标签页上,我直接点击第4个或第5个标签就会出现错误。
我的活动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" />
答案 0 :(得分:0)
我自己修复过,问题是适配器中的变量是静态的。