用Android Studio编写微信五个界面代码、
时间: 2023-11-22 22:25:29 浏览: 109
这里提供一个简单的示例代码,实现微信五个界面的切换和基本功能:
MainActivity.java
```
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout tabLayout;
private List<Fragment> fragmentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.view_pager);
tabLayout = findViewById(R.id.tab_layout);
fragmentList = new ArrayList<>();
fragmentList.add(new WeChatFragment());
fragmentList.add(new ContactsFragment());
fragmentList.add(new DiscoverFragment());
fragmentList.add(new MeFragment());
fragmentList.add(new AddFriendFragment());
FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "微信";
case 1:
return "通讯录";
case 2:
return "发现";
case 3:
return "我";
case 4:
return "添加朋友";
default:
return "";
}
}
};
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
}
```
WeChatFragment.java
```
public class WeChatFragment extends Fragment {
private RecyclerView recyclerView;
private List<ChatItem> chatItemList;
private ChatListAdapter chatListAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_we_chat, container, false);
recyclerView = view.findViewById(R.id.recycler_view);
chatItemList = new ArrayList<>();
chatItemList.add(new ChatItem("张三", "你好啊", "14:20", 1));
chatItemList.add(new ChatItem("李四", "在干嘛呢", "12:30", 2));
chatItemList.add(new ChatItem("王五", "今天天气真好", "11:20", 0));
chatItemList.add(new ChatItem("赵六", "明天去哪里玩啊", "10:10", 3));
chatItemList.add(new ChatItem("小明", "你吃饭了吗?", "09:20", 0));
chatItemList.add(new ChatItem("小红", "听说你要搬家了", "08:30", 0));
chatItemList.add(new ChatItem("小李", "你喜欢看电影吗", "07:40", 0));
chatItemList.add(new ChatItem("小刚", "周末有空吗", "06:50", 0));
chatItemList.add(new ChatItem("小美", "我刚买了一件新衣服", "05:30", 0));
chatItemList.add(new ChatItem("小丽", "你看了最新的电视剧吗", "04:20", 0));
chatListAdapter = new ChatListAdapter(getActivity(), chatItemList);
recyclerView.setAdapter(chatListAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
}
```
ContactsFragment.java
```
public class ContactsFragment extends Fragment {
private ListView listView;
private List<ContactItem> contactItemList;
private ContactListAdapter contactListAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contacts, container, false);
listView = view.findViewById(R.id.list_view);
contactItemList = new ArrayList<>();
contactItemList.add(new ContactItem("张三", "13812345678"));
contactItemList.add(new ContactItem("李四", "13912345678"));
contactItemList.add(new ContactItem("王五", "13612345678"));
contactItemList.add(new ContactItem("赵六", "13712345678"));
contactItemList.add(new ContactItem("小明", "13512345678"));
contactItemList.add(new ContactItem("小红", "13412345678"));
contactItemList.add(new ContactItem("小李", "13312345678"));
contactItemList.add(new ContactItem("小刚", "13212345678"));
contactItemList.add(new ContactItem("小美", "13112345678"));
contactItemList.add(new ContactItem("小丽", "13012345678"));
contactListAdapter = new ContactListAdapter(getActivity(), contactItemList);
listView.setAdapter(contactListAdapter);
return view;
}
}
```
DiscoverFragment.java
```
public class DiscoverFragment extends Fragment {
private Button btnScan;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_discover, container, false);
btnScan = view.findViewById(R.id.btn_scan);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "扫一扫", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
```
MeFragment.java
```
public class MeFragment extends Fragment {
private TextView tvName;
private TextView tvAccount;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_me, container, false);
tvName = view.findViewById(R.id.tv_name);
tvAccount = view.findViewById(R.id.tv_account);
tvName.setText("张三");
tvAccount.setText("123456789");
return view;
}
}
```
AddFriendFragment.java
```
public class AddFriendFragment extends Fragment {
private EditText etName;
private Button btnSearch;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_friend, container, false);
etName = view.findViewById(R.id.et_name);
btnSearch = view.findViewById(R.id.btn_search);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etName.getText().toString();
Toast.makeText(getActivity(), "搜索:" + name, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
```
以上是实现微信五个界面的示例代码,具体实现过程中还需要根据实际需求进行相应的调整和优化。
阅读全文