赞
踩
设计一个类微信界面,主要分为上中下三个部分,通过下方按钮实现“聊天”、“联系人”、“发现”以及“设置”这四个页面的切换,并在“聊天”页面中实现列表功能。
主页面布局分为上中下三个部分,顶部由top.xml文件实现,因为需要切换四个页面,所以中间的内容分别包含在fragment1.xml、fragment2.xml、fragment3.xml和fragment4.xml中,底部由buttom.xml文件实现。使用include插入top.xml、中间的fragment.xml以及buttom.xml文件。
具体代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <include
- layout="@layout/top"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
-
- <androidx.fragment.app.FragmentContainerView
- android:id="@+id/content"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1" />
-
- <include
- layout="@layout/buttom"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/textView5"
- android:layout_width="match_parent"
- android:layout_height="140dp"
- android:layout_gravity="center"
- android:layout_weight="1"
- android:gravity="center"
- android:text="聊天界面"
- android:textSize="40dp" />
-
- <androidx.recyclerview.widget.RecyclerView
- android:id="@+id/recyclerview"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1" />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView
- android:id="@+id/textView6"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:textSize="50dp"
- android:gravity="center"
- android:layout_gravity="center"
- android:text="联系人界面" />
- </LinearLayout>
- package com.example.myapplication1;
-
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- import androidx.fragment.app.Fragment;
-
- public class Fragment2 extends Fragment {
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
-
- // Inflate the layout for this fragment
- return inflater.inflate(R.layout.fragment2, container, false);
- }
- }
- package com.example.myapplication1;
-
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- import androidx.fragment.app.Fragment;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class Fragment1 extends Fragment {
-
- RecyclerView recyclerView;
- Myadapter myadapter;
- View view1;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- view1=new View(getContext());
-
- view1= inflater.inflate(R.layout.fragment1, container, false);
- recyclerView= view1.findViewById(R.id.recyclerview);
-
- List<String> list=new ArrayList();
- for (int i=1;i<10;i++){
- list.add("这是第"+i+"行:");
- }
-
- myadapter=new Myadapter(list,view1.getContext());
- recyclerView.setAdapter(myadapter);
-
- LinearLayoutManager manager=new LinearLayoutManager(view1.getContext());
- manager.setOrientation(RecyclerView.VERTICAL);
-
- recyclerView.setLayoutManager(manager);
- return view1;
- }
- }
代码说明:主要功能是为Fragment绑定一个列表视图RecycleView。通过findViewById()方法获取RecyclerView对象,并将其设置为垂直方向的线性布局。然后创建了一个List<String>对象list,用于存储数据。接着创建一个Myadapter对象,并将其设置为RecyclerView的适配器。
为RecyclerView提供数据并创建对应的视图项
- package com.example.myapplication1;
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
-
- import androidx.annotation.NonNull;
- import androidx.recyclerview.widget.RecyclerView;
-
- import java.util.List;
-
- public class Myadapter extends RecyclerView.Adapter<Myadapter.Myholder> {
- List<String> list1;
- Context context1;
-
- // 构造函数,传入数据源和内容
- public Myadapter(List list, Context context){
- list1=list;
- context1=context;
- }
- // 创建 ViewHolder,即创建 RecyclerView 中 item 的布局
- @NonNull
- @Override
- public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
-
- // 加载 item 的布局文件
- View view=LayoutInflater.from(context1).inflate(R.layout.item,parent,false);
- // 创建 ViewHolder 对象
- Myholder myholder=new Myholder(view);
-
- return myholder;
- }
- // 绑定数据到 ViewHolder 上
- public void onBindViewHolder(@NonNull Myholder holder, int position) {
- holder.textView.setText(list1.get(position));
-
- }
- // 返回数据源的大小
- @Override
- public int getItemCount() {
- return list1.size();
- }
-
- // ViewHolder 类,用于保存 item 中的控件对象
- class Myholder extends RecyclerView.ViewHolder{
- TextView textView;
-
- public Myholder(@NonNull View itemView) {
-
- super(itemView);
- // 找到 item 中的 TextView 控件
- textView=itemView.findViewById(R.id.textView9);
- }
- }
-
- }
- private void fragmentHide() {
- FragmentTransaction ft=fm.beginTransaction()
- .hide(fragment1)
- .hide(fragment2)
- .hide(fragment3)
- .hide(fragment4);
- ft.commit();
- }
- private void fragmentshow(Fragment fragment){
- fragmentHide();
- FragmentTransaction ft=fm.beginTransaction()
- .show(fragment);
- ft.commit();
- }
- private void innitial() {
- FragmentTransaction ft=fm.beginTransaction()
- .add(R.id.content,fragment1)
- .add(R.id.content,fragment2)
- .add(R.id.content,fragment3)
- .add(R.id.content,fragment4);
- ft.commit();
- }
- @Override
- public void onClick(View view) {
- if (view.getId() == l1) {
- fragmentshow(fragment1);
- }
- else if (view.getId() == l2) {
- fragmentshow(fragment2);
- }
- else if (view.getId() == l3) {
- fragmentshow(fragment3);
- }
- else if (view.getId() == l4) {
- fragmentshow(fragment4);
- }
-
- }
- package com.example.myapplication1;
-
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.fragment.app.Fragment;
- import androidx.fragment.app.FragmentManager;
- import androidx.fragment.app.FragmentTransaction;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
-
- import android.content.Context;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.LinearLayout;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class MainActivity1 extends AppCompatActivity implements View.OnClickListener {
- Fragment fragment1,fragment2,fragment3,fragment4;
- LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
- static final int l1=R.id.linearLayout1;
- static final int l2=R.id.linearLayout2;
- static final int l3=R.id.linearLayout3;
- static final int l4=R.id.linearLayout4;
- FragmentManager fm;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main1);
-
- linearLayout1=findViewById(R.id.linearLayout1);
- linearLayout2=findViewById(R.id.linearLayout2);
- linearLayout3=findViewById(R.id.linearLayout3);
- linearLayout4=findViewById(R.id.linearLayout4);
-
- fm=getSupportFragmentManager();
- fragment1=new Fragment1();
- fragment2=new Fragment2();
- fragment3=new Fragment3();
- fragment4=new Fragment4();
-
- innitial();
- fragmentHide();
- fragmentshow(fragment1);
- linearLayout1.setOnClickListener(this);
- linearLayout2.setOnClickListener(this);
- linearLayout3.setOnClickListener(this);
- linearLayout4.setOnClickListener(this);
-
- }
-
- private void fragmentHide() {
- FragmentTransaction ft=fm.beginTransaction()
- .hide(fragment1)
- .hide(fragment2)
- .hide(fragment3)
- .hide(fragment4);
- ft.commit();
- }
-
- private void fragmentshow(Fragment fragment){
- fragmentHide();
- FragmentTransaction ft=fm.beginTransaction()
- .show(fragment);
- ft.commit();
- }
-
- private void innitial() {
- FragmentTransaction ft=fm.beginTransaction()
- .add(R.id.content,fragment1)
- .add(R.id.content,fragment2)
- .add(R.id.content,fragment3)
- .add(R.id.content,fragment4);
- ft.commit();
- }
-
- @Override
- public void onClick(View view) {
- if (view.getId() == l1) {
- fragmentshow(fragment1);
- }
- else if (view.getId() == l2) {
- fragmentshow(fragment2);
- }
- else if (view.getId() == l3) {
- fragmentshow(fragment3);
- }
- else if (view.getId() == l4) {
- fragmentshow(fragment4);
- }
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。