Custom view nested use, simple operation.

Simple custom 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); }}Copy the code
R.layout.demo_01
<? The 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>Copy the code

One: nested use (recyclerView)

1: create RecyclerView related layout files first

RecyclerView

<? The 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>Copy the code

RecyclerView – item

<? The 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>Copy the code
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")); }}Copy the code
3: nested use
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); }}Copy the code

Use on layout

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

Android small white one, brain is not enough, notes to gather together, daily update small white knowledge (own knowledge, haha),

Write a small story: there is no story to say, later make up the story, record every day, come on, I will be better, full of hope for life! Don’t think about mortgages, car insurance, getting married, having kids, blah, blah, blah.