Android studio微信界面简单制作(二)
在第一次的基础上我们来做一个进阶的,主要是为了熟悉UI设计
实验目标和实验内容:
1、掌握UI设计中的layout布局(约束布局)与基本控件(button、text、imageview等);
2、掌握复杂控件与adapter的使用
实验结果:
核心代码
MainActivity.java:
package com.example.mywechat2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Fragment mt1=new weixinBlankFragment();
private Fragment mt2=new friendBlankFragment();
private Fragment mt3=new contactBlankFragment();
private Fragment mt4=new settingBlankFragment();
private FragmentManager fm;
private LinearLayout m1;
private LinearLayout m2;
private LinearLayout m3;
private LinearLayout m4;
private ImageButton n1;
private ImageButton n2;
private ImageButton n3;
private ImageButton n4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
initFragment();
initEvent();
selectFragment(0);
}
private void initFragment(){
fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.content,mt1);
transaction.add(R.id.content,mt2);
transaction.add(R.id.content,mt3);
transaction.add(R.id.content,mt4);
transaction.commit();
}
private void initView(){
m1 = (LinearLayout)findViewById(R.id.weixin);
m2 = (LinearLayout)findViewById(R.id.friend);
m3 = (LinearLayout)findViewById(R.id.contact);
m4 = (LinearLayout)findViewById(R.id.setting);
n1 = (ImageButton)findViewById(R.id.weixinimage);
n2 = (ImageButton)findViewById(R.id.friendimage);
n3 = (ImageButton)findViewById(R.id.contactimage);
n4 = (ImageButton)findViewById(R.id.settingimage);
}
private void initEvent(){
n1.setOnClickListener(this);
n2.setOnClickListener(this);
n3.setOnClickListener(this);
n4.setOnClickListener(this);
}
private void selectFragment(int i){
FragmentTransaction transaction = fm.beginTransaction();
hideFragment(transaction);
switch (i){
case 0:
transaction.show(mt1);
n1.setImageResource(R.drawable.tab_weixin_pressed);
break;
case 1:
transaction.show(mt2);
n2.setImageResource(R.drawable.tab_find_frd_pressed);
break;
case 2:
transaction.show(mt3);
n3.setImageResource(R.drawable.tab_address_pressed);
break;
case 3:
transaction.show(mt4);
n4.setImageResource(R.drawable.tab_settings_pressed);
break;
default:
break;
}
transaction.commit();
}
private void hideFragment(FragmentTransaction transaction){
transaction.hide(mt1);
transaction.hide(mt2);
transaction.hide(mt3);
transaction.hide(mt4);
}
@Override
public void onClick(View view) {
reset();
switch (view.getId()){
case R.id.weixinimage:
selectFragment(0);
break;
case R.id.friendimage:
selectFragment(1);
break;
case R.id.contactimage:
selectFragment(2);
break;
case R.id.settingimage:
selectFragment(3);
break;
default:
break;
}
}
public void reset(){
n1.setImageResource(R.drawable.tab_weixin_normal);
n2.setImageResource(R.drawable.tab_find_frd_normal);
n3.setImageResource(R.drawable.tab_address_normal);
n4.setImageResource(R.drawable.tab_settings_normal);
}
}
contactAdapter.java:
package com.example.mywechat2;
import android.content.Context;
import android.util.Log;
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;
import java.util.ArrayList;
public class contactAdapter extends RecyclerView.Adapter<contactAdapter.t3ViewHolder> {
private static final String TAG = contactAdapter.class.getSimpleName();
private Context mContext;
private List<contactDataBean> mList = new ArrayList<>();
public contactAdapter(Context context) {
mContext = context;
}
public void sett3DataList(List<contactDataBean> list) {
Log.d(TAG, "sett3DataList: " + list.size());
mList = list;
notifyDataSetChanged();
}
@NonNull
@Override
public t3ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.t3_recycle_item, parent, false);
return new t3ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull t3ViewHolder holder, int position) {
holder.t3Name.setText(mList.get(position).getArea());
holder.t3Content.setText(mList.get(position).getTeam());
if (position == 0) {
holder.t3Name.setVisibility(View.VISIBLE);
} else {
if (mList.get(position).getArea().equals(mList.get(position - 1).getArea())) {
holder.t3Name.setVisibility(View.GONE);
} else {
holder.t3Name.setVisibility(View.VISIBLE);
}
}
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
public class t3ViewHolder extends RecyclerView.ViewHolder {
TextView t3Name, t3Content;
public t3ViewHolder(View itemView) {
super(itemView);
t3Name = itemView.findViewById(R.id.t3_group);
t3Content = itemView.findViewById(R.id.t3_mem);
}
}
}
weixinAdapter.java:
package com.example.mywechat2;
import android.content.Context;
import android.util.Log;
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.ArrayList;
import java.util.List;
public class weixinAdapter extends RecyclerView.Adapter<weixinAdapter.t1ViewHolder> {
private static final String TAG = weixinAdapter.class.getSimpleName();
private Context mContext;
private List<String> mList = new ArrayList<>();
public weixinAdapter(Context context) {
mContext = context;
}
public void sett1DataList(List<String> list) {
Log.d(TAG, "sett1DataList: " + list.size());
mList = list;
notifyDataSetChanged();
}
@NonNull
@Override
public t1ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.t1_recycle_item, parent, false);
return new t1ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull t1ViewHolder holder, int position) {
holder.t1Name.setText(position + 1 + "");
holder.t1Content.setText(mList.get(position));
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
public class t1ViewHolder extends RecyclerView.ViewHolder {
TextView t1Name, t1Content;
public t1ViewHolder(View itemView) {
super(itemView);
t1Name = itemView.findViewById(R.id.tv_num);
t1Content = itemView.findViewById(R.id.tv_content);
}
}
}
t1_recycle_item.xml:
<?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="100dp"
android:layout_margin="8dp"
android:background="@android:color/darker_gray"
android:orientation="vertical">
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
tools:text="1" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
tools:text="内容" />
</LinearLayout>
t3_recycle_item.xml:
<?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="wrap_content"
android:layout_margin="8dp"
android:background="@android:color/darker_gray"
android:orientation="vertical">
<TextView
android:id="@+id/t3_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:padding="8dp"
android:text="小组1"
android:textColor="@android:color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/t3_mem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
tools:text="某某某" />
4、结果截图
这样就实现了一个纵向布局和分组的结合
看了这个博客我懂了很多,大家也可以去看看https://www.jianshu.com/p/c4cfe38a91ed
(喜欢守望先锋的可以加我一起玩啊,我的战网ID:Freak#52373)