ViewPager嵌套RecycleView(Fragment作为数据源)来展示10条数据,每个Item显示的内容是(Android软件开发工程师)(用自定义的Adapter 实现
来源:互联网 发布:盘扣式脚手架计算软件 编辑:程序博客网 时间:2024/05/19 19:33
自定义布局文件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.RecyclerView 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。Javapublic 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。Javapublic 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(瀑布流)。Javapublic 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。Javapublic 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); } }}
0 0
- ViewPager嵌套RecycleView(Fragment作为数据源)来展示10条数据,每个Item显示的内容是(Android软件开发工程师)(用自定义的Adapter 实现
- 用RecycleView嵌套ViewPager(Fragment作为数据源)来展示10条数据,(用自定义的Adapter 实现
- ViewPager-------Fragment作为ViewPager的数据源(FragmentPagerAdapter)
- ViewPager-------Fragment作为ViewPager的数据源(FragmentStatePagerAdapter)
- Android HolderView实现的数据源(Adapter)
- Android开发 ScrollView嵌套RelativeLayout、RecycleView(内容显示不全),RelativeLayout底部对齐失效
- android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面)
- android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面)
- android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面)
- android标题栏RadioButton实现,下面展示切换viewpager,fragment,内容是listview
- 【Android开发笔记】viewpager+fragment对adapter的选择
- Android 多层fragment 嵌套时,viewPager不显示的问题
- Android list每个item嵌套ViewPager
- Android 自定义Adapter以实现自定义填充ListView的Item
- Fragment嵌套ViewPager的网络获取数据实现滑动播放
- 封装RecycleView的Adapter并实现列表展示
- viewpager+Fragment结构,Fragment嵌套的Fragment显示不出来
- Android之Listview(item为单选题)自定义adapter,像考试时前面的10几道单选题的实现
- redis-事务操作
- c++对象模型详解之二——《深度探索C++对象模型》
- 神经病院Objective-C Runtime入院第一天——isa和Class
- POJ 3352 Road Construction(边双连通)
- 深入理解RunLoop
- ViewPager嵌套RecycleView(Fragment作为数据源)来展示10条数据,每个Item显示的内容是(Android软件开发工程师)(用自定义的Adapter 实现
- 3n+1数链问题
- 用RecycleView嵌套ViewPager(Fragment作为数据源)来展示10条数据,(用自定义的Adapter 实现
- Theano官方LSTM代码解析(2)
- Git使用介绍
- Delta3d角色注册机制
- im即时通讯实现原理
- JavaScript.Date
- JNI中传数组与字符串