android 继承relativelayout,Android自定义View(RelativeLayout),并嵌套(recyclerView)

自定义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

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">

android:id="@+id/demo_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Demo" />

一:嵌套使用(recyclerView)

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

RecyclerView

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">

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" />

RecyclerView——item

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/item_text"

android:textSize="50dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

2:Adapter

public class DemoAdapter extends RecyclerView.Adapter {

private List list;

private Context context;

public DemoAdapter(Context context, List 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 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);

}

}

布局上使用

android:id="@+id/demoview"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

Android小白一枚,脑子不够,笔记来凑,

日常更新小白知识点(自己的知识点,啊哈哈),

写个小故事吧:

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值