我是android studio的新手,我试图在中间创建一个带有菜单选项卡的选项卡式应用程序,
我在我的片段中使用RecyclerView
,如tutorial
我已按照应有的方式完成所有工作,但是在"将适配器绑定到RecyclerView"第一步我遇到了问题,
findViewById(R.id.my_recycler_id);
返回null
以下是代码:
主要活动
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
ArrayList<DummyItem> dummies;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
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();
}
});
RecyclerView rvDummies =(RecyclerView) findViewById(R.id.listdummies);
// Initialize contacts
dummies = DummyItem.createDummiesList(20);
// Create adapter passing in the sample user data
MyItemRecyclerViewAdapter adapter = new MyItemRecyclerViewAdapter(dummies, this);
//Log.d("D",fragment.toString());
// Attach the adapter to the recyclerview to populate items
rvDummies.setAdapter(adapter);
// That's all!
}
... (some other code)
}
我的经历
public class ItemFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
RecyclerView fragmentchildRecyclerView;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemFragment() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static ItemFragment newInstance(int columnCount) {
ItemFragment fragment = new ItemFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
fragmentchildRecyclerView = (RecyclerView) view.findViewById(R.id.listdummies);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS, context));
}
return view;
}
public RecyclerView getRecyclerView(){
return fragmentchildRecyclerView;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(DummyItem item);
}
}
虚拟物品
public static class DummyItem {
public final String id;
public final String content;
public final String details;
public DummyItem(String id, String content, String details) {
this.id = id;
this.content = content;
this.details = details;
}
@Override
public String toString() {
return content;
}
public static ArrayList<DummyItem> createDummiesList(int numDummies){
ArrayList<DummyItem> dummies = new ArrayList<DummyItem>();
for (int i = 1; i <= numDummies; i++) {
dummies.add(new DummyItem("id "+numDummies,"title "+numDummies,"description "+numDummies));
}
return dummies;
}
}
我搜索了很多,我发现它可能在创建之前调用findViewByID
,而其他人则说该片段应该有一个函数返回{{1}所以我可以访问它......
任何建议都会有所帮助。
答案 0 :(得分:1)
当组件位于片段中时,您无法引用活动中的组件。相反,添加您的
RecyclerView rvDummies = (RecyclerView) view.findViewById(R.id.listdummies);
在Fragment的OnCreateView方法中并在那里设置适配器。这里视图对象将是Fragment可能膨胀的布局。
这样做的理由:由于片段将具有单独的布局,因此必须在片段中引用其中包含的组件作为片段膨胀的视图(布局)的子元素。