ViewPager嵌套RecycleView(Fragment作为数据源)来展示10条数据,每个Item显示的内容是(Android软件开发工程师)(用自定义的Adapter 实现

自定义布局文件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。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);

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值