我正在努力制作像Google Play / Movies这样的应用设计。
我已根据此示例制作并自定义了SlidingTabs: https://developer.android.com/samples/SlidingTabsBasic/project.html
要在每个标签上显示不同的布局,我将此代码与differemt R.layout_pager_item一起使用:
public Object instantiateItem(ViewGroup container, int position) {
View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
container, false);
container.addView(view);
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setText(String.valueOf(position + 1));
return view;
}
但是当我添加:
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
到R.layout_pager_item我有错误: 尝试在空对象引用上调用虚方法'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)'
RecyclerView涵盖了我的所有标签,我只看到了TollBar和我的RecyclerView。
如何组织标签和RecyclerView?
现在我使用这段代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
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.ArrayList;
public class ContentFragment extends Fragment{
private static final String KEY_TITLE = "title";
private static final String KEY_INDICATOR_COLOR = "indicator_color";
private static final String KEY_DIVIDER_COLOR = "divider_color";
private static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";
int pageNumber;
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<String> items;
public static ContentFragment newInstance(CharSequence title, int indicatorColor,
int dividerColor, int page) {
Bundle bundle = new Bundle();
bundle.putCharSequence(KEY_TITLE, title);
bundle.putInt(KEY_INDICATOR_COLOR, indicatorColor);
bundle.putInt(KEY_DIVIDER_COLOR, dividerColor);
bundle.putInt(ARGUMENT_PAGE_NUMBER, page);
ContentFragment fragment = new ContentFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
switch (pageNumber){
case 0:
case 1:
case 2:
return inflater.inflate(R.layout.pager_item, container, false);
mRecyclerView = (RecyclerView) mRecyclerView.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
items = new ArrayList<String>();
mAdapter = new MyAdapter(items);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
for(int i = 0; i < 10; i++) {
mAdapter.add("Title: " + i);
}
case 3:
return inflater.inflate(R.layout.pager_item2, container, false);
}
return null;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
Abd与LinearLayout管理器有错误,它仅适用于ActionBarActivity。但是当我从Fragment更改为ActionBarActivity然后getArguments停止工作
答案 0 :(得分:0)
您需要一个包含所有标签的Viewpager
。
每个标签都是带有元素列表的Recycler View
答案 1 :(得分:0)
我在Fragment中看到很多错误,更改onCreateView()
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pager_item, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
items = new ArrayList<String>();
mAdapter = new MyAdapter(items);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
for(int i = 0; i < 10; i++) {
mAdapter.add("Title: " + i);
}
return view;
}
我不知道您拥有的其他课程(您的ViewPage,SlidingTab,Adapters等)。