每日总结

ViewPage学习
viewpage可以实现也页面的滑动,页面滑动的内容可以是fragment也可以是普通文本或者其他。
1:在main.xml里布局viewpage,然后新建一个文本布局(要有id值)
2:将viewpage和这个文本布局适配
package com.example.viewpage;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewPagerHolder> {
    private  List<String> titles=new ArrayList<>();
    private  List<Integer> colors=new ArrayList<>();
    public  ViewPagerAdapter(){
        titles.add("zyk");
        titles.add("zqs");
        titles.add("zjr");
        titles.add("cjy");

        colors.add(R.color.colorAccent);
        colors.add(R.color.colorPrimary);
        colors.add(R.color.colorPrimaryDark);
        colors.add(R.color.black);

    }
    @NonNull
    @Override
    public ViewPagerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //解析文本布局的xml文件
        return new ViewPagerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewPagerHolder holder, int position) {
        //将新的文本设置到文本布局里面,将颜色与文字绑定滑动
        holder.textView.setText(titles.get(position));
        holder.relativeLayout.setBackgroundResource(colors.get(position));
    }

    @Override
    public int getItemCount() {
        return 4;
    }
    class   ViewPagerHolder extends RecyclerView.ViewHolder{
        TextView textView;
        RelativeLayout relativeLayout;
        public ViewPagerHolder(@NonNull View itemView) {
            super(itemView);
            relativeLayout=itemView.findViewById(R.id.container);
            textView=itemView.findViewById(R.id.tvTitle);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">


    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/viewpage2"
        android:background="@color/colorPrimary"/>



</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tvTitle"
        android:text="hello word"
        android:layout_gravity="center_vertical"
        android:textColor="#ff4535"
        android:textSize="32dp"
        />
</RelativeLayout>
package com.example.viewpage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager2 viewPager2=findViewById(R.id.viewpage2);
        ViewPagerAdapter viewpageAdapt=new  ViewPagerAdapter();
        viewPager2.setAdapter(viewpageAdapt);
    }
}

 

posted @ 2023-02-24 00:04  cojames  阅读(11)  评论(0编辑  收藏  举报