用RecycleView嵌套ViewPager(Fragment作为数据源)来展示10条数据,(用自定义的Adapter 实现
2017-02-27 13:33
459 查看
自定义布局文件item.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="30dp"> <TextView android:id="@+id/tv_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25dp" android:background="#fb2e" android:gravity="center"/> </LinearLayout>
listview展示方式的Fragment。xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.work.work2application.ListFragment" android:background="#faeb"> <!-- TODO: Update blank fragment layout --> <android.support.v7.widget.Recycl 4000 erView android:id="@+id/rv_list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#feb"> </android.support.v7.widget.RecyclerView> </LinearLayout> GridView展示方式的Fragment。xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.work.work2application.GridFragment"> <!-- TODO: Update blank fragment layout --> <android.support.v7.widget.RecyclerView android:id="@+id/rv_grid" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#febc"> </android.support.v7.widget.RecyclerView> </FrameLayout> 瀑布流展示方式的Fragment。xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.work.work2application.PbFragment"> <!-- TODO: Update blank fragment layout --> <android.support.v7.widget.RecyclerView android:id="@+id/rv_pb" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f2eb"> </android.support.v7.widget.RecyclerView> </FrameLayout> acvititymain。xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.work.work2application.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#faeb"> </android.support.v4.view.ViewPager> </RelativeLayout>
ListView。Java public class ListFragment extends Fragment { private RecyclerView rv_list; private String[] str; public ListFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view=inflater.inflate(R.layout.fragment_list, container, false); rv_list= (RecyclerView) view.findViewById(R.id.rv_list); intiAdapter(); return view; } public void intiAdapter(){ str=new String[10]; for (int i = 0; i < 10; i++) { str[i]="Android软件开发工程师"; } RecycleAdapter radapter=new RecycleAdapter(str,rv_list.getContext()); rv_list.setLayoutManager(new LinearLayoutManager(rv_list.getContext())); rv_list.setAdapter(radapter); rv_list.addItemDecoration(new DividerItemDecoration(rv_list.getContext(),LinearLayoutManager.VERTICAL)); } GridView。Java public class GridFragment extends Fragment { private RecyclerView rv_grid; private String[] str; public GridFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_grid, container, false); rv_grid= (RecyclerView) view.findViewById(R.id.rv_grid); intiAdapter(); return view; } public void intiAdapter() { str = new String[10]; for (int i = 0; i < 10; i++) { str[i] = "Android软件开发工程师"; } RecycleAdapter radapter = new RecycleAdapter(str, rv_grid.getContext()); rv_grid.setLayoutManager(new GridLayoutManager(rv_grid.getContext(), 2)); rv_grid.setAdapter(radapter); } StaggeredGrid(瀑布流)。Java public class PbFragment extends Fragment { private RecyclerView rv_pb; private String[] str; public PbFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_pb, container, false); rv_pb= (RecyclerView) view.findViewById(R.id.rv_pb); intiAdapter(); return view; } public void intiAdapter(){ str=new String[10]; for (int i = 0; i < 10; i++) { str[i]="Android软件开发工程师"; } RecycleAdapter radapter=new RecycleAdapter(str,rv_pb.getContext()); rv_pb.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); rv_pb.setAdapter(radapter); } MainAcvitity。Java public class MainActivity extends AppCompatActivity { private ViewPager vp; private List<Fragment> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp= (ViewPager) findViewById(R.id.vp); list=new ArrayList<>(); list.add(new com.work.work2application.ListFragment()); list.add(new GridFragment()); list.add(new PbFragment()); FragmentAdapter adapter=new FragmentAdapter(getSupportFragmentManager(),list); vp.setAdapter(adapter); }
自定义adapter。Java public RecycleAdapter(String[] str, Context context) { this.str = str; this.context = context; inflater=LayoutInflater.from(context); } @Override public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=inflater.inflate(R.layout.item_layout,parent,false); MyHolder holder=new MyHolder(view); return new MyHolder(view); } @Override public void onBindViewHolder(MyHolder holder, int position) { holder.tv_item.setText(str[position]); holder.tv_item.setTag(position); // //给瀑布流这是100到400的随机高度 // int height= (int) (100+Math.random()*300); // ViewGroup.LayoutParams params=holder.tv_item.getLayoutParams(); // holder.tv_item.setLayoutParams(params); // holder.tv_item.setText(str[position]); } @Override public int getItemCount() { return str.length; } class MyHolder extends RecyclerView.ViewHolder{ private TextView tv_item; public MyHolder(View itemView) { super(itemView); tv_item= (TextView) itemView.findViewById(R.id.tv_item); } } }
相关文章推荐
- ViewPager嵌套RecycleView(Fragment作为数据源)来展示10条数据,每个Item显示的内容是(Android软件开发工程师)(用自定义的Adapter 实现
- 使用自定义的BaseAdapter实现LIstView的展示
- 使用自定义的BaseAdapter实现LIstView的展示(转)
- 【安卓-自定义布局】安卓App开发思路 一步一个脚印(十四)实现Fragment的缓冲视图和数据,防止重复加载
- FragmentPagerAdapter,实现fragment的数据刷新
- Android中Spinner下拉列表(使用自定义Adapter实现,显示数据取自联系人数据库)
- Android创建自定义视图列表对话框(数据库中的数据作为数据源,使用RecyclerView作为列表)
- eCharts添加自定义geojson数据实现地图展示
- 通过viewpager+fragment结合FragmentPagerAdapter实现自定义view定时轮转
- 使用自定义的BaseAdapter实现LIstView的展示
- 简单图库软件的实现(联网下载图片保存到sdcard在Listview中展示,并作为ContentProvider为其他软件提供图库数据)
- 使用自定义的BaseAdapter实现LIstView的展示
- 自定义的BaseAdapter实现LIstView的展示
- Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定
- view对象和Fragment分别作为数据源实现ViewPager(导航栏)功能
- ViewPager-------Fragment作为ViewPager的数据源(FragmentStatePagerAdapter)
- 使用自定义的BaseAdapter实现LIstView的展示
- eCharts添加自定义geojson数据实现地图展示
- android自定义数据展示view,随手指滑动实现
- 自定义View实现运动类app中3个圆弧展示运动数据