Android自定义View(RelativeLayout),并嵌套(recyclerView)

363 阅读1分钟

自定义view嵌套使用,简单操作。

一:简单的自定义view(relativeLayout)

public class ViewRelativeLayout extends RelativeLayout {
    public ViewRelativeLayout(Context context) {
        super(context);
    }

    public ViewRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ViewRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        //R.layout.demo_01
        View view = LayoutInflater.from( getContext()).inflate(R.layout.demo_01, this, false);
        addView(view);
    }
}
R.layout.demo_01
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/design_default_color_primary"> 
    <TextView
        android:id="@+id/demo_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Demo" />
</RelativeLayout>

一:嵌套使用(recyclerView)

1:先创建RecyclerView相关布局文件

RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/design_default_color_primary">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rel_1"
        android:background="@color/design_default_color_primary_dark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" /> 
</RelativeLayout>

RecyclerView——item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/item_text"
        android:textSize="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
2:Adapter
public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.MyViewHolder> {

    private List<Integer> list;

    private Context context;
    public DemoAdapter(Context context, List<Integer> list) {
        this.context = context;
        this.list = list;

    }

    @NonNull
    @Override
    public DemoAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_adapter, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DemoAdapter.MyViewHolder holder, int position) {
        MyViewHolder viewHolder = (MyViewHolder) holder;

        viewHolder.title.setText(list.get(position).toString() );

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title;
        public TextView content;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.item_text);
            title.setTextColor(Color.parseColor("#F0F0F0"));
        }
    }

3:嵌套使用
public class Demo1 extends RelativeLayout {
    public Demo1(Context context) {
        super(context);

    }

    public Demo1(Context context, AttributeSet attrs) {
        super(context, attrs);
        initview(context);
    }

    public Demo1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    private List<Integer> list;

    TextView textView;

    RecyclerView recyclerView;

    private void initview(Context context) {
        View view = LayoutInflater.from( getContext()).inflate(R.layout.demo_01, this, false);
        recyclerView = view.findViewById(R.id.rel_1);
        init(context);
        addView(view);

    }

    DemoAdapter demoAdapter;

    private void init(Context context) {

        list = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            if (i == 10) {
                list.add(0);
            } else {
                list.add(i + 1);
            }
        }

        demoAdapter = new DemoAdapter(context, list);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 4));
        recyclerView.setAdapter(demoAdapter);
    }

}

布局上使用

 <com.example.demo1.Demo1
        android:id="@+id/demoview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

Android小白一枚,脑子不够,笔记来凑, 日常更新小白知识点(自己的知识点,啊哈哈),

写个小故事吧:     没啥故事可说的了,以后编好了再讲故事,每天记录一下,加油,我会更棒,对生活充满希望!不要想房贷车贷保险结婚生子等等等等。